<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>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
spring:
cloud:
sentinel:
eager: true
scg:
fallback:
mode: response
response-body: '{"code":429, "msg": "系统繁忙,请稍后重试"}'
response-status: 200
datasource:
gw-flow: # 此处使用nacos作为数据源,参考com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration
nacos: # 参考com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
data-id: sentinel-gw-flow.json
group-id: DEFAULT_GROUP
data-type: json
rule-type: gw-flow
api-group:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
data-id: sentinel-api-group.json
group-id: DEFAULT_GROUP
data-type: json
rule-type: gw-api-group
sentinel-gw-flow.json,参考com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule
[
{
"resource":"getTicket",
"resourceMode":1,
"degrade":0,
"count":1,
"controlBehavior":3
}
]
sentinel-api-group.json,参考com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition
[
{
"apiName":"getTicket",
"predicateItems":[
{
"pattern":"/api/ticket/adWelfare/givenTicket"
}
]
}
]