disconf浅入浅出(二)disconf客户端disconf-client的使用

disconf-client使用步骤如下:

一、在maven配置文件中加入disconf-client包:

  1. <!-- disconf -->
  2. <dependency>
  3. <groupId>com.baidu.disconf</groupId>
  4. <artifactId>disconf-client</artifactId>
  5. <version>2.6.36</version>
  6. </dependency>

二、新建disconf资源配置文件disconf.xml

在项目resources文件夹下新建disconf.xml,并输入以下内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  9. <aop:aspectj-autoproxy proxy-target-class="true"/>
  10. <!-- 使用disconf必须添加以下配置 -->
  11. <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
  12. destroy-method="destroy">
  13. <!-- 找描的包名-->
  14. <property name="scanPackage" value="com.shixinke.farm"/>
  15. </bean>
  16. <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
  17. init-method="init" destroy-method="destroy">
  18. </bean>
  19. <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->
  20. <bean id="configproperties_disconf"
  21. class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
  22. <property name="locations">
  23. <!-- 要自动加载的配置文件列表 -->
  24. <list>
  25. <value>classpath:application.properties</value>
  26. <value>classpath:business.properties</value>
  27. </list>
  28. </property>
  29. </bean>
  30. <bean id="propertyConfigurer"
  31. class="com.baidu.disconf.client.addons.properties.
  32. ReloadingPropertyPlaceholderConfigurer">
  33. <property name="ignoreResourceNotFound" value="true"/>
  34. <property name="ignoreUnresolvablePlaceholders" value="true"/>
  35. <property name="propertiesArray">
  36. <list>
  37. <ref bean="configproperties_disconf"/>
  38. </list>
  39. </property>
  40. </bean>
  41. </beans>

三、新建disconf配置文件disconf.properties

在项目resources目录下新建disconf.properties,如输入以下内容:

  1. #配置服务器的 HOST,用逗号分隔,就是disconf-web访问地址
  2. disconf.conf_server_host=192.168.0.100:8081
  3. #APP名称,与disconf-web版要一致
  4. disconf.app=farm-web
  5. #版本号
  6. disconf.version=1.0.0
  7. #是否使用远程配置文件,true(默认)会从远程获取配置, false则直接获取本地配置 否 false
  8. disconf.enable.remote.conf=true
  9. #环境 否 默认为 DEFAULT_ENV。优先读取命令行参数,然后再读取此文件的值,最后才读取默认值
  10. disconf.env=development
  11. #忽略的分布式配置,用空格分隔 否 空
  12. disconf.ignore
  13. #调试模式。调试模式下,ZK超时或断开连接后不会重新连接(常用于client单步debug)。非调试模式下,ZK超时或断开连接会自动重新连接。 否 false
  14. disconf.debug=true
  15. #获取远程配置 重试次数,默认是3次 否 3
  16. disconf.conf_server_url_retry_times=3
  17. #获取远程配置 重试时休眠时间,默认是5秒 否 5
  18. disconf.conf_server_url_retry_sleep_seconds=5
  19. #用户定义的下载文件夹, 远程文件下载后会放在这里。注意,此文件夹必须有有权限,否则无法下载到这里 否 ./disconf/download
  20. disconf.user_define_download_dir=./config
  21. #下载的文件会被迁移到classpath根路径下,强烈建议将此选项置为 true(默认是true)
  22. disconf.enable_local_download_dir_in_class_path=true

四、在项目中引入disconf资源配置文件

这里以spring boot为例

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.support.SpringBootServletInitializer;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.ImportResource;
  8. @Configuration
  9. @EnableAutoConfiguration
  10. @ComponentScan(basePackages = {"com.shixinke.farm"})
  11. @ImportResource({"classpath:disconf.xml"})
  12. @SpringBootApplication
  13. /**
  14. * @author shixinke [ishixinke@qq.com]
  15. * @date 17-12-6.
  16. */
  17. public class Application extends SpringBootServletInitializer {
  18. public static void main(String[] args) throws InterruptedException {
  19. SpringApplication.run(Application.class, args);
  20. }
  21. }

