nacos实现配置文件的热更新,服务不用重启即可读取到nacos配置中心修改发布后的最新值,spring,springboot项目读取本地配置文件的各种方式;文章中介绍了一下注解的使用:@NacosConfigurationProperties,@NacosPropertySource,@ConfigurationProperties,@NacosValue,@value,@RefreshScope,@EnableNacosConfig。
使用nacos的企业越来越多,nacos确实很好用,最近就抽时间研究了一下nacos配置文件的热更新的几种方式,顺便复习了一下读取本地配置文件的几种方式。
文章最后提供源码连接,完全自己写的,绝对可用。
一、nacos配置文件热更新的几种方式
热更新,顾名思义就是在不用重启服务的情况下,实现配置的更新,服务的重启可能对项目的影响很大,所以nacos肯定是支持热更新的,我一共总结了4种方式,可以实现nacos的热更新,网上也找了很多,要么那些文章是从别的地方复制粘贴过来的不能用,要么就是pom依赖不对,总之搞不好,很头疼。所以我自己完整弄一遍(首先要安装好nacos服务端,这里就略过了)。
1、新建项目,pom引入
1、父模块引入pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>NacosConfig</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>NacosValueExample</module></modules><properties><spring-boot.version>2.2.8.RELEASE</spring-boot.version><spring-cloud.version>Hoxton.RELEASE</spring-cloud.version><spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version><nacos-spring-context.version>0.2.2-RC1</nacos-spring-context.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId><version>${nacos-spring-context.version}</version></dependency></dependencies></dependencyManagement></project>
2、子模块引入pom
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId></dependency></dependencies>
3、项目结构
2、使用@RefreshScope+@Value实现nacos热更新
1、在nacos配置中心新建config-teacher.yaml配置文件
内容如下:
teacher:name: 老李age: 45
配置编辑保存:
配置中心已有配置文件了:
2、项目中新建bootstrap.yml配置文件,将config-teacher.yaml配置进去
spring:application:name: test-configcloud:# nacos 配置nacos:config:file-extension: yamlrefresh-enabled: trueenabled: trueserver-addr: 127.0.0.1:8848group: DEFAULT_GROUP#添加配置时 Group 的值一定要和 spring.cloud.nacos.config.group 的配置值一致。#group: NACOS-DEMO## 可以配置多个 Data Id 同时配置时,他的优先级关系是 [n]其中 n 的值越大,优先级越高。extension-configs[0]:data-id: config-student.yamlgroup: DEFAULT_GROUPrefresh: trueextension-configs[1]:data-id: config-teacher.yamlgroup: DEFAULT_GROUPrefresh: truediscovery:server-addr: 127.0.0.1:8848
3、启动类
@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);}
}
4、读取配置文件代码
package com.nacos.example.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@RefreshScope
public class TestValue3 {@Value("${teacher.name}")private String name;@Value("${teacher.age}")private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
5、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;}}
6、访问接口效果:
我们修改nacos配置中心的config-teacher.yaml文件
然后重新访问刚刚的接口,服务不重启:
可以看到,修改后的配置已经读取到了,第一种方式成功!
3、使用@RefreshScope+@ConfigurationProperties实现nacos热更新
1、配置文件不变,读取配置文件的代码:
@Component
@RefreshScope
@ConfigurationProperties(prefix = "teacher")
public class TestRefreshScope1 {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller注入:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestRefreshScope1 testRefreshScope1;@GetMapping(value = "/getValue5")public Object getValue5() {// 因为RefreshScope使用的动态代理,这里直接返回testRefreshScope1会报错,所以重新new对象从testRefreshScope1获取值在返回// No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolverTestRefreshScope1 scope1 = new TestRefreshScope1();scope1.setName(testRefreshScope1.getName());scope1.setAge(testRefreshScope1.getAge());return scope1;}}
3、访问接口:
然后我们修改配置文件的内容:
然后重新访问刚刚的接口,服务不重启:
已经成功获取到修改后的值。
4、使用@NacosConfigurationProperties实现nacos热更新
1、读取配置代码
@Component
@NacosConfigurationProperties(dataId = "config-teacher.yaml", prefix = "teacher", autoRefreshed = true)
public class TestNacosConfiguration1 {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller注入:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestNacosConfiguration1 testNacosConfiguration1;@GetMapping(value = "/getValue7")public Object getValue7() {return testNacosConfiguration1;}}
3、配置文件不修改,访问接口:
修改nacos配置文件的内容:
重新请求接口:
同样获取到了最新的配置。
5、使用@NacosPropertySource和@ConfigurationProperties实现nacos热更新
1、读取配置的代码:
@Component
@NacosPropertySource(dataId = "config-teacher.yml", autoRefreshed = true)
@ConfigurationProperties(prefix = "teacher")
public class TestConfiguration3 {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller注入:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestConfiguration3 testConfiguration3;@GetMapping(value = "/getValue8")public Object getValue8() {return testConfiguration3;}}
3、访问接口:
然后我们修改nacos配置中的文件:
我们重新访问刚刚的接口,服务不重启:
同样,获取到了最新的nacos配置文件内容。以上就是nacos热更新的4种方式。
二、本地项目配置文件读取的几种方式
注意:需要将nacos相关的配置注释,不然会读取nacos配置中心的文件!这里演示的是读取本地项目的配置文件。
1、使用@Value读取本地配置文件内容
1、新建application.yml配置文件,添加内容:
student:name: 张三age: 20
读取配置文件代码:
@Component
public class TestValue1 {@Value("${student.name}")private String name;@Value("${student.age}")private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller接口:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestValue1 testValue1;@GetMapping(value = "/getValue1")public Object getValue1() {return testValue1;}
}
3、访问接口,获取到配置文件内容:
2、使用@Value+@PropertySource读取指定配置文件内容
1、新建config2.properties配置文件,添加内容:
teacher.name=李维
teacher.age=32
读取配置文件代码:
@Component
@PropertySource(value = "classpath:config2.properties", encoding = "utf-8")
public class TestValue2 {@Value("${teacher.name}")private String name;@Value("${teacher.age}")private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller接口:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestValue2 testValue2;@GetMapping(value = "/getValue2")public Object getValue2() {return testValue2;}
}
3、访问接口,获取到配置文件内容:
3、使用@ConfigurationProperties读取配置文件内容
1、application.yml文件不变:
student:name: 张三age: 20
读取配置文件代码:
@Component
@ConfigurationProperties(prefix = "student")
public class TestConfiguration1 {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller接口:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestConfiguration1 testConfiguration1;@GetMapping(value = "/getValue3")public Object getValue3() {return testConfiguration1;}
}
3、访问接口,获取到配置文件内容:
4、使用@ConfigurationProperties+@PropertySource读取指定配置文件内容
1、config2.properties配置文件不变:
teacher.name=李维
teacher.age=32
读取配置文件代码:
@Component
@PropertySource("classpath:config.properties")
@ConfigurationProperties(prefix = "teacher")
public class TestConfiguration2 {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
2、controller接口:
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowiredprivate TestConfiguration2 testConfiguration2;@GetMapping(value = "/getValue4")public Object getValue4() {return testConfiguration2;}
}
3、访问接口获取:
源代码gitee地址:https://gitee.com/wangyi0228/nacos-config.git