/duic-java-client

duic 配置中心 Java/Android 客户端工具包

Primary LanguageJavaApache License 2.0Apache-2.0

duic 配置中心 Java/Android 工具包。

特性

  1. 支持 Java、Servlet、Spring、Android 等应用配置管理。

  2. 支持配置热加载。

  3. 支持配置无延迟更新。

  4. 支持 @Value 注入配置。

  5. 提供 ConfigUtils API 静态方法获取配置。

使用

maven
<dependency>
  <groupId>io.zhudy.duic</groupId>
  <artifactId>duic-java-client</artifactId>
  <version>1.4.0</version>
</dependency>
gradle
compile "io.zhudy.duic:duic-java-client:1.4.0"
Important

duic-java-client 采用的 json 解析库为 jackson/gson 在使用的项目中至少要依赖下面其中一个 json

testCompile "com.fasterxml.jackson.core:jackson-databind:x.x.x"
// testCompile "com.google.code.gson:gson:x.x.x"

ConfigUtils API

  1. ConfigUtils.containsKey(key) 判断配置是否存在。

  2. ConfigUtils.getBoolean(key) 获取一个 boolean 配置。

  3. ConfigUtils.getBoolean(key, defaultValue) 获取一个 boolean 配置不存在则返回默认值。

  4. ConfigUtils.getInt(key) 获取一个 int 配置。

  5. ConfigUtils.getInt(key, defaultValue) 获取一个 int 配置不存在则返回默认值。

  6. ConfigUtils.getLong(key) 获取一个 long 配置。

  7. ConfigUtils.getLong(key, defaultValue) 获取一个 long 配置不存在则返回默认值。

  8. ConfigUtils.getFloat(key) 获取一个 float 配置。

  9. ConfigUtils.getFloat(key, defaultValue) 获取一个 float 配置不存在则返回默认值。

  10. ConfigUtils.getDouble(key) 获取一个 double 配置。

  11. ConfigUtils.getDouble(key, defaultValue) 获取一个 double 配置不存在则返回默认值。

  12. ConfigUtils.getString(key) 获取一个 String 配置。

  13. ConfigUtils.getString(key, defaultValue) 获取一个 String 配置不存在则返回默认值。

  14. ConfigUtils.get(key) 获取一个 Object 配置。

  15. ConfigUtils.get(key, defaultValue) 获取一个 Object 配置不存在则返回默认值。

  16. ConfigUtils.getOrNull(key) 获取一个 Object 配置不存在则返回 null

DuicClientUtils API

  1. DuicClientUtils.getState(url) 获取服务器配置状态。

  2. DuicClientUtils.getState(url, configToken) 获取服务器配置状态。

  3. DuicClientUtils.getProperties(url) 获取服务器配置。

  4. DuicClientUtils.getProperties(url, configToken) 获取服务器配置。

Java/Android 使用

创建 Config 实例并将设置为 ConfigUtils 的默认配置

Config config = new Config.Builder()
        .baseUri("https://duic.zhudy.io/api/v1")
        .name("hello")
        .profile("world")
        // 访问令牌(可选)
        .configToken("token1,token2")
        // 监控服务器配置变化,当配置发生改变时自动重载(可选)
        .enableWatch()
        // 启用快速失败策略,当获取配置失败时,直接抛出异常(可选)
        .enableFailFast()
        // 配置加载监听(可选)
        .listener()
        .build();
ConfigUtils.setDefaultConfig(config);

Java Web Servlet 使用

web.xml
<!-- 默认配置路径 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
duic.base.uri=https://duic.zhudy.io/api/v1
duic.name=hello
duic.profile=world
duic.config.token=
duic.watch.enabled=true
duic.fail.fast=false
# 多个 DuicListener 采用英文逗号(,)分隔
duic.listeners=xx.MyDuicListener

Spring 使用

  1. 声明 DuicConfigBeanFactoryPostProcessor 实例

    @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 中声明

    <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>
  2. 使用 @Value 注入配置属性

@Component
public class Example {

  @Value("${k1.string}")
  private String k1;
}
Tip

@Value 注入的属性可热更新,当配置变化时会重新在实例中注入新的配置属性。