五、在项目中使用配置

假如有两个配置文件:

  • application.properties
  1. app.env=development
  2. app.version=1.0.0.dev
  3. app.doamin=disconf.test.com
  • business.properties
  1. app.js.version=20171207
  2. app.css.version=20171207
1、通过注解的方式使用
  1. package com.shixinke.farm.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. @RequestMapping("/config")
  10. @Controller
  11. /**
  12. * @author shixinke [ishixinke@qq.com]
  13. * @date 17-12-6.
  14. */
  15. public class MainController {
  16. @Value("${app.env}")
  17. private String env;
  18. @GetMapping("/list")
  19. @ResponseBody
  20. public Map<String, String> configList() {
  21. Map<String, String> result = new HashMap<>(10);
  22. result.put("test", "test");
  23. result.put("env", env);
  24. return result;
  25. }
  26. }

注:根据以上的配置,我们在disconf-web中改变配置的值,我们的项目中会自动下载对应的配置文件,但是web项目如果不重启,那么配置值还是不会改变的,因为这些配置值已经放到内存中,不再从文件中读取。

2、通过配置bean文件读取reload后配置值
(1)增加一个读取配置文件的bean文件
  1. package com.shixinke.farm.configuration;
  2. import com.baidu.disconf.client.common.annotations.DisconfFile;
  3. import com.baidu.disconf.client.common.annotations.DisconfFileItem;
  4. import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
  5. import com.baidu.disconf.client.common.update.IDisconfUpdate;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Scope;
  8. @Configuration
  9. @Scope("singleton")
  10. @DisconfFile(filename = "business.properties")
  11. @DisconfUpdateService(classes = {BusinessConfiguration.class})
  12. /**
  13. * @author shixinke [ishixinke@qq.com]
  14. * @date 17-12-6.
  15. */
  16. public class BusinessConfiguration implements IDisconfUpdate {
  17. private String appJsVersion;
  18. private String appCssVersion;
  19. @DisconfFileItem(name = "app.js.version", associateField = "appJsVersion")
  20. public String getAppJsVersion() {
  21. return appJsVersion;
  22. }
  23. public void setAppJsVersion(String appJsVersion) {
  24. this.appJsVersion = appJsVersion;
  25. }
  26. @DisconfFileItem(name = "app.css.version", associateField = "appCssVersion")
  27. public String getAppCssVersion() {
  28. return appCssVersion;
  29. }
  30. public void setAppCssVersion(String appCssVersion) {
  31. this.appCssVersion = appCssVersion;
  32. }
  33. /**
  34. * 这里为了简单,在此不添加任何第三方日志包(生产环境可使用其他log包)
  35. * @throws Exception
  36. */
  37. @Override
  38. public void reload() throws Exception {
  39. System.out.println("app.js.version:"+appJsVersion);
  40. System.out.println("app.css.version:"+appCssVersion);
  41. }
  42. }
(2)项目中通过读取配置的bean读取相应的配置
  1. package com.shixinke.farm.controller;
  2. import com.shixinke.farm.configuration.BusinessConfiguration;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. @RequestMapping("/config")
  11. @Controller
  12. /**
  13. * @author shixinke [ishixinke@qq.com]
  14. * @date 17-12-6.
  15. */
  16. public class MainController {
  17. @Autowired
  18. private BusinessConfiguration businessConfiguration;
  19. @GetMapping("/business/list")
  20. @ResponseBody
  21. public Map<String, String> businessList() {
  22. Map<String, String> result = new HashMap<>(10);
  23. result.put("appJsVersion", businessConfiguration.getAppJsVersion());
  24. result.put("appCssVersion", businessConfiguration.getAppCssVersion());
  25. return result;
  26. }
  27. }

根据以上配置,我们在disconf-web端修改配置项,我们在项目中就可以不重启web项目就可以读取最新的配置项的值
完整演示代码请参照:https://github.com/shixinke/blog-source-code/tree/master/farm-web