《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
@Configuration
public class NacosConfig {
@Bean
public Converter<List, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, “localhost”);
return ConfigFactory.createConfigService(properties);
}
}
如果用到了namespace隔离环境,可以在nacosConfigService
方法中再加入配置,比如:properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
第五步:实现Nacos的配置拉取。
@Component(“flowRuleNacosProvider”)
public class FlowRuleNacosProvider implements DynamicRuleProvider<List> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List> converter;
public static final String FLOW_DATA_ID_POSTFIX = “-sentinel”;
public static final String GROUP_ID = “DEFAULT_GROUP”;
@Override
public List getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
-
getRules
方法中的appName
参数是Sentinel中的服务名称。 -
configService.getConfig
方法是从Nacos中获取配置信息的具体操作。其中,DataId和GroupId分别对应客户端使用时候的对应配置。比如这里的例子对应了之前我们在《Sentinel使用Nacos存储规则》一文中的配置,具体如下:
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
注意:两边的DataId和GroupId必须对应上。
第六步:实现Nacos的配置推送。
@Component(“flowRuleNacosPublisher”)
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List, String> converter;
public static final String FLOW_DATA_ID_POSTFIX = “-sentinel”;
public static final String GROUP_ID = “DEFAULT_GROUP”;
@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 + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
}
}
- 这里的大部分内容与上一步中的实现一致。主要就是Nacos中存储配置的DataId和GroupId不要弄错。
第七步:修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
中DynamicRuleProvider
和DynamicRulePublisher
注入的Bean,改为上面我们编写的针对Apollo的实现:
@Autowired
@Qualifier(“flowRuleNacosProvider”)
private DynamicRuleProvider<List> ruleProvider;
@Autowired
@Qualifier(“flowRuleNacosPublisher”)
private DynamicRulePublisher<List> rulePublisher;
最后,读者可以使用本文改造后的sentinel-dashboard联合之前《Sentinel使用Nacos存储规则》一文的例子来验证本文内容。
本文介绍内容的客户端代码,示例读者可以通过查看下面仓库中的alibaba-sentinel-dashboard-nacos
项目:
如果您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!
结语
小编也是很有感触,如果一直都是在中小公司,没有接触过大型的互联网架构设计的话,只靠自己看书去提升可能一辈子都很难达到高级架构师的技术和认知高度。向厉害的人去学习是最有效减少时间摸索、精力浪费的方式。
我们选择的这个行业就一直要持续的学习,又很吃青春饭。
虽然大家可能经常见到说程序员年薪几十万,但这样的人毕竟不是大部份,要么是有名校光环,要么是在阿里华为这样的大企业。年龄一大,更有可能被裁。
送给每一位想学习Java小伙伴,用来提升自己。
本文到这里就结束了,喜欢的朋友可以帮忙点赞和评论一下,感谢支持!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
,但这样的人毕竟不是大部份,要么是有名校光环,要么是在阿里华为这样的大企业。年龄一大,更有可能被裁。
送给每一位想学习Java小伙伴,用来提升自己。
[外链图片转存中…(img-P1TPCljX-1714659230015)]
本文到这里就结束了,喜欢的朋友可以帮忙点赞和评论一下,感谢支持!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!