nacos 实现配置文件的热更新,服务不用重启即可读取到nacos配置中心修改发布后的最新值,spring,springboot项目读取本地配置文件的各种方式;文章中介绍了一下注解的使用:@NacosConfigurationProperties,@NacosPropertySource,@ConfigurationProperties,@NacosValue,@value,@RefreshScope,@EnableNacosConfig。
@RefreshScope + @Value
1
2
3
|
teacher:
name: 张三
age: 36
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@SpringBootApplication
// 增加@EnableNacosConfig才能使用@NacosValue注解和@NacosConfigurationProperties注解获取nacos配置中心的配置文件内容
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
// 读取配置文件代码
@Data
@Component
@RefreshScope
public class TestValue3 {
@Value("${teacher.name}")
private String name;
@Value("${teacher.age}")
private Integer age;
}
// controller注入:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowired private TestValue3 testValue3;
@GetMapping(value = "/getValue9")
public Object getValue9() {
TestValue3 value3 = new TestValue3();
value3.setName(testValue3.getName());
value3.setAge(testValue3.getAge());
return value3;
}
}
|
@ConfigurationProperties
配置文件不变,读取配置文件的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "teacher")
public class TestRefreshScope1 {
private String name;
private Integer age;
}
// controller注入:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowired private TestRefreshScope1 testRefreshScope1;
@GetMapping(value = "/getValue5")
public Object getValue5() {
// 因为RefreshScope使用的动态代理,这里直接返回testRefreshScope1会报错,所以重新new对象从testRefreshScope1获取值在返回
// No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver
TestRefreshScope1 scope1 = new TestRefreshScope1();
scope1.setName(testRefreshScope1.getName());
scope1.setAge(testRefreshScope1.getAge());
return scope1;
}
}
|
@NacosConfigurationProperties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Data
@Component
@NacosConfigurationProperties(dataId = "config-teacher.yaml", prefix = "teacher", autoRefreshed = true)
public class TestNacosConfiguration1 {
private String name;
private Integer age;
}
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowired
private TestNacosConfiguration1 testNacosConfiguration1;
@GetMapping(value = "/getValue7")
public Object getValue7() {
return testNacosConfiguration1;
}
}
|
@NacosPropertySource 和 @ConfigurationProperties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Data
@Component
@NacosPropertySource(dataId = "config-teacher.yml", autoRefreshed = true)
@ConfigurationProperties(prefix = "teacher")
public class TestConfiguration3 {
private String name;
private Integer age;
}
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowired
private TestConfiguration3 testConfiguration3;
@GetMapping(value = "/getValue8")
public Object getValue8() {
return testConfiguration3;
}
}
|
Spring Boot 读取配置
@Value
1
2
3
4
5
6
7
8
9
10
11
|
@RestController
@RequestMapping("/config")
public class ConfigController {
@Value("${user.name}")
private String userName;
@GetMapping(value = "/getValue1")
public Object getValue1() {
return testValue1;
}
}
|
@Value + @PropertySource
1
2
3
4
5
6
7
8
9
|
@Data
@Component
@PropertySource(value = "classpath:config2.properties", encoding = "utf-8")
public class TestValue2 {
@Value("${teacher.name}")
private String name;
@Value("${teacher.age}")
private Integer age;
}
|
@ConfigurationProperties 读取
读取配置文件代码:
1
2
3
4
5
6
7
|
@Data
@Configuration
@ConfigurationProperties(prefix = "student")
public class TestConfiguration1 {
private String name;
private Integer age;
}
|