最近使用到了nacos,apollo作为配置中心,记录一下。前提是nacos环境已经搭建好了的,我们是先自己搭建测试,然后让运维来搭建,所以直接用就行了。
0.需要通过搭建好的nacos控制台建一些配置,让下面用。这里dataId理解为文件名,groupId默认一般写项目名。
1.首先引入jar包
com.alibaba.boot
nacos-config-spring-boot-starter
0.2.5
2.代码配置方面,可以配置到application中;也可以写到本地,在启动jar包的时候通过环境变量配置进去。我们选择了后者。在本地新建一个nacos.properties文件,内容如下
nacos.config.server-addr=xxx #命名空间,stable和sit参数不同 nacos.config.namespace=xxx nacos.config.type=properties
有这3个就够了,addr是运维给的nacos地址端口号,配置文件的地址;namespace可以用来区分环境或者不同项目,在页面上可以找到;peoperties是下载下来的文件格式,要和服务器上的对应。
3.本地文件要在启动时候读取到代码中,要写一个类。这个类是自定义的,要实现EnvironmentPostProcessor, Ordered接口,这是springboot专门用于扩展读取本地配置文件的。
import com.alibaba.nacos.common.utils.ResourceUtils;
import com.chuanglan.flash.core.common.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
@Slf4j
public class NacosEnvPostProcessor implements EnvironmentPostProcessor, Ordered {
private int order = Ordered.LOWEST_PRECEDENCE;
private static final String nacos_namespace = "nacos.config.namespace";
private static final String nacos_server_addr = "nacos.config.server-addr";
private static final String nacos_password = "nacos.config.password";
private static final String nacos_username = "nacos.config.username";
@Override
public int getOrder() {
return 0;
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String filePath = environment.getProperty("nacos.conf");
if (StringUtils.isEmpty(filePath)) {
log.warn("n [----未配置Nacos配置路径-----] n");
return;
}
//读取文件路径中的配置 放到 Environment中去
Properties nacosProperties = null;
FileInputStream inputStream = null;
try {
File configFile = new File(filePath);
if (configFile.exists()) {
inputStream = new FileInputStream(configFile);
nacosProperties = new Properties();
nacosProperties.load(inputStream);
} else {
log.error("n [----获取Nacos配置信息地址有误,filePath:{},err:{}---]n", filePath, "配置文件不存在");
System.exit(0);
}
} catch (IOException e1) {
e1.printStackTrace();
log.error("n [----获取Nacos配置信息地址有误,filePath:{},err:{}---]n", filePath, e1.getStackTrace());
System.exit(0);
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Properties properties = new Properties();
if (!StringUtils.isEmpty(nacosProperties.getProperty(nacos_namespace))) {
properties.put(nacos_namespace, nacosProperties.getProperty(nacos_namespace));
}
if (!StringUtils.isEmpty(nacosProperties.getProperty(nacos_server_addr))) {
properties.put(nacos_server_addr, nacosProperties.getProperty(nacos_server_addr));
}
if (!StringUtils.isEmpty(nacosProperties.getProperty(nacos_password))) {
properties.put(nacos_password, nacosProperties.getProperty(nacos_password));
}
if (!StringUtils.isEmpty(nacosProperties.getProperty(nacos_username))) {
properties.put(nacos_username, nacosProperties.getProperty(nacos_username));
}
environment.getPropertySources().addLast(new PropertiesPropertySource("nacosfile", properties));
log.info("n-- 当前 nacos.config.namespace = " + nacosProperties.getProperty("nacos.config.namespace") + "n");
}
}
然后,还在resources 下 meta-INF文件夹下加上 spring.factories文件
org.springframework.boot.env.EnvironmentPostProcessor=xxx.NacosEnvPostProcessor
这样就是实现了动态配置
4.然后就是具体位置读取配置文件
@NacosPropertySource(dataId = "dataID", groupId = "groupID", autoRefreshed = true, type = ConfigType.PROPERTIES)
public class xxx{
@NacosValue("${url253}")
private String url253;
}
5.写个controller输出一下就可以验证了。



