经过@NacosPropertySource能够注入一个配置文件,若是咱们须要将配置分类存储或者某些配置须要共用,这种需求场景下,一个项目中须要加载多个配置文件,能够能够直接使用多个@NacosPropertySource注解便可。html
@NacosPropertySource(dataId = "nacos-springboot", autoRefreshed = true)
@NacosPropertySource(dataId = "redis", autoRefreshed = true)
@SpringBootApplication
public class NacosSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(NacosSpringBootApp.class, args);
}
}
复制代码
或者@NacosPropertySourcesredis
@NacosPropertySources({
@NacosPropertySource(dataId = "nacos-springboot", autoRefreshed = true),
@NacosPropertySource(dataId = "redis", autoRefreshed = true)
})
复制代码
后面咱们会讲解在Spring Cloud中使用,能够直接用配置文件的方式指定多个data-id。spring
先介绍一种比较古老的方式,太繁琐了,以下:api
@NacosInjected
private ConfigService configService;
@PostConstruct
public void init() {
try {
onMessage();
} catch (NacosException e) {
e.printStackTrace();
}
}
public void onMessage() throws NacosException {
configService.addListener("nacos-springboot", "DEFAULT_GROUP", new AbstractListener() {
public void receiveConfigInfo(String config) {
System.err.println(config);
}
});
}
复制代码
经过ConfigService添加一个监听器,监听具体的配置文件springboot
下面使用注解方式来监听,既简单又方便很直接,以下:bash
@NacosConfigListener(dataId = "nacos-springboot")
public void onMessage(String config) {
System.out.println(config);
}
复制代码
固然配置监听还支持多类型的转换,上面的示列只是简单的获取整个配置的字符串,关于更多的类型转换后面咱们单独讲解。框架
Nacos自己是提供Http接口的,经过接口咱们能够对配置进行操做。只是目前SDK这块只支持Java,其余语言的后面会支持,只是目前还没开发,经过后台的示列代码咱们能够看到有支持多语言的计划。spa
其余语言目前可使用API来对接:nacos.io/zh-cn/docs/…code
对比这块你们能够根据自身的需求去作一个比对,只要符合你的需求,那对你来讲这个框架就是好的。cdn
若是说你目前已经在使用Apollo,那么我建议仍是不要换,太折腾。
若是你还没使用Apollo,在对配置中心作选型,这个时候能够去作下详细的对比。从功能点,稳定性,使用,部署等方面去比较。
若是你公司内部用了dubbo的话,我以为Nacos能够考虑用起来,这样既然替代ZK,又能增长一个配置中心的功能,能够说是比较好的方式了。
Nacos部署很是简单,能够直接下载官方编译好的包,解压改改配置文件便可启动,集群部署也是同样的,后面咱们会单独讲解集群部署。
同时还支持Docker部署,熟悉Docker的朋友就方便了。