1、添加依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.6.3</version>
</dependency>
添加限流规则,设置限流参数
//设置API
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("account_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/api/v1/account/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
//设置规则
rules.add(new GatewayFlowRule("userplatform")
.setCount(2)
.setIntervalSec(1)
);
rules.add(new GatewayFlowRule("account_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(5)
.setIntervalSec(1)
);
具体配置类
@Configuration
public class GatewayConfiguration {
private String RESOURCE_NAME = "xxxA";
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(-5)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
@PostConstruct
public void doInit() {
initCustomizedApis();
initGatewayRules();
}
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("account_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/api/v1/account/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("account_api_limit_accountId")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/api/v1/account/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("userplatform")
.setCount(2)
.setIntervalSec(1)
);
rules.add(new GatewayFlowRule("account_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(3)
.setIntervalSec(1)
);
/* rules.add(new GatewayFlowRule("account_api_limit_accountId")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(1)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("accountId")
)
);*/
GatewayRuleManager.loadRules(rules);
}
}
2、启动dashboard
dashboard下载地址:Sentinel releases
java -Dserver.port=8080 -Dproject.name=sentinel-dashboard-test -jar ~/soft/sentinel-dashboard-1.6.3.jar
浏览器访问:localhost:8080 默认用户名和密码都是sentinel。
连接dashboard
3、在gateway项目中添加JVM参数
-Djava.net.preferIPv4Stack=true -Dcsp.sentinel.dashboard.server=localhost:8080 -Dcsp.sentinel.api.port=8666 -Dproject.name=MYAPP -Dcsp.sentinel.app.type=1
例如idea中添加JVM参数
说明:-Dcsp.sentinel.app.type=1 这个参数是Gateway接入Sentinel才需要配置,配置上之后dashboard会展示出为gateway特别定制的界面。而且这个功能在1.6.3版本才有,于2019.07合入的新功能。
4、触发客户端连接控制台
客户端配置好了与控制台的连接参数之后,并不会主动连接上控制台,需要触发一次客户端的规则才会开始进行初始化,并向控制台发送心跳和客户端规则等信息。(注意)
流控规则:
5、请求链路:
6、API管理:
7、更改流控规则
动态生效,限流后会报Blocked by Sentinel: ParamFlowException异常。
可以针对请求头中的参数如Authorization,请求参数名和参数值,uri,ip等进行限流。