nacos管理的配置文件内容:
a:
tool: zjw
company: jl
time-out: 1000
data:
home: hb
age: hz
方法一
-
引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
-
在启动类上加注解:@NacosPropertySource
@NacosPropertySource(dataId = "a.yml", autoRefreshed = true)
-
在要使用配置的属性上加注解:@NacosValue
@NacosValue(value = "${a.tool}",autoRefreshed = true)
String value;
完成上述步骤后即可实现nacos管理配置文件中a.tool
配置的热更新。
方法二
-
引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
-
在程序配置文件中加入配置
spring.cloud.nacos.config.refreshable-dataids=a.yml,...,c.yml
-
在需要使用配置的类上加注解:@RefreshScope
@RestController
@RequestMapping("local/nacos")
@RefreshScope
public class NacosAction {
@Value(value = "${a.tool}")
String value;
//http://localhost:8080/local/nacos/getNacosSouce
@RequestMapping("getNacosSouce")
public String getNacosSource(){
return value;
}
}
在第三步或者使用配置类的方式
@Component
@ConfigurationProperties(prefix="a")//配置文件中的前缀
public class NacosConfig {
private String tool;
private String company;
private String timeOut;
private JSONObject data;
....Getter and Setter方法
}
@RestController
@RequestMapping("local/nacos")
public class NacosAction {
@Autowired
private NacosConfig nacosConfig;
//http://localhost:8080/local/nacos/getNacosSouceByConfig
@RequestMapping("getNacosSouceByConfig")
public NacosConfig getNacosSouceByConfig(){
return nacosConfig;
}
}
完成上述步骤后即可实现nacos管理配置文件中a.tool
或前缀为a
配置的热更新。