• 欢迎访问ByWei.Cn,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,加入百味博客 软件定制QQ群
  • 已升级为最新版主题,并将持续优化改造中,支持说说碎语功能,可像添加文章一样直接添加说说,博客主题升级啦
  • 感谢您百度求点赞啊!百度网址
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏百味博客吧
  • 博主热烈欢迎 软件定制开发 联系:http://www.bywei.cn

SpringBoot2.0整合Disconf配置中心实战

编程语言 百味博客 5年前 (2019-09-09) 2211次浏览 0个评论 扫描二维码

Disconf简介

配置中心使用开源 Distributed Configuration Management Platform(分布式配置管理平台) 统一管理配置
配置中心
包括 百度、滴滴打车、银联、网易、拉勾网 等知名互联网公司正在使用!

主要目标

  • 部署简单:同一个上线包,无须改动配置,即可在 多个环境中(dev/qa/pre/prod) 上线
  • 部署动态化:更改配置,无需重新打包或重启,即可 实时生效
  • 统一管理:提供web平台,统一管理 多个环境(dev/qa/pre/prod)、多个产品 的所有配置
  • 支持微服务架构

配置中心选型

  • 关于配置中心选型资料详见 《开源配置中心对比》

对接注意事项

  • httpclient 4.2.1以下的版本会报错, 推荐使用4.5.9 GA版本
  • 配置文件请保证配置正确性,如:配置项前后存在空格,配置格式,特殊字符,配置项编码等
  • spring5原client需重写ReloadingPropertyPlaceholderConfigurer类,super.parseStringValue 方法

SpringBoot对接

以下SpringBoot2 DisconfConfig 代码注入Bean配置对接方式可有效解决集成需要启动两次配置才生效的问题

1.  新建Disconf配置类DisconfConfig

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baidu.disconf.client.DisconfMgrBean;
import com.baidu.disconf.client.DisconfMgrBeanSecond;
import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean;
import com.baidu.disconf.client.config.inner.DisInnerConfigAnnotation;
import com.baidu.disconf.client.support.DisconfAutowareConfig;
import com.google.common.collect.Lists;

/**
 * Disconf配置中心
 * @author ByWei.Cn
 * @date 2019/08/28
 */
@Configuration
public class DisconfConfig {

    @DisInnerConfigAnnotation(name = "disconf.scanpackage")
    public String scanpackage;
    
    @DisInnerConfigAnnotation(name = "disconf.locations")
    public String locations;
    
    public DisconfConfig() {
        super();
        try {
            DisconfAutowareConfig.autowareConfig(this, "disconf.properties");
        } catch (Exception e) {
        }
    }

    @Bean(destroyMethod = "destroy")
    public DisconfMgrBean disconfMgrBean() {
        DisconfMgrBean mgr = new DisconfMgrBean();
        mgr.setScanPackage(scanpackage);
        return mgr;
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public DisconfMgrBeanSecond disconfMgrBeanSecond() {
        return new DisconfMgrBeanSecond();
    }
    
    @Bean
    public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() {
        ReloadablePropertiesFactoryBean reloadProp = new ReloadablePropertiesFactoryBean();
        String[] props = locations.split(",");
        List propsList = Lists.newArrayList();
        for (String prop : props) {
            propsList.add("classpath:"+prop);
        }
        reloadProp.setLocations(propsList);
        return reloadProp;
    }

    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean) {
        PropertyPlaceholderConfigurer prop = new PropertyPlaceholderConfigurer();
        prop.setIgnoreResourceNotFound(true);
        prop.setIgnoreUnresolvablePlaceholders(true);
        try {
            prop.setPropertiesArray(reloadablePropertiesFactoryBean.getObject());
        } catch (IOException e) {
             e.printStackTrace();
        }
        return prop;
    }
}

2.  在disconf.properties文件中新增自定义扩展配置

# scan package
disconf.scanpackage=cn.bywei.demo
# properties locations','split
disconf.locations=bywei-conf.properties

 

Disconf对接小技巧

  • 获取disconf配置项:DisClientConfig.getInstance().ENV
  • 多配置可通过自定义启动变量:
    xml获取- #{environment[‘project.channel’]} ;
    注解JAVA代码获取-DisconfConfig implements EnvironmentAware

百味博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot2.0整合Disconf配置中心实战
喜欢 (23)
[微信扫一扫]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址