目标
sentinel接入gateway网关,使用nacos做sentinel规则的持久化
官网
https://sentinelguard.io/zh-cn/docs/open-source-framework-integrations.html
接入准备
- 下载sentinel可视化jar包.版本为1.8.6
地址:https://github.com/alibaba/Sentinel/releases - 配置yml
spring:
cloud:
sentinel:
transport:
# 当前服务与控制台交互的端口号,默认为8719,同一个机器上若有多个应用于控制台交互时需要设置成不同的端口
port: 8719
dashboard: 你的控制台所在的ip:8080 #如果是在本地 则是localhost
# 服务启动时直接建立心跳连接
eager: true
scg:
# 流控触发返回
fallback:
response-body: '{"code":-10000, "data":null, "msg":"前方拥堵,请稍后重试"}'
mode: response
response-status: 500
# Sentinel 储存规则的数据源配置(我这里使用的是Nacos来存储Sentinel的限流规则)
datasource:
flow-control:
nacos:
server-addr: 你的nacos ip:8848
data-id: seek-gateway-sentinel-test
data-type: json
group-id: DEFAULT_GROUP
namespace: 你配置的nacos namespace
rule-type: gw_flow
- 新增config配置
@Configuration
public class SentinelConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public SentinelConfiguration(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(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
}
- 当触发限流
环境配置
测试环境
启动
后台启动 (窗口可以随时关闭,不影响应用)
nohup java -Dserver.port=8739 -Dcsp.sentinel.dashboard.server=你的ip:8739 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=sentinel -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.8.jar >sentinel.log 2>&1 &
直接启动(窗口不可关闭,关闭即服务停止)
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
登陆控制台:
你的ip:8739 sentinel/sentinel
规则nacos持久化
由于sentinel的规则存在内存中,如果不将sentinel配置的规则持久化,每次重启项目规则都会消失
测试环境
登陆nacos:你的nacos ip:8848
-
新增一个配置
-
配置微服务规则
api分组初始化
初始化api分组方式
如果需要新增api分组,在下方新增对象,注入 apiDefinitions 即可
@Configuration
public class GatewayConfiguration {
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(-1)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
@PostConstruct
void init() {
// 初始化api分组信息 此处注入会有问题 即api分组持久化 后面在调研,先采用针对服务限流
// initApiDefinitions();
}
// sentinel 初始化api分组信息
private void initApiDefinitions() {
Set<ApiDefinition> apiDefinitions = new HashSet<ApiDefinition>() {
{
//setApiName 设置API名称
add(new ApiDefinition().setApiName("relation")
.setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem()
//SentinelGatewayConstants.URL_MATCH_STRATEGY_EXACT 精确
//SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX 前缀
//SentinelGatewayConstants.URL_MATCH_STRATEGY_REGEX 正则
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
//路由
.setPattern("/app-api/user/relation/*"));
}
}));
}
};
GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
}
}
遗留问题 ?初始化api分组,会导致服务无法注册到sentinel dashboard
sentinel网关限流提供api分组(可以理解为自定义的一些接口集合),但在代码中初始化api分组,会导致服务无法注册到sentinel dashboard 。问题无法解决。见:https://github.com/alibaba/Sentinel/issues/3017