SpringBoot加载共用配置文件

SpringBoot加载共用配置文件

问题:多个SpringBoot项目,项目间通过Hessian调用,每个项目要配置一些一样的配置文件,如数据库连接,Redis相关配置,MQ相关配置。如果一旦需要修改,那么每个配置文件都要修改

解决办法:把共用文件单独放在数据库或者放到一个独立服务中,在项目启动时动态加载配置
具体方案:仿照ConfigFileApplicationListener写一个配置加载类,并通过spring.factories指定加载

ConfigFileApplicationListener
public class ConfigFileApplicationListener
		implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
		......
		}

可见ConfigFileApplicationListener实现了EnvironmentPostProcessor,Ordered,SmartApplicationListener三个接口。自定义的配置加载类只需要继承EnvironmentPostProcessor,Ordered这两个就可以了

自定义的配置加载类
package com.wangyanan.datastructure.common;

import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

/**
 * 公共配置文件
 *
 * @author 王亚楠
 * @date 2018年04月23日 18:14
 **/
public class MyConfig implements EnvironmentPostProcessor, Ordered {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        JSONObject jsonObject = new JSONObject();
        //此处可以http方式 到配置服务器拉取一堆公共配置+本项目个性配置的json串,拼到Properties里
        try {
            URL url = new URL("http://wang_ya_nan.gitee.io/common_configuration_file/spring.application");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod("GET");
            //连接
            connection.connect();
            //得到响应码
            int responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream inputStream = connection.getInputStream();
                //将响应流转换成字符串
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                String str;
                while ((str = bufferedReader.readLine()) != null) {
                    stringBuilder.append(str);
                }
                String json = stringBuilder.toString();
                jsonObject = new JSONObject(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Properties properties = new Properties();
        Map<String, Object> map = jsonObject.toMap();
        map.forEach((key, value) -> properties.setProperty(key, (String) value));
        MutablePropertySources propertySources = environment.getPropertySources();
        //addLast 结合下面的 getOrder() 保证顺序 读者也可以试试其他姿势的加载顺序
        propertySources.addLast(new PropertiesPropertySource("myConfig", properties));
    }

    /**
     * +1 保证application.propertie里的内容能覆盖掉本配置文件中默认的
     **/
    @Override
    public int getOrder() {
        return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
    }
}

这里是从自己的码云上拉的配置,这里可以自定义实现

如何为每个服务加上配置
  • 使用SpringFactoriesLoaderspring.factories
  • SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。

我们在resources下新建META-INF/spring.factories,写入:

org.springframework.boot.env.EnvironmentPostProcessor=com.wangyanan.datastructure.common.MyConfig

作用:指定EnvironmentPostProcessor的实现为我们自定义的类,这时候是对获取项目配置的一种扩展(当前是从远程获取),Ordered接口的作用主要是解决默认配置需要覆盖我们共用配置的问题

具体操作步骤
  1. 在一个新项目(配置导入项目)中,写好自定义的配置类,加上相关配置文件
  2. 在需要环境配置的项目中,引入配置倒入项目

我的个人博客有空来坐坐
https://www.wangyanan.online

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值