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