在工做中咱们时常要根据不一样环境配置不一样的参数,让项目的灵活性、可用性变得更高,那灵活的配置就变得必不可少,虽然目前已经存在不少配置管理解决方案,可是管理方式局限性大,适应范围窄。html
DuiC 在设计初就考虑到配置的应用范围,将配置获取方式设计为 RESTful API 的方式,可支持任何服务器端,客户端以及 WEB 轻松获取配置。其次 DuiC 管理配置统一采用 YAML 的方式,并提供 WEB 编辑器及语法高亮,语法校验等功能,下降配置修改的错误率。java
DuiC 源码: https://github.com/zhudyos/duic
Swagger 文档: https://app.swaggerhub.com/ap...
Docker 镜像: https://hub.docker.com/r/zhud...
DuiC 提供 Java 客户端 API 帮助你快速获取配置,支持 Android/Java/JavaWeb/Spring 获取配置同时支持热加载配置。git
duic-java-client: https://github.com/zhudyos/du...
使用 API 提供的 ConfigUtils
工具包获取配置。
建立 Config 实例并将设置为 ConfigUtils 的默认配置,使用。github
Config config = new Config.Builder() .baseUri("https://duic.zhudy.io/api/v1") .name("hello") .profile("world") // .configToken("...") // .reloadPlot(new ReloadPlot(10, TimeUnit.SECONDS)) // 重载 // .failFast(true) // 快速失败 // .listener() // 配置加载监听 .build(); ConfigUtils.setDefaultConfig(config);
使用 API 提供的 ConfigUtils
工具包获取配置。
在 web.xml 中配置监听器web
<!-- 默认配置路径 classpath:duic.properties --> <context-param> <param-name>duicConfigLocation</param-name> <param-value>classpath:duic.properties</param-value> </context-param> <listener> <listener-class>io.zhudy.duic.config.web.DuicConfigContextListener</listener-class> </listener>
在 duic.properties 文件中增长配置管理中心信息spring
duic.base.uri=https://duic.zhudy.io/api/v1 duic.name=hello duic.profile=world duic.config.token= duic.reload.period=10 duic.reload.unit=SECONDS duic.fail.fast=false # 多个 DuicListener 采用英文逗号(,)分隔 duic.listeners=xx.MyDuicListener
ConfigUtils
工具包获取配置@Value
的方式注入配置(推荐)以上2种方式都支持热加载,推荐使用 @Value
的方式注入配置,这种方式对于你的代码没有任何的浸入性,支持 spring3.2 以上的版本使用。docker
使用注解的方式初始化bootstrap
@Bean public static DuicConfigBeanFactoryPostProcessor duicConfigBeanFactoryPostProcessor() { DuicConfigBeanFactoryPostProcessor processor = new DuicConfigBeanFactoryPostProcessor(); processor.setBaseUri("https://duic.zhudy.io/api/v1"); processor.setName("hello"); processor.setProfile("world"); return processor; }
使用 XML 的方式初始化api
<bean id="duicConfigBeanFactoryPostProcessor" class="io.zhudy.duic.config.spring.duicConfigBeanFactoryPostProcessor"> <property name="baseUri" value="https://duic.zhudy.io/api/v1"/> <property name="name" value="hello"/> <property name="profile" value="world"/> </bean>
注入配置服务器
@Component public class Example { @Value("${k1.string}") private String k1; }
DuiC 也提供了 duic-spring-cloud-config-client 来支持 spring-boot,使用 DuiC 管理配置能够彻底替代 spring-cloud-config。
duic-spring-cloud-config-client: https://github.com/zhudyos/du...
该工具包支持 @ConfigurationProperties
及 @Value
注入配置,同时也支持热加载。
注意若是你使用 @ConfigurationProperties
注入配置而且想要热加载配置须要配合使用 @RefreshScope
注解。
关于更多 @RefreshScope
的资料请查看 spring-cloud 官方文档:https://cloud.spring.io/sprin...
使用示例在 bootstrap.yml 文件中增长以下配置
spring: application: name: samples (1) duic: spring: cloud: config: watch: enabled: true (2) initial_delay: 10000 (3) fixed_delay: 10000 (4) uri: https://duic.zhudy.io/api/v1 (5) profile: first,second (6) # token: [TOKEN] (7)
https://github.com/zhudyos/du...
关于 DuiC 更加详细的描述,能够查看仓库中在 readme。
在线演示平台: https://duic.zhudy.io/index.html
e-mail: kevinz@weghst.com
password: 123456
你们可使用在线演示平台,尝试使用其提供的 API 及 SDK。
友情提醒:服务器配置较差,访问速度可能不是很快,请体谅。