4.1.1.2. Spring Cloud

4.1.1.2.1. gateway

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
    <!--不加这个依赖,那么网关中就不支持lb:的route。-->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

通过Spring Cloud原生注解@EnableDiscoveryClient开启服务注册发现功能。

@EnableDiscoveryClient
@SpringBootApplication
public class MySpringCloudGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringCloudGatewayApplication.class, args);
    }

}

声明route:

spring:
  cloud:
    gateway:
      routes:
        - id: my-admin
          uri: lb://my-admin
          predicates:
            - Path=/admin/**

小技巧

route配置,可以写到nacos中,修改也能生效。

以上是最基本的代码,配置好,至少程序可以正常跑。

如果从gateway请求,通过wireshark抓包,可以看到到具体的微服务后,会自动添加一些Forwarded的请求头信息。

GET /admin/hello?name=chenjie2 HTTP/1.1\r\n
User-Agent: curl/8.4.0\r\n
Accept: */*\r\n
X-Request-red: blue\r\n                                                  <-------- 通过AddRequestHeader过滤器添加
Forwarded: proto=http;host="127.0.0.1:17000";for="127.0.0.1:6201"\r\n    <-------- gateway自动添加
X-Forwarded-For: 127.0.0.1\r\n                                           <-------- gateway自动添加
X-Forwarded-Proto: http\r\n                                              <-------- gateway自动添加
X-Forwarded-Port: 17000\r\n                                              <-------- gateway自动添加
X-Forwarded-Host: 127.0.0.1:17000\r\n                                    <-------- gateway自动添加
host: 172.21.176.1:17001\r\n
content-length: 0\r\n
\r\n
[Response in frame: 1038]
[Full request URI: http://172.21.176.1:17001/admin/hello?name=aa]

4.1.1.2.2. nacos

<!-- 服务注册&发现 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.88.251:8848
        username: nacos
        password: nacos
        secret-key: SecretKey012345678901234567890123456789012345678901234567890123456789
        namespace: 3d6e5c2a-0100-4f0e-b434-5d1db1fee2ef
        ip: 192.168.91.7 # 如果主机上有多个IP,设置该属性,注册的服务使用指定的IP。
      config:
        server-addr: 192.168.88.251:8848
        namespace: 3d6e5c2a-0100-4f0e-b434-5d1db1fee2ef
        group: DEFAULT_GROUP
        file-extension: yaml
        username: nacos
        password: nacos
        secret-key: SecretKey012345678901234567890123456789012345678901234567890123456789

如果提示user not found,说明nacos开启了认证,必须在discovery和config下配置用户名、密码、安全密钥信息。

4.1.1.2.3. sentinel

参考链接:

建议采用push模式。

4.1.1.2.3.1. 单节点部署

去Release页面 下载最新版本

nohup java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=10.196.126.43:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.8.jar > sentinel-dashboard.log 2>&1 &

默认用户名和密码都是 sentinel

微服务使用sentinel

spring:
  sentinel:
    transport:
      port: 8719
      dashboard: 10.196.126.43:8080
      client-ip: 192.168.91.7 # 告诉sentinel dashboard,要使用该IP通信。否则,如果主机上有多个IP,则可能注册的IP是错误的,导致通信错误。

代码中声明:

@SentinelResource(value = "sayHello")
public String sayHello(String name) {
    return String.format("hello %s", name);
}

现在可以到sentinel中配置流控规则,比如针对QPS,单机阈值5,直接,快速失败。

4.1.1.2.3.2. 流量控制

流控日志:${user_home}/logs/csp/sentinel-block.log

流控日志

curl http://localhost:8719/clusterNode
curl http://localhost:8719/cnode?id=sayHello
curl http://localhost:8719/tree

4.1.1.2.3.3. 网关

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    <version>${project.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
</dependency>