统一配置管理java_DuiC 统一配置管理 2

为什么设计 DuiC

在工作中我们时常要根据不同环境配置不同的参数,让项目的灵活性、可用性变得更高,那灵活的配置就变得必不可少,虽然目前已经存在很多配置管理解决方案,但是管理方式局限性大,适应范围窄。

DuiC 在设计初就考虑到配置的应用范围,将配置获取方式设计为 RESTful API 的方式,可支持任何服务器端,客户端以及 WEB 轻松获取配置。其次 DuiC 管理配置统一采用 YAML 的方式,并提供 WEB 编辑器及语法高亮,语法校验等功能,降低配置修改的错误率。

已实现的功能

检查配置状态

通过 key 获取某个配置

通过 name/profile 获取整体配置

多个 profile 配置深度合并

配置权限管理

分布式配置管理

容器部署

Java Client API

DuiC 提供 Java 客户端 API 帮助你快速获取配置,支持 Android/Java/JavaWeb/Spring 获取配置同时支持热加载配置。

Java/Android 使用

使用 API 提供的 ConfigUtils 工具包获取配置。

创建 Config 实例并将设置为 ConfigUtils 的默认配置,使用。

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);

Java Web 使用

使用 API 提供的 ConfigUtils 工具包获取配置。

在 web.xml 中配置监听器

duicConfigLocation

classpath:duic.properties

io.zhudy.duic.config.web.DuicConfigContextListener

在 duic.properties 文件中增加配置管理中心信息

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

在 Spring 中使用

使用 API 提供的 ConfigUtils 工具包获取配置

使用 @Value 的方式注入配置(推荐)

以上2种方式都支持热加载,推荐使用 @Value 的方式注入配置,这种方式对于你的代码没有任何的浸入性,支持 spring3.2 以上的版本使用。

使用注解的方式初始化

@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 的方式初始化

注入配置

@Component

public class Example {

@Value("${k1.string}")

private String k1;

}

Spring Boot 支持

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)

duic docker-compose

关于 DuiC 更加详细的描述,可以查看仓库中在 readme。

在线演示平台:https://duic.zhudy.io/index.html

e-mail: kevinz@weghst.com

password: 123456

大家可以使用在线演示平台,尝试使用其提供的 API 及 SDK。

友情提醒:服务器配置较差,访问速度可能不是很快,请体谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值