springboot统一配置文件管理nacos配置信息

如果微服务很多的话,把连接nacos的配置信息写在配置文件里面,当nacos的连接信息改变的时候,要修改每个微服务的配置信息,很麻烦。用一个统一的配置文件来管理nacos的连接信息,微服务都读取这个配置文件里面的关于nacos的连接信息。

1弄一个配置文件 configCenter.yml,内容如下

activity:
  nacos-addr: 127.0.0.1:8849
  namespace: 99bc9b8f-e2be-481a-a8bc-3f7f1eb0c6fa
  group: SPRING_CLOUD
  username: spring_cloud
  password: spring_cloud

2设置启动参数(配置文件的地址)
-DconfigurationCenter=E:\code\xuexi\configCenter.yml

3自定义环境后置处理器 NacosEnvironmentPostProcessor 将配置文件configCenter.yml加载到应用环境中
package com.example.demo.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * 自定义环境后置处理器
 * 加载nacos通用配置
 */
@Component
public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String configurationCenter = System.getProperty("configurationCenter");
        Resource resource = new FileSystemResource(configurationCenter);
        environment.getPropertySources().addLast(loadProfiles(resource));
    }

    /**
     * 加载yml配置文件
     *
     * @param resource 资源对象
     * @return 属性信息
     */
    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) {
            throw new IllegalArgumentException("source: " + resource + " not exist");
        } else {
            String filename = resource.getFilename();
            if (filename != null) {
                if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
                    return loadYaml(resource);
                } else if (filename.endsWith(".properties")) {
                    return loadProperty(resource);
                }
            }
            throw new IllegalArgumentException("source: " + resource + " type no support");
        }
    }

    /**
     * 加载properties格式的配置文件
     *
     * @param resource 资源对象
     * @return 属性信息
     */
    private PropertySource<?> loadProperty(Resource resource) {
        try {
            //从输入流中加载一个Properties对象
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            return new PropertiesPropertySource(resource.getFilename(), properties);
        } catch (Exception ex) {
            throw new IllegalStateException("load configurationCenter file failed: " + resource, ex);
        }
    }

    /**
     * 加载yml格式的配置文件
     *
     * @param resource 资源对象
     * @return 属性信息
     */
    private PropertySource<?> loadYaml(Resource resource) {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource);
            Properties properties = factory.getObject();
            return new PropertiesPropertySource(resource.getFilename(), properties);
        } catch (Exception ex) {
            throw new IllegalStateException("load configurationCenter file failed: " + resource, ex);
        }
    }
}
4 创建一个名为META-INF/spring.factories的资源文件,并在其中加入以下内容:这样,在Spring Boot启动时,你的自定义配置文件就会被自动加载并合并到应用环境中。
org.springframework.boot.env.EnvironmentPostProcessor=\
  com.example.demo.config.NacosEnvironmentPostProcessor

5 boostrap.xml配置文件的内容
spring:
  cloud:
    nacos:
      config:
        server-addr: ${activity.nacos-addr}
        namespace: ${activity.namespace}
        username: ${activity.username}
        password: ${activity.password}
        group: ${activity.group}
        prefix: my_config
        file-extension: yaml
        enabled: true
      discovery:
        server-addr: ${activity.nacos-addr}
        namespace: ${activity.namespace}
        username: ${activity.username}
        password: ${activity.password}
        group: ${activity.group}
        register-enabled: true
6启动jar包的命令 增加  -server -DconfigurationCenter=configCenter.yml

其他说明

当打包出现如下错误时

解决方式1,可以把test相关的模块删了

方式2

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值