该技术类似Spring Cloud Config。
常用方案是引入配置服务器,架构如下:
引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
写配置(约定大于配置),要谨遵如下映射关系图:
创建新的bootstrap.yml配置文件:
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
application:
name: content-center
profiles:
active: dev
在NACOS操作面板配置:
启动内容中心:
@Value("${your.configuration}")
private String yourConfiguration;
@GetMapping("/test-config")
public String testConfiguration() {
return this.yourConfiguration;
}
修改配置后,应用可动态刷新配置,无需重启应用。
添加@RefreshScope即可:
package com.javaedge.contentcenter;
@Slf4j
@RefreshScope
public class TestController {
不要回滚到初始状态! Nacos 至今未解决。
既然迟迟不修复,我们只能尽量避免使用历史版本,而直接修改配置。
所指定的 > 通用的
如这段配置在用户/内容中心都有,可以将其交给nacos管理共享配置:
cloud:
nacos:
discovery:
# 指定nacos server的地址
server-addr: localhost:8848
NACOS配置共享的两种方案:
bootstrap-sharded-detaids.yml:
spring:
cloud:
nacos:
config:
# 共享配置的DataId,多个使用,分隔
# 越靠后,优先级越高; common2.yml > common1.yaml
# .yaml后缀不能少,只支持yaml/properties
shared-dataids: common1.yaml,common2.yaml
# 哪些共享配置支持动态刷新,多个使用,分隔
refreshable-dataids: common1.yaml
server-addr: 127.0.0.1:8848
file-extension: yaml
application:
name: content-center
profiles:
active: dev
bootstrap-ext-config.yml
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
ext-config:
# 需共享的DataId,yaml后缀不能少,只支持yaml/properties
# 越靠后,优先级越高 优先级common2.yaml > common1.yaml
- data-id: common1.yaml
# common1.yaml所在的group
group: DEFAULT_GROUP
# 是否允许刷新,默认false
refresh: true
- data-id: common2.yaml
group: DEFAULT_GROUP
refresh: true
application:
name: content-center
profiles:
active: dev
shared-dataids < ext-config < 自动
bootstrap.yml就是引导上下文的配置文件。对于应用,就是来连接Nacos以读取Nacos中的配置。
远程配置(Nacos) & 本地配置优先级。默认,远程配置优先级较高哦
如下的配置文件必须放在远程配置中才生效:
spring:
cloud:
config:
# 是否允许本地配置覆盖远程配置
allow-override: true
# 是否一切以本地配置为准,默认false
override-none: false
# 系统环境变量或系统属性才能覆盖远程配置文件的配置
# 本地配置文件中配置优先级低于远程配置,默认true
override-system-properties: true
其数据都是存在本地路径:
~/nacos/naming/public
$ naming cd public
$ public ll
total 24
-rw-r--r-- 1 sss staff 552B 12 15 22:27 DEFAULT_GROUP%40%40content-center
-rw-r--r-- 1 sss staff 534B 12 15 22:27 DEFAULT_GROUP%40%40gateway
-rw-r--r-- 1 sss staff 550B 12 15 22:27 DEFAULT_GROUP%40%40user-center
drwxr-xr-x 5 sss staff 160B 12 10 02:17 failover
$ public open .
$ public pwd
/Users/sss/nacos/naming/public
{
"metadata": {},
"dom": "DEFAULT_GROUP@@user-center",
"cacheMillis": 10000,
"useSpecifiedURL": false,
"hosts": [
{
"valid": true,
"marked": false,
"metadata": {
"preserved.register.source": "SPRING_CLOUD"
},
"instanceId": "192.168.1.48#8081#DEFAULT#DEFAULT_GROUP@@user-center",
"port": 8081,
"healthy": true,
"ip": "192.168.1.48",
"clusterName": "DEFAULT",
"weight": 1,
"ephemeral": true,
"serviceName": "DEFAULT_GROUP@@user-center",
"enabled": true
}
],
"name": "DEFAULT_GROUP@@user-center",
"checksum": "ab77b430a90bca45f50cf5a64fce71fb",
"lastRefTime": 1576420043289,
"env": "",
"clusters": ""
}
数据分为如下两部分
$NACOS_ HOME/data/derby-data
想查看其具体内容,须停止nacos,然后连接Derby这个Apache开发的内嵌数据库,通过IDEA的数据源连接
配置属性的快照
~/nacos/config