Add Maven dependency
Integrating Sentinel in previous versions required manual configuration. In the new version, it is enough to add spring-cloud-alibaba-sentinel-gateway dependency, which is configured for us internally. You can refer to: 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>
Configure 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: '服务器压力山大,请稍后再试!'}" # 响应内容体
Configure rules
There are several ways to configure rules
- The code is configured by Bean initialization.
- Configuration via console
- Integrated configuration center for configuration
configuration in code
@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);
}
}
Configuration in the console
The console configuration is more intuitive and convenient. The disadvantage is that it cannot be persistent, as long as the service restart rules are gone. Console rules are persistent, you can check if they are persistent .
Configuration Center Configuration Rules
Take Nacos as an example here
Add Maven dependency
<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>
Configure 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 add rule configuration
- Create a Nacos configuration file with the same namespace, groupId, and dataId as above.
- Configure the rule class GatewayFlowRule in the configuration parameter reference code
[
{
"app": "gateway-server",
"burst": 0,
"controlBehavior": 0,
"count": 5.0,
"grade": 1,
"interval": 1,
"intervalUnit": 0,
"maxQueueingTimeoutMs": 500,
"resource": "user_route",
"resourceMode": 0
}
]
- Restart the project, and the rule configuration in Nacos can be seen in the console.