本文主要介绍自己将nacos作为spring boot的配置中心的实践过程,希望对有需求的小伙伴提供一些帮助。
通过nacos实现配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。
详细的说明可以查看官方文档:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
在linux系统中可以通过以下命令安装 docker-compse:
yum install -y docker-compose
使用如下命令创建部署文件nacos-standalone-mysql-8.yaml
:
vim nacos-standalone-mysql-8.yaml
文件内容如下:
version: "2"
services:
nacos:
image: nacos/nacos-server:2.0.0
container_name: nacos-standalone-mysql
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
volumes:
- ./standalone-logs/:/home/nacos/logs
ports:
- "8848:8848"
- "9555:9555"
depends_on:
- mysql
restart: always
mysql:
container_name: mysql
image: nacos/nacos-mysql:8.0.16
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nacos_devtest
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos
volumes:
- ./mysql:/var/lib/mysql
ports:
- "3306:3306"
使用如下命令启动nacos:
docker-compose -f nacos-standalone-mysql-8.yaml up -d
使用下面的命令可以查看启动日志:
docker-compose -f nacos-standalone-mysql-8.yaml logs -f
启动成功后访问:http://localhost:8848/nacos
登录用户名:nacos
登录密码:nacos
可以去maven中央仓库查找nacos-config-spring-boot-starter
最新依赖,这里使用的最新依赖。
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>0.2.7version>
dependency>
注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
将spring boot项目中原有的配置放到nacos中,如下图所示:
在 application.yaml
文件中配置 Nacos server 的地址:
nacos:
config:
server-addr: 127.0.0.1:8848
使用 @NacosPropertySource
加载nacos中dataId = "spring-boot-demo.yaml"
的配置文件,并开启自动更新:
@SpringBootApplication
@NacosPropertySource(dataId = "spring-boot-demo.yaml", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
通过 Nacos 的 @NacosValue
注解设置属性值。
@RestController
public class ConfigController {
@NacosValue(value = "${name:test}", autoRefreshed = true)
private String name;
@GetMapping("/get")
public String get() {
return name;
}
}
启动 NacosConfigApplication
,调用 http://localhost:8777/spring-boot-demo/get
,返回内容是 祢豆子。
使用spring boot整合nacos的配置管理还是挺简单的,总体来说就只有三步:
1、引入 nacos-config-spring-boot-starter 依赖
2、在配置文件中设置nacos的地址
3、在启动类上使用注解@NacosPropertySource 指定nacos中配置的 Data ID
1、在application.yaml
配置文件中指定nacos中配置的 Data ID 不会生效,需要通过注解@NacosPropertySource
指定才能生效。
2、 nacos-config-spring-boot-starter:0.2.7
依赖最高支持 spring boot 2.3.x
,如果使用spring boot 2.4.x
将会报如下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nacosConfigurationPropertiesBinder': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:296) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1206) ~[spring-beans-5.3.2.jar:5.3.2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:225) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:311) ~[spring-beans-5.3.2.jar:5.3.2]
... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
at com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder.(NacosBootConfigurationPropertiesBinder.java:51) ~[nacos-config-spring-boot-autoconfigure-0.2.7.jar:0.2.7]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_181]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_181]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:212) ~[spring-beans-5.3.2.jar:5.3.2]
... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181]
... 28 common frames omitted