不死鸟.亚历山大.狼崽子

不死鸟.亚历山大.狼崽子

代码地址:https://download.csdn.net/download/u013938578/87767363

从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可以提供两种资源维度的限流:

  • route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 routeId
  • 自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

网关微服务构建

1.创建子工程sentinel_gateway,在pom.xml文件中引入依赖

        <!--网关的起步依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!--eureka注册中心客户端的起步依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.在引导类中配置eureka客户端开启注解

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //开启Eureka客户端发现功能
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }
}

3.在application.yml配置文件中配置应用的端口、应用的名称、eureka配置

# 端口
server:
  port: 9013

# 应用的名称
spring:
  application:
    name: sentinel-gateway

# eureka配置
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9010/eureka

4.在application.yml配置文件中进行网关配置

# 应用的名称
spring:
  cloud:
    gateway:
      routes:
        - id: sentinel-feign-gateway
          # 路由转发路径
          uri: lb://sentinel-feign-client:9012
          # 断言
          predicates:
            - Path=/hello/**

5.运行测试

启动微服务

  • eureka_server
  • sentinel_feign_client
  • sentinel_feign_provider
  • sentinel_gateway

浏览器输入http://localhost:9013/hello,浏览器会显示出”Hello Sentinel!“的内容,表示网关微服务已经配置好。

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

 

整合sentinel

网关微服务配置好之后,就可以开始整合Spring Cloud GateWay和Sentinel了。

1.在sentinel_gateway的pom.xml中引入依赖

        <!--Spring Cloud Alibaba Sentinel依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

        <!--sentinel支持spring cloud gateway的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

2.创建GatewayConfiguration配置类,配置流控降级回调操作

package com.example.demo.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

@Component
public class GatewayConfiguration {

    //初始化限流或者降级的回调函数
    @PostConstruct
    public void doInit(){
        //设置限流或者降级的回调函数
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            //被限流或降级处理的方法
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                return ServerResponse.status(200).syncBody("系统繁忙,请稍候");
            }
        });
    }

}

3.在application.yml中配置Sentinel控制台访问地址

# 应用的名称
spring:
  # 网关配置
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.222.132:9000

4.运行测试

启动项目,在Sentinel控制中增加关于资源的流控规则,Sentinel在适配Spring Cloud Gateway时提供了两种配置资料的规则

  • route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 routeId自定义 API
  • 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

route维度规则定义

在sentinel控制台中增加流控规则,其中API类型选择“Route ID”,API名称为网关配置的路由id,QPS阀值设置为2。

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

 

 之后,在浏览器中输入http://localhost:9013/hello,慢速刷新,则持续显示”Hello Sentinel”;快速刷新则会交替出现”Hello Sentinel”和“系统繁忙,请稍候”。这说明对资源限流成功。

自定义 API 维度

在sentinel控制台的左侧菜单中选择“API管理”,在弹出的窗口中新增API分组,API名称自己定义,匹配模式选择”前缀“,匹配串设置为网关路由的断言路径。

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

 

 之后,在sentinel控制台的左侧菜单中选择“流控规则”,其中API类型选择“API分组”,API名称为之前添加的API分组的名称,QPS阀值设置为2。

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

 

SpringCloud(23):Sentinel对Spring Cloud Gateway的支持-LMLPHP

 

之后,在浏览器中输入http://localhost:9013/hello,慢速刷新,则持续显示”Hello Sentinel”;快速刷新则会交替出现”Hello Sentinel”和“系统繁忙,请稍候”。这说明对资源限流成功。

05-25 15:02