准备工作:
下载Sentinel Dashboard的release版本。地址:https://github.com/alibaba/Sentinel/releases
一 :修改pom.xml
中的sentinel-datasource-nacos的依赖,将<scope>test</scope>
注释掉,这样才能在主程序中使用。
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <!--<scope>test</scope>--> </dependency>
二:找到resources/app/scripts/directives/sidebar/sidebar.html
中的这段代码:
<li ui-sref-active="active"> <a ui-sref="dashboard.flowV1({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控规则 </a> </li>
修改为:
<li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控规则 </a> </li>
这样修改之后就会跳转到FlowControllerV2的接口。
三:再项目com.alibaba.csp.sentinel.dashboard中新建一个nacos包来实现扩展功能。
1 @Component 2 @ConfigurationProperties(prefix = "nacos.server") 3 public class NacosConfigProperties { 4 5 private String ip; 6 7 private String port; 8 9 private String namespace; 10 11 private String groupId; 12 13 public String getIp() { 14 return ip; 15 } 16 public void setIp(String ip) { 17 this.ip = ip; 18 } 19 public String getPort() { 20 return port; 21 } 22 public void setPort(String port) { 23 this.port = port; 24 } 25 public String getNamespace() { 26 return namespace; 27 } 28 public void setNamespace(String namespace) { 29 this.namespace = namespace; 30 } 31 public String getGroupId() { 32 return groupId; 33 } 34 public void setGroupId(String groupId) { 35 this.groupId = groupId; 36 } 37 public String getServerAddr() { 38 return this.getIp()+":"+this.getPort(); 39 } 40 @Override 41 public String toString() { 42 return "NacosConfigProperties [ip=" + ip + ", port=" + port + ", namespace=" 43 + namespace + ", groupId=" + groupId + "]"; 44 } 45 46 }
public final class NacosConfigConstant { public static final String FLOW_DATA_ID_POSTFIX = "-sentinel-flow"; public static final String GROUP_ID = "DEFAULT_GROUP"; }
1 @Configuration 2 public class NacosConfig { 3 4 @Autowired 5 private NacosConfigProperties nacosConfigProperties; 6 7 /** 8 * 非常关键 这里将FlowRuleEntity转换成FlowRule才会对客户端生效 9 * @return FlowRule 10 */ 11 @Bean 12 public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { 13 return rules -> JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true); 14 } 15 16 @Bean 17 public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { 18 return s -> JSON.parseArray(s, FlowRuleEntity.class); 19 } 20 21 @Bean 22 public ConfigService nacosConfigService() throws Exception { 23 Properties properties = new Properties(); 24 properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr()); 25 properties.put(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace()); 26 return ConfigFactory.createConfigService(properties); 27 } 28 }
四:编写动态推拉模式的扩展代码
1 @Component("flowRuleNacosProvider") 2 public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { 3 4 private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class); 5 6 @Autowired 7 private NacosConfigProperties nacosConfigProperties; 8 9 @Autowired 10 private ConfigService configService; 11 12 @Autowired 13 private Converter<String, List<FlowRuleEntity>> converter; 14 15 @Override 16 public List<FlowRuleEntity> getRules(String appName) throws Exception { 17 String rules = configService.getConfig(appName + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), 3000); 18 logger.info("从Nacos中拉取到限流规则信息:{}",rules); 19 if (StringUtil.isEmpty(rules)) { 20 return new ArrayList<>(); 21 } 22 return converter.convert(rules); 23 } 24 }
1 @Component("flowRuleNacosPublisher") 2 public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { 3 4 @Autowired 5 private NacosConfigProperties nacosConfigProperties; 6 7 @Autowired 8 private ConfigService configService; 9 @Autowired 10 private Converter<List<FlowRuleEntity>, String> converter; 11 12 @Override 13 public void publish(String app, List<FlowRuleEntity> rules) throws Exception { 14 AssertUtil.notEmpty(app, "app name cannot be empty"); 15 if (rules == null) { 16 return; 17 } 18 configService.publishConfig(app + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), converter.convert(rules)); 19 } 20 21 }
五:然后将FlowControllerV2中的默认DynamicRuleProvider
和DynamicRulePublisher
修改为:
@Autowired @Qualifier("flowRuleNacosProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleNacosPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; private void publishRules(/*@NonNull*/ String app) throws Exception { List<FlowRuleEntity> rules = repository.findAllByApp(app); rulePublisher.publish(app, rules); logger.info("添加限流规则成功{}", JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true)); }
六:application.properties配置文件
#nacos nacos.server.ip=localhost nacos.server.port=8848 nacos.server.namespace= nacos.server.group-id=DEFAULT_GROUP