Disconf使用簡單Demo
創建配置文件
在敲Demo之前,需要在Disconf上創建自己的APP,然後在APP的某個環境下創建配置文件,如下面截圖中的流程,這裏就簡單創建了一個redis.properties,內容是redis的IP和端口號,Key值分別是redis.host和redis.port。
創建Demo
1、使用工具
Eclipse Kepler Release
Maven
2、創建項目
創建一個Maven Project,修改pom.xml文件,加載所需要的jar包。
[html] view plain copy
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <guava.version>16.0.1</guava.version>
- <java.version>1.7</java.version>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.2.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>com.baidu.disconf</groupId>
- <artifactId>disconf-client</artifactId>
- <version>2.6.31</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-log4j</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
2、添加配置文件
在resources包下添加配置文件,分別為applicationContext.xml、disconf.properties,如果需要的話,還要添加log4j的配置文件,以便查看控制臺打印的信息。
applicationContext.xml,要註意修改紅色加粗部分,修改為自己創建的包路徑。
[html] view plain copy
- <?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="<span style="color:#ff6666;"><strong>com.tgb.service</strong></span>"/>
- </bean>
- <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
- init-method="init" destroy-method="destroy">
- </bean>
- <bean id="configproperties_disconf"
- class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:/redis.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.properties,具體的配置項可以參考官網:http://disconf.readthedocs.io/zh_CN/latest/config/src/client-config.html#disconf-client
[html] view plain copy
- # 是否使用遠程配置文件,true(默認)會從遠程獲取配置,false則直接從本地獲取配置
- disconf.enable.remote.conf=true
- # 配置服務器的HOST,用逗號分隔
- disconf.conf_server_host=192.168.111.130:8080
- # 版本號
- disconf.version=1.0.0
- # APP的名稱
- disconf.app=YZEvaluationSystem
- # 添加的配置文件所在的環境
- disconf.env=local
- # 忽略分布式配置
- disconf.ignore=
- # 獲取遠程配置重試次數
- disconf.conf_server_url_retry_times=1
- # 獲取遠程配置重試時休眠時間
- disconf.conf_server_url_retry_sleep_seconds=1
- # 用戶自定義的下載路徑
- disconf.user_define_download_dir=./config
log4j.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
- <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
- </layout>
- </appender>
- <root>
- <level value="INFO" />
- <appender-ref ref="CONSOLE" />
- </root>
- </log4j:configuration>
3、創建相應的類
在創建好的包路徑下創建一個JedisConfig類和DisconfRunMain類。
JedisConfig類
[java] view plain copy
- package com.tgb.service;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.annotation.Configuration;
- import com.baidu.disconf.client.common.annotations.DisconfFileItem;
- import com.baidu.disconf.client.common.update.IDisconfUpdate;
- @Configuration
- public class JedisConfig implements IDisconfUpdate {
- protected static final Logger LOGGER = LoggerFactory
- .getLogger(JedisConfig.class);
- // 代表連接地址
- private String host;
- // 代表連接port
- private int port;
- /**
- * 地址, 分布式文件配置
- *
- * @return
- */
- @DisconfFileItem(name = "redis.host", associateField = "host")
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- /**
- * 端口, 分布式文件配置
- *
- * @return
- */
- @DisconfFileItem(name = "redis.port", associateField = "port")
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public void reload() throws Exception {
- LOGGER.info("host: " + host);
- }
- }
DisconfRunMain類
[java] view plain copy
- package com.tgb.service;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.ImportResource;
- @SpringBootApplication
- @ImportResource({ "classpath:applicationContext.xml" })
- // 引入disconf
- public class DisconfRunMain{
- public static void main(String args[]) {
- SpringApplication.run(Application.class, args);
- }
- }
4、執行main方法
在main方法上右鍵run as Java Application即可啟動,啟動過程會打印Spring Boot啟動信息、Disconf的初始化配置信息、連接Zookeeper的信息以及打印出redis.properties文件的內容信息。從文件夾中查看項目,會發現項目路徑下多出一個config文件夾,裏面是下載到本地的redis.properties文件,是因為在disconf.properties中設置了下載路徑為./config。
Disconf使用簡單Demo