1.部署
从docker中央仓库pull nacos2.x最新镜像,打上tag上传到公司的私有仓库,这部分忽略。
挂载配置文件:
这里没有修改dockerfile,部署后发现主要配置文件在这个文件夹下面:
接着就可以设置挂载和configmap(需要把原来的环境参数先退换掉)。
这里要注意一下,不推荐使用设置env部署,因为环境变量太多了。密钥等配置按需修改,我这里是内网访问,暂时没有修改
#设置数据库模式
spring.sql.init.platform=mysql
#设置数据库配置信息
db.url.0=xxx
db.user.0=xxx
db.password.0=xxx
nacos.core.auth.server.identity.key=nacos
nacos.core.auth.server.identity.value=nacos
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.plugin.nacos.token.expire.seconds=2592000
# spring
server.servlet.contextPath=/nacos
server.contextPath=/nacos
server.port=8848
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.accesslog.enabled=false
server.error.include-message=ALWAYS
# default current work dir
server.tomcat.basedir=file:.
#*************** Config Module Related Configurations ***************#
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=1
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=nacos
### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
### The default token:
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=false
nacos.core.auth.enable.userAgentAuthWhite=false
## spring security config
### turn off security
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/
v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
2.增加配置
我这边只使用了配置中心,这里选择使用了namespace来区分不同的环境:
引入maven配置:
这个jar包貌似是支持jdk8的最后一个版本了
<!--nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
编写启动配置文件(bootstrap.yml)
spring:
application:
name: boot-app
cloud:
nacos:
config:
server-addr: ${NACOS_SERVER}:8848
#server-addr: localhost:8848
file-extension: properties
username: ${NACOS_USER}
#username: nacos
password: ${NACOS_PASSWORD}
#password: nacos
namespace: ${NACOS_NAMESPACE}
group: DEFAULT_GROUP
这边使用了环境参数暴露配置nacos信息,因为这个配置文件配置比较少,免得挂载配置文件了
3.增加原有组件配置的刷新
使用的是1.4.x的客户端
以下是初始化nacos客户端configService和配置热更新的监听器部分代码:
private Properties buildProperties() {
Properties properties = new Properties();
//这里全部从environment获取,可以保证动态刷新
//nacos 地址
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddr);
//用户名
properties.setProperty(PropertyKeyConst.USERNAME, environment.getProperty("spring.cloud.nacos.config.username"));
//密码
properties.setProperty(PropertyKeyConst.PASSWORD, environment.getProperty("spring.cloud.nacos.config.password"));
//namespace
properties.setProperty(PropertyKeyConst.NAMESPACE, environment.getProperty("spring.cloud.nacos.config.namespace"));
return properties;
}
...
try {
// Add config listener.
this.configService = NacosFactory.createConfigService(buildProperties());
configService.addListener(nacosDataId, nacosGroup, configListener);
} catch (Exception e) {
log.error("nacos config update produce error: ", e);
}
以下是初始化获取配置部分代码:
if(this.configService == null){
return;
}
String configs = null;
try {
//初始化时从nacos 客户端获取配置
configs = this.configService.getConfig(nacosDataId, nacosGroup, DEFAULT_TIMEOUT);
} catch (NacosException e) {
e.printStackTrace();
}
if (StringUtils.isBlank(configs)) {
log.warn("initial config is null");
return;
}
//这边比较奇怪,nacos客户端返回的是string字符串,需要转换为Properties
Properties properties = loadByStringConfig(configs);
configs = properties.getProperty(dataId);
log.debug("poll config from nacos: " + configs);
if(StringUtils.isBlank(configs)){
return;
}
//更新本地配置
updateResource(configs);
以下是监听器实现部分代码,这里使用了单线程去获取配置(保证了并发安全),不适用于配置比较多的情况,因为当前nacos 版本(2.x)还是使用了全量的配置的同步,这点比较坑:
private static class LogNacosListener implements Listener {
private final ExecutorService executorService;
private LogConfigerLisenter listener;
private final String dataId;
LogNacosListener(ExecutorService pool, String dataId){
this.executorService = pool;
this.dataId = dataId;
}
void setListener(LogConfigerLisenter listener){
this.listener = listener;
}
LogConfigerLisenter getListener(){
return this.listener;
}
//更新配置的线程池
@Override
public Executor getExecutor() {
return this.executorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
if (StringUtils.isBlank(configInfo)) {
log.warn("update config is null");
return;
}
Properties properties = loadByStringConfig(configInfo);
configInfo = properties.getProperty(dataId);
log.info("update config from nacos: " + configInfo);
if(StringUtils.isBlank(configInfo)){
return;
}
if(this.listener != null){
//更新配置的缓存
listener.configUpdate(configInfo);
}
}
}