配置中心

1.添加依赖
1
2
3
4
5
<dependency>  
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
2.启动类注解 @NacosPropertySource
1
2
3
4
5
6
7
8
9
10
@SpringBootApplication  
// dataId 对应配置管理-配置列表-dataId
@NacosPropertySource(dataId = "nacosDemo", autoRefreshed = true)
public class NacosDemoApplication {

public static void main(String[] args) {
SpringApplication.run(NacosDemoApplication.class, args);
}

}
3.配置nacos地址
1
2
3
spring:  
application:
name: nacos-demo
4.使用配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController  
public class CacheController {

@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;

private static final String template = "useLocalCache is %s!";

@GetMapping("/cache")
public String cache() {
// 默认返回false
return String.format(template, useLocalCache);
}
}
// 此时返回结果 :useLocalCache is false!
  • 增加配置并发布

1
// 返回结果 :useLocalCache is true!