Gateway整合Sentinel熔断降级

添加Maven依赖

在以前的版本中整合 Sentinel 需要手动配置。新版本中添加 spring-cloud-alibaba-sentinel-gateway 依赖就可以了,内部帮我们配置好了。可以参考: SentinelSCGAutoConfiguration

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

配置Sentinel

spring:
  cloud:
    sentinel:
      # 配置Sentinel控制台
      transport:
        dashboard: localhost:8888 # Sentinel 控制台地址
      # 如果需要配置Sentinel全局异常处理,可以添加以下配置
      scg:
        fallback:
          mode: response # 重定向(redirect) 或者 响应(response)
          # redirect: # mode 为 redirect 时,设置重定向URL
          response-status: 200 # 响应状态码
          response-body: "{code: 500, msg: '服务器压力山大,请稍后再试!'}" # 响应内容体

配置规则

配置规则有多种方式

  • 代码中通过Bean的初始化来配置。
  • 通过控制台配置
  • 集成配置中心进行配置

代码中配置

@Configuration
public class GatewayFlowRuleConfig {

    /**
     * 初始化网关流控规则
     */
    @PostConstruct
    public void init() {
        Set<GatewayFlowRule> rules = new HashSet<>();

        rules.add(new GatewayFlowRule()
                .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID) // 模式
                .setResource("user_route") // 资源名,gateway网关路由
                .setGrade(RuleConstant.FLOW_GRADE_QPS) // 流控类型 QPS、线程数
                .setCount(3) // 间隔时间内最大请求次数
                .setIntervalSec(1L) // 间隔时间
                .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT) // 流控方式 默认快速失败
                .setBurst(0) // 超过最大请求次数,还能容忍多少个请求通过
        );

        GatewayRuleManager.loadRules(rules);
    }
}

控制台中配置

控台配置比较直观,方便。缺点就是不能持久化,只要服务重启规则就没有了。控制台规则持久化,可以查看如果持久化
在这里插入图片描述

配置中心配置规则

这里以 Nacos 为例

添加Maven依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

配置Sentinel Datasource

spring:
  cloud:
    sentinel:
      datasource:

        # 名称自定义
        gateway-flow-rule:
          nacos:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            namespace: ${spring.cloud.nacos.discovery.namespace}
            group-id: SENTINEL_GATEWAY_FLOW_GROUP
            data-id: ${spring.application.name}-gateway-flow-rules.json
            # 配置网关的流控规则一定要设置成 gw-flow,flow 是普通流控规则。
            rule-type: gw-flow # 设置 gateway flow 流控规则类型

Nacos添加规则配置

  • 创建Nacos配置文件,命名空间、groupId、dataId与上面配置一致。
  • 配置参数参考代码中配置规则类 GatewayFlowRule
[
	{
		"app": "gateway-server",
		"burst": 0,
		"controlBehavior": 0,
		"count": 5.0,
		"grade": 1,
		"interval": 1,
		"intervalUnit": 0,
		"maxQueueingTimeoutMs": 500,
		"resource": "user_route",
		"resourceMode": 0
	}
]
  • 重启项目,Nacos中的规则配置在控制台就能看到了。
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42270645/article/details/123659480