您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何使用Sentinel Dashboard動(dòng)態(tài)推把數(shù)據(jù)同步到Nacos”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何使用Sentinel Dashboard動(dòng)態(tài)推把數(shù)據(jù)同步到Nacos”吧!
下載Sentinel Dashboard的release版本。地址:https://github.com/alibaba/Sentinel/releases
一 :修改pom.xml
中的sentinel-datasource-nacos的依賴(lài),將<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> 流控規(guī)則 </a> </li> |
修改為:
<li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控規(guī)則 </a> </li> |
這樣修改之后就會(huì)跳轉(zhuǎn)到FlowControllerV2的接口。
三:再項(xiàng)目com.alibaba.csp.sentinel.dashboard中新建一個(gè)nacos包來(lái)實(shí)現(xiàn)擴(kuò)展功能。
@Component @ConfigurationProperties(prefix = "nacos.server") public class NacosConfigProperties { private String ip; private String port; private String namespace; private String groupId; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public String getServerAddr() { return this.getIp()+":"+this.getPort(); } @Override public String toString() { return "NacosConfigProperties [ip=" + ip + ", port=" + port + ", namespace=" + namespace + ", groupId=" + groupId + "]"; } }
public final class NacosConfigConstant { public static final String FLOW_DATA_ID_POSTFIX = "-sentinel-flow"; public static final String GROUP_ID = "DEFAULT_GROUP"; }
@Configuration public class NacosConfig { @Autowired private NacosConfigProperties nacosConfigProperties; /** * 非常關(guān)鍵 這里將FlowRuleEntity轉(zhuǎn)換成FlowRule才會(huì)對(duì)客戶(hù)端生效 * @return FlowRule */ @Bean public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { return rules -> JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true); } @Bean public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } @Bean public ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace()); return ConfigFactory.createConfigService(properties); } }
四:編寫(xiě)動(dòng)態(tài)推拉模式的擴(kuò)展代碼
@Component("flowRuleNacosProvider") public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class); @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter<String, List<FlowRuleEntity>> converter; @Override public List<FlowRuleEntity> getRules(String appName) throws Exception { String rules = configService.getConfig(appName + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), 3000); logger.info("從Nacos中拉取到限流規(guī)則信息:{}",rules); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } return converter.convert(rules); } }
@Component("flowRuleNacosPublisher") public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter<List<FlowRuleEntity>, String> converter; @Override public void publish(String app, List<FlowRuleEntity> rules) throws Exception { AssertUtil.notEmpty(app, "app name cannot be empty"); if (rules == null) { return; } configService.publishConfig(app + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), converter.convert(rules)); } }
五:然后將FlowControllerV2中的默認(rèn)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("添加限流規(guī)則成功{}", 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
感謝各位的閱讀,以上就是“如何使用Sentinel Dashboard動(dòng)態(tài)推把數(shù)據(jù)同步到Nacos”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何使用Sentinel Dashboard動(dòng)態(tài)推把數(shù)據(jù)同步到Nacos這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。