- 安装sentinel , https://github.com/alibaba/Sentinel/tree/release-1.8
下载最新的sentinel(1.8)源码到本地
- 注释掉pom.xml中的 sentinel-datasource-nacos
中的test
- 修改resources/app/scripts/directives/sidebar/sidebar.html,
将flowV1改成flow,这样设置规则的时候默认走v2,如下,稍后访问sentinel的时候清理下浏览器缓存。
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
- 在com.alibaba.csp.sentinel.dashboard.rule下创建nacos文件夹创建如下代码,后面5、6、7、8步都在该nacos文件夹下创建:
//加载外部化配置
@ConfigurationProperties(prefix="sentinel.nacos",ignoreUnknownFields = true)
public class NacosPropertiesConfiguration {
private String serverAddr;
private String dataId;
private String groupId = "DEFAULT_GROUP";
private String namespace;
public String getServerAddr() {
return serverAddr;
}
public void setServerAddr(String serverAddr) {
this.serverAddr = serverAddr;
}
public String getDataId() {
return dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
- 创建配置类 NacosConfiguration
@EnableConfigurationProperties(NacosPropertiesConfiguration.class)
@Configuration
public class NacosConfiguration {
@Bean
public Converter<List<FlowRuleEntity>,String> flowRuleEntityEncoder(){
return JSON::toJSONString;
}
@Bean
public Converter<String,List<FlowRuleEntity>> flowRuleEntityDecoder(){
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService(NacosPropertiesConfiguration nacosPropertiesConfiguration) throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfiguration.getServerAddr());
properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfiguration.getNamespace());
return ConfigFactory.createConfigService(properties);
}
}
- 创建一个常量类 NacosConstants
public class NacosConstants {
public static final String DATA_ID_POSTFIX = "-sentinel-flow";
public static final String GROUP_ID = "DEFAULT_GROUP";
}
- 实现动态从nacos配置中心获取流控规则
@Service
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class);
@Autowired
private NacosPropertiesConfiguration nacosConfigProperties;
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String dataID=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString();
String rules = configService.getConfig(dataID, nacosConfigProperties.getGroupId(), 3000);
logger.info("pull FlowRule from Nacos Config:{}",rules);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
- 创建一个流控规则发布类
@Service
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private NacosPropertiesConfiguration nacosPropertiesConfiguration;
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, String> converter;
@Override
public void publish(String appName, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(appName, "appName cannot be empty");
if (rules == null) {
return;
}
String dataID=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString();
configService.publishConfig(dataID, nacosPropertiesConfiguration.getGroupId(), converter.convert(rules));
}
}
- 修改FlowControllerV2类,将7、8步中的类配置进来
@RestController
@RequestMapping(value = "/v2/flow")
public class FlowControllerV2 {
private final Logger logger = LoggerFactory.getLogger(FlowControllerV2.class);
@Autowired
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
- 在application.properties文件中添加Nacos的服务端配置信息
sentinel.nacos.serverAddr=IP:8848
# namespace不配置默认走public命名空间
sentinel.nacos.namespace=fb4f87e6-806d-4a03-85f8-5d55f143f042
sentinel.nacos.groupId=DEFAULT_GROUP