Disconf,百度的分布式配置管理平台
阅读原文时间:2021年04月23日阅读:1

disconf 可以为各种业务平台提供统一的配置管理服务。

  • 支持配置(配置项+配置文件)的分布式化管理

  • 配置发布统一化

  • 极简的使用方式(注解式编程 或 XML代码无代码侵入模式)

  • 低侵入性或无侵入性、强兼容性

  • 需要Spring编程环境

重要功能特点

  • 支持配置(配置项+配置文件)的分布式化管理

  • 配置发布统一化

    • 配置发布、更新统一化(云端存储、发布):配置存储在云端系统,用户统一在平台上进行发布、更新配置。

    • 配置更新自动化:用户在平台更新配置,使用该配置的系统会自动发现该情况,并应用新配置。特殊地,如果用户为此配置定义了回调函数类,则此函数类会被自动调用。

  • 配置异构系统管理

    • 异构包部署统一化:这里的异构系统是指一个系统部署多个实例时,由于配置不同,从而需要多个部署包(jar或war)的情况(下同)。使用 Disconf后,异构系统的部署只需要一个部署包,不同实例的配置会自动分配。特别地,在业界大量使用部署虚拟化(如JPAAS系统,SAE,BAE) 的情况下,同一个系统使用同一个部署包的情景会越来越多,Disconf可以很自然地与他天然契合。

    • 异构主备自动切换:如果一个异构系统存在主备机,主机发生挂机时,备机可以自动获取主机配置从而变成主机。

    • 异构主备机Context共享工具:异构系统下,主备机切换时可能需要共享Context。可以使用Context共享工具来共享主备的Context。

  • 极简的使用方式(注解式编程 或 XML代码无代码侵入模式):我们追求的是极简的、用户编程体验良好的编程方式。目前支持两种开发模式:基于XML配置或才基于注解,即可完成复杂的配置分布式化。

  • #### 需要Spring编程环境

注:配置项是指某个类里的某个Field字段。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.disconf.client.config.ConfigMgr;
import com.baidu.disconf.client.config.DisClientConfig;
import com.baidu.disconf.client.config.DisClientSysConfig;
import com.baidu.disconf.client.fetcher.FetcherFactory;
import com.baidu.disconf.client.fetcher.FetcherMgr;
import com.baidu.disconf.core.common.constants.DisConfigTypeEnum;
import com.baidu.disconf.core.common.path.DisconfWebPathMgr;


/**
 * 下载所有的disconf文件 * 

 *
 */
public class DisConfUtils {

    public static final Logger logger = LoggerFactory.getLogger(DisConfUtils.class);

    /**
     * 加载配置文件
     * 
     * @return
     * @throws IOException
     */
    private static Properties loadProperties() throws IOException {

        // 将我们的配置装载到properties中
        Properties pro = new Properties();
        // 获得disconf配置文件的路径
        String proPath = StringUtils.spliceUrl(ObjectUtils.getClassPath(), "disconf.properties");
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(proPath);
            pro.load(fin);
        } finally {
            if (!ObjectUtils.isNull(fin)) {
                try {
                    fin.close();
                } catch (IOException e) {
                    logger.error("close disconf.properties file Inputstream err", e);
                }
            }
        }
        return pro;
    }

    /**
     * 加载本地的配置文件
     * 
     * @return
     */
    private static String[] loadLocal() {
        String basePath = ObjectUtils.getClassPath();
        File baseFile = new File(basePath);
        String[] xmlPaths = null;

        String fileName = "";
        List<String> xmlList = new ArrayList<>();

        for (File sunFile : baseFile.listFiles()) {
            fileName = sunFile.getName();
            if (fileName.endsWith(".xml")) {
                xmlList.add("classpath:" + fileName);
            }
        }
        xmlPaths = new String[xmlList.size()];
        xmlList.toArray(xmlPaths);
        return xmlPaths;
    }

    /**
     * 加载远程的配置文件
     * 
     * @return
     * @throws Exception
     */
    private static String[] loadRemove() throws Exception {
        String[] xmlPaths = null;
        Properties disProperties = null;
        try {
            // 加载默认的配置文件
            disProperties = loadProperties();
        } catch (Exception e) {
            logger.error("load disconf properties file InputStream err", e);
            throw new Exception("load disconf properties file InputStream err", e);
        }

        try {
            // 初始化disconf的客户端
            ConfigMgr.init();
            // 获得disconf客户端的系统配置
            DisClientSysConfig sysConfig = DisClientSysConfig.getInstance();
            // 获得disconf客户端的配置
            DisClientConfig config = DisClientConfig.getInstance();
            // 获得下载disconf文件的对象
            FetcherMgr fetcherMgr = FetcherFactory.getFetcherMgr();

            // 获得下载文件的文件路径
            String downloadFiles = disProperties.getProperty("conf_server_download");
            if (!StringUtils.isEmpty(downloadFiles)) {
                List<String> xmlList = new ArrayList<>();
                String[] fileNames = downloadFiles.split(",");
                String url = "";
                // 循环下载配置文件
                for (String fileName : fileNames) {
                    url = DisconfWebPathMgr.getRemoteUrlParameter(sysConfig.CONF_SERVER_STORE_ACTION, config.APP,
                            config.VERSION, config.ENV, fileName, DisConfigTypeEnum.FILE);
                    fetcherMgr.downloadFileFromServer(url, fileName);
                    if (fileName.endsWith(".xml")) {
                        // 如果是xml后缀则需要添加上相对路径
                        xmlList.add("classpath:" + fileName);
                    }
                }
                xmlPaths = new String[xmlList.size()];
                xmlList.toArray(xmlPaths);
            }

        } catch (Exception e) {
            logger.error("connect disconf service err", e);
        }

        return xmlPaths;
    }

    /**
     * 连接disconf的服务器端,并下载服务器中的数据
     * 
     * @return
     * @throws Exception
     */
    public static String[] loadDisconf() throws Exception {
        // 首选加载远程的配置
        String[] locations = loadRemove();
        if (ObjectUtils.isNull(locations) || locations.length <= 0) {
            // 如果无法加载到远程的配置文件,则加载本地的配置文件
            locations = loadLocal();
        }
        return locations;
    }

}









import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;


/**
 * 重写spring的配置文件加载的监听器 实现disconf服务器上的文件下载并将下载后的spring配置文件装入spring的路径中
 * 
 *
 */
public class ContextListener extends ContextLoaderListener {

    public static final Logger logger = LoggerFactory.getLogger(ContextListener.class);

    public ContextListener() {
    }

    public ContextListener(WebApplicationContext context) {
        super(context);
    }

    /**
     * 重写父接口的创建ApplicationContext的类
     */
    protected WebApplicationContext createWebApplicationContext(ServletContext sc) {

        // 调用父类的方法
        WebApplicationContext cont = super.createWebApplicationContext(sc);
        if (null != cont) {
            // 如果获取成功,强制类型转换
            ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) cont;
            try {
                // 下载并获得所有的spring的配置文件的路径
                String[] locations = DisConfUtils.loadDisconf();
                // 设置spring的文件路径到Application的路径中
                wac.setConfigLocations(locations);
            } catch (Exception e) {
                logger.error("load spring config xml file error",e);
                throw new IllegalStateException("DownLoad spring xml error");
            }
        }
        return cont;
    }

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章