這篇文章主要講解了“如何使用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”吧!
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),成都創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:成都搬家公司等網(wǎng)站設(shè)計(jì)、全網(wǎng)整合營(yíng)銷(xiāo)推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。
下載Sentinel Dashboard的release版本。地址:https://github.com/alibaba/Sentinel/releases
一:修改pom.xml
中的sentinel-datasource-nacos的依賴(lài),將
注釋掉,這樣才能在主程序中使用。
|
二:找到resources/app/scripts/directives/sidebar/sidebar.html
中的這段代碼:
修改為:
這樣修改之后就會(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ì)客戶端生效 * @return FlowRule */ @Bean public Converter, String> flowRuleEntityEncoder() { return rules -> JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true); } @Bean public Converter
> 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> { private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class); @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter
> converter; @Override public List 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> { @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter
, String> converter; @Override public void publish(String app, List
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> ruleProvider; @Autowired @Qualifier("flowRuleNacosPublisher") private DynamicRulePublisher
> rulePublisher;
private void publishRules(/*@NonNull*/ String app) throws Exception { Listrules = 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)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!