SpringCloud Gateway整合sentinel(实现限流)

303 阅读2分钟

这是我参与「掘金日新计划 · 8 月更文挑战」的第13天,点击查看活动详情

b75a694fd66444f41e8c757285a58f0.png

前言

前几篇我们使用了 分别使用了sentinel 和gateWay 进行微服务的的限流操作处理,当然专业的事情还是交给专业的去做,今天我们就使用网关gateWay整合sentinel实现限流。废话不多说上代码

首先引入依赖

<!-- 网关服务-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- nacos注册中心-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- sentinel 的数据库()持久化化到nacos-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- 网关整合 snetinel-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

下面我们配置一下配置文件

spring:
  application:
    name: bugvip-gatway
  profiles:
    active: dev
  cloud:
    sentinel:
      enabled: true
     # eager: true 饿汉模式启动
      transport:
        clientIp: 192.168.1.9# 控制台服务地址
        dashboard: 192.168.1.7:8868
        port: 9091
      datasource:#配置数据源
        ds:
          nacos:
            server-addr: 192.168.1.7:8848
            group-id: SENTINEL_GROUP
            data-id: ${spring.application.name}-flow-rules
            data-type: json
            rule-type: flow
      web-context-unify: false
    nacos:
      discovery:
        server-addr: 192.168.1.7:8848 #注册中心 的地址
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true #转换小写
      routes: # 路由转发配置
        - id: bugvip-boss
          uri: lb://bugvip-boss
          predicates:
            - Path=/api/boss/**
          filters:
            - RewritePath=/api/(?<segment>.*),/${segment}

ok 下面我们访问 sentinel的控制台查看网关服务

image.png 我们可以看到左侧的菜单和之前单独使用的时候是有区别的下面我们来使用流控降级

image.png 直接给 boss服务创建限流规则 Burst size 值是阈值超出后允许的大小

image.png 当然可以通过IP 域名 请求头 cookie 进行限流

image.png 下面我们演示一下IP 127.0.0.1

image.png 进行请求测试 image.png 如果我们 要使用指定的接口限流怎么操作了 api分组 就可以实现。首先我们创建一个api分组

image.png 再次添加的流控规则的时候我们就可以选择api分组并进行选择刚才我们创建的api分组

image.png 进行请求测试

image.png 当然这个触发流控之后的返回信息我们可以自定义

@Slf4j
@Component
public class BugVipBlockExceptionHandler implements BlockRequestHandler {

    @Override
    public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable ex) {
        log.info("sentinel handler:");
        String msg = null;
        if (ex instanceof FlowException) {
            msg = "资源被限流了";
        } else if (ex instanceof DegradeException) {
            msg = "资源降级了";
        } else if (ex instanceof ParamFlowException) {
            msg = "热点参数限流";
        } else if (ex instanceof SystemBlockException) {
            msg = "系统规则(负载/...不满足要求)";
        } else if (ex instanceof AuthorityException) {
            msg = "授权规则不通过";
        }
        // http状态码
        return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(R.error().message(msg)));
    }
}

可以看到自定义的限流返回已经生效 image.png 可以看到后台打印的流控异常信息 image.png 其他的功能就不一一演示了,和 sentinel差不多使用基本上没什么太大区别

实践是检验真理的唯一准则,感兴趣的可以去试试呀!明天见咯 😃😃😃😃