disconf-client使用步骤如下:
一、在maven配置文件中加入disconf-client包:
<!-- disconf -->
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>
二、新建disconf资源配置文件disconf.xml
在项目resources文件夹下新建disconf.xml,并输入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<!-- 找描的包名-->
<property name="scanPackage" value="com.shixinke.farm"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
<!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->
<bean id="configproperties_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<!-- 要自动加载的配置文件列表 -->
<list>
<value>classpath:application.properties</value>
<value>classpath:business.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="com.baidu.disconf.client.addons.properties.
ReloadingPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<list>
<ref bean="configproperties_disconf"/>
</list>
</property>
</bean>
</beans>
三、新建disconf配置文件disconf.properties
在项目resources目录下新建disconf.properties,如输入以下内容:
#配置服务器的 HOST,用逗号分隔,就是disconf-web访问地址
disconf.conf_server_host=192.168.0.100:8081
#APP名称,与disconf-web版要一致
disconf.app=farm-web
#版本号
disconf.version=1.0.0
#是否使用远程配置文件,true(默认)会从远程获取配置, false则直接获取本地配置 否 false
disconf.enable.remote.conf=true
#环境 否 默认为 DEFAULT_ENV。优先读取命令行参数,然后再读取此文件的值,最后才读取默认值
disconf.env=development
#忽略的分布式配置,用空格分隔 否 空
disconf.ignore
#调试模式。调试模式下,ZK超时或断开连接后不会重新连接(常用于client单步debug)。非调试模式下,ZK超时或断开连接会自动重新连接。 否 false
disconf.debug=true
#获取远程配置 重试次数,默认是3次 否 3
disconf.conf_server_url_retry_times=3
#获取远程配置 重试时休眠时间,默认是5秒 否 5
disconf.conf_server_url_retry_sleep_seconds=5
#用户定义的下载文件夹, 远程文件下载后会放在这里。注意,此文件夹必须有有权限,否则无法下载到这里 否 ./disconf/download
disconf.user_define_download_dir=./config
#下载的文件会被迁移到classpath根路径下,强烈建议将此选项置为 true(默认是true)
disconf.enable_local_download_dir_in_class_path=true
四、在项目中引入disconf资源配置文件
这里以spring boot为例
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.shixinke.farm"})
@ImportResource({"classpath:disconf.xml"})
@SpringBootApplication
/**
* @author shixinke [ishixinke@qq.com]
* @date 17-12-6.
*/
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
}
}
五、在项目中使用配置
假如有两个配置文件:
- application.properties
app.env=development
app.version=1.0.0.dev
app.doamin=disconf.test.com
- business.properties
app.js.version=20171207
app.css.version=20171207
1、通过注解的方式使用
package com.shixinke.farm.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@RequestMapping("/config")
@Controller
/**
* @author shixinke [ishixinke@qq.com]
* @date 17-12-6.
*/
public class MainController {
@Value("${app.env}")
private String env;
@GetMapping("/list")
@ResponseBody
public Map<String, String> configList() {
Map<String, String> result = new HashMap<>(10);
result.put("test", "test");
result.put("env", env);
return result;
}
}
注:根据以上的配置,我们在disconf-web中改变配置的值,我们的项目中会自动下载对应的配置文件,但是web项目如果不重启,那么配置值还是不会改变的,因为这些配置值已经放到内存中,不再从文件中读取。
2、通过配置bean文件读取reload后配置值
(1)增加一个读取配置文件的bean文件
package com.shixinke.farm.configuration;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@Scope("singleton")
@DisconfFile(filename = "business.properties")
@DisconfUpdateService(classes = {BusinessConfiguration.class})
/**
* @author shixinke [ishixinke@qq.com]
* @date 17-12-6.
*/
public class BusinessConfiguration implements IDisconfUpdate {
private String appJsVersion;
private String appCssVersion;
@DisconfFileItem(name = "app.js.version", associateField = "appJsVersion")
public String getAppJsVersion() {
return appJsVersion;
}
public void setAppJsVersion(String appJsVersion) {
this.appJsVersion = appJsVersion;
}
@DisconfFileItem(name = "app.css.version", associateField = "appCssVersion")
public String getAppCssVersion() {
return appCssVersion;
}
public void setAppCssVersion(String appCssVersion) {
this.appCssVersion = appCssVersion;
}
/**
* 这里为了简单,在此不添加任何第三方日志包(生产环境可使用其他log包)
* @throws Exception
*/
@Override
public void reload() throws Exception {
System.out.println("app.js.version:"+appJsVersion);
System.out.println("app.css.version:"+appCssVersion);
}
}
(2)项目中通过读取配置的bean读取相应的配置
package com.shixinke.farm.controller;
import com.shixinke.farm.configuration.BusinessConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@RequestMapping("/config")
@Controller
/**
* @author shixinke [ishixinke@qq.com]
* @date 17-12-6.
*/
public class MainController {
@Autowired
private BusinessConfiguration businessConfiguration;
@GetMapping("/business/list")
@ResponseBody
public Map<String, String> businessList() {
Map<String, String> result = new HashMap<>(10);
result.put("appJsVersion", businessConfiguration.getAppJsVersion());
result.put("appCssVersion", businessConfiguration.getAppCssVersion());
return result;
}
}
根据以上配置,我们在disconf-web端修改配置项,我们在项目中就可以不重启web项目就可以读取最新的配置项的值
完整演示代码请参照:https://github.com/shixinke/blog-source-code/tree/master/farm-web