sentinel的客户端接入与简单配置
如果你是maven项目
1 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<!-- 热点规则控制包 使用热点规则需要配置-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
<version>1.7.1</version>
</dependency>
<!-- sentinel控制台通信包-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-common</artifactId>
<version>1.7.1</version>
</dependency>
<!-- 用于查看Sentinel客户端相关规则 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--sentinel指定nacos为数据源 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.1</version>
</dependency>
2 依赖添加完毕客户端还需要简单配置
# 本工程监听端口
server.port=8090
# 设置sentinel客户端http server与控制台之间通信的端口
spring.cloud.sentinel.transport.port=8731
# 设置控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
#本工程名
spring.application.name=dashboard
# 设置Sentinel Nacos数据源配置;其中flow是数据源名,可以自行随意修改# Nacos数据源地址(需要启动一台Nacos Server)
spring.cloud.sentinel.datasource.flow.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.flow.nacos.dataId=client-flow-rules-${spring.application.name}
spring.cloud.sentinel.datasource.flow.nacos.groupId=SENTINEL_GROUP
spring.cloud.sentinel.datasource.flow.nacos.data-type=json
# 设置Sentinel Nacos数据源配置;其中degrade是数据源名,可以自行随意修改# Nacos数据源地址(需要启动一台Nacos Server)
spring.cloud.sentinel.datasource.degrade.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.degrade.nacos.dataId=client-degrade-rules-${spring.application.name}
spring.cloud.sentinel.datasource.degrade.nacos.groupId=SENTINEL_GROUP
spring.cloud.sentinel.datasource.degrade.nacos.data-type=json
# 设置Sentinel Nacos数据源配置;其中param是数据源名,可以自行随意修改# Nacos数据源地址(需要启动一台Nacos Server)
spring.cloud.sentinel.datasource.param.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.param.nacos.dataId=client-param-rules-${spring.application.name}
spring.cloud.sentinel.datasource.param.nacos.groupId=SENTINEL_GROUP
spring.cloud.sentinel.datasource.param.nacos.data-type=json
# 进行该项设置,可以通过http://localhost:8090/actuator/sentinel访问到该客户端的所有规则
management.endpoints.web.exposure.include=*
我这边之所以这么配置,从前边添加的依赖就可以看出,我这边采用了nacos对sentinel规则进行了持久化
这里有几个注意的点
1,sentinel控制台端口的配置,要与你客户端启动端口保持一致(默认8080)
2,dataId和groupId这两个参数要与nacos持久化规则的dataId和groupId相匹配(源码dataId默认是工程名+“-rule-rules” rule这里虚指,实际是规则对应英文,groupId默认SENTINEL_GROUP)
3 获取nacos规则配置注册到sentinel
@Configuration
public class DataSourceInitFunc {
Logger logger = LoggerFactory.getLogger(DataSourceInitFunc.class);
@Autowired
private SentinelProperties sentinelProperties;
@Bean
public DataSourceInitFunc init(){
sentinelProperties.getDatasource().entrySet().stream().filter(map -> map.getValue().getNacos() != null).forEach(map -> {
NacosDataSourceProperties nacos = map.getValue().getNacos();
// 限流规则,需要Nacos的dataId中包含flow字符串
if(nacos.getDataId().contains(SentinelRuleEnum.FLOW.getValue())) {
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(nacos.getServerAddr(),
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
logger.info("[NacosSource初始化,从Nacos中获取---《限流规则》---注册完成]"+nacos.getDataId());
}
// 降级规则,需要Nacos的dataId中包含degrade字符串
if(nacos.getDataId().contains(SentinelRuleEnum.DEGRADE.getValue())){
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(nacos.getServerAddr(),
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
logger.info("[NacosSource初始化,从Nacos中获取---《熔断规则》---注册完成]"+nacos.getDataId());
}
// 热点规则,需要Nacos的dataId中包含degrade字符串
if(nacos.getDataId().contains(SentinelRuleEnum.PARAM.getValue())){
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleNacosDataSource = new NacosDataSource<>(nacos.getServerAddr(),
nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {
}));
ParamFlowRuleManager.register2Property(paramFlowRuleNacosDataSource.getProperty());
logger.info("[NacosSource初始化,从Nacos中获取---《热点规则》---注册完成]" +nacos.getDataId());
}
});
return new DataSourceInitFunc();
}
}
SentinelRuleEnum.FLOW.getValue() 这里边定义了一个枚举类
public enum SentinelRuleEnum {
/**
* 规则类型定义
*/
FLOW("client-flow"), DEGRADE("client-degrade"),PARAM("client-param"){
@Override
public boolean isRest(){
return true;
}
};
private String value;
private SentinelRuleEnum(String value){
this.value=value;
}
public void setValue(String value) {
this.value = value;
}
public String getValue(){
return value;
}
public boolean isRest(){
return false;
}
}
到这里客户端接入sentinel和获取nacos配置中心规则就完成啦!