应用接入nacos方案:
- SpringBoot接入nacos(nacos-config-spring-boot-starter0.2.4版本以下)
- 引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.2.2-RC1</version>
</dependency>
- 启动类添加注解
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${spring.cloud.nacos.config.server-addr}",namespace = "${spring.cloud.nacos.config.namespace}"))
@NacosPropertySource(dataId = "",autoRefreshed = true)
- 启动类添加静态代码块
static {
try {
Properties properties = new Properties();
properties.load(OrderApplication.class.getClassLoader().getResourceAsStream("bootstrap.properties"));
String groupId = properties.getProperty("spring.cloud.nacos.config.group");
String dataId = properties.getProperty("spring.application.name");
NacosPropertySource annotation = OrderApplication.class.getAnnotation(NacosPropertySource.class);
InvocationHandler ih = Proxy.getInvocationHandler(annotation);
Field memberValuesField = ih.getClass().getDeclaredField("memberValues");
memberValuesField.setAccessible(true);
Map map = (Map) memberValuesField.get(ih);
map.put("groupId", groupId);
map.put("dataId", dataId);
} catch (Exception ex) {
ex.printStackTrace();
}
}
样例如下:
- bootstrap.properties配置文件添加配置
spring.cloud.nacos.config.server-addr=xxx.xxx.xxx.xxx:xxxx spring.cloud.nacos.discovery.server-addr=xxx.xxx.xxx.xxx:xxxx spring.cloud.nacos.config.namespace=dev spring.application.name=test.properties spring.cloud.nacos.config.group=ceshi
其中spring.cloud.nacos.config.server-addr为nacos访问地址
spring.cloud.nacos.config.namespace为配置环境
spring.cloud.nacos.config.group 为groupId
spring.application.name为dataId
@NacosValue(value = "${test1:123}", autoRefreshed = true)
autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true
value中为配置项 :后可指定初始默认值
@Value("${test1}") 不支持热更行
- SpringBoot接入nacos(nacos-config-spring-boot-starter0.2.4版本以下)推荐使用
- 引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
- application.properties文件添加配置
nacos.config.bootstrap.enable=true
nacos.config.server-addr=xxx.xxx.xxx.xxx:xxxx
nacos.config.namespace=dev
nacos.config.data-id=test.properties
nacos.config.group=ceshi
nacos.config.auto-refresh=true
3、获取配置
@NacosValue(value = "${test1:123}", autoRefreshed = true)
autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true
value中为配置项 :后可指定初始默认值
@Value("${test1}") 不支持热更行
三、SpringCloud接入nacos
1、引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.2.RELEASE</version> </dependency>
- bootstrap.properties配置文件添加配置
spring.cloud.nacos.discovery.server-addr=xxx.xxx.xxx.xxx:xxxx
spring.cloud.nacos.config.server-addr=xxx.xxx.xxx.xxx:xxxx
spring.cloud.nacos.config.namespace=dev
spring.application.name=example
spring.cloud.nacos.config.group=0.0.1
spring.cloud.nacos.config.file-extension=properties
其中spring.cloud.nacos.config.server-addr为nacos访问地址
spring.cloud.nacos.config.namespace为配置环境
spring.application.name为dataId
spring.cloud.nacos.config.group为groupId
spring.cloud.nacos.config.file-extension固定为properties
3、获取配置
@NacosValue(value = "${test1:123}", autoRefreshed = true)
autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true
value中为配置项 :后可指定初始默认值
@Value("${test1}") 原生不支持热更行 若想支持热更新可在类上加@RefreshScope注解才可