1. 程式人生 > >Disconf使用簡單Demo

Disconf使用簡單Demo

get prot html 分享 ocs aspectj baidu 下載到本地 lips

創建配置文件

在敲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
  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <guava.version>16.0.1</guava.version>
  4. <java.version>1.7</java.version>
  5. </properties>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>1.3.2.RELEASE</version>
  10. </parent>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.baidu.disconf</groupId>
  14. <artifactId>disconf-client</artifactId>
  15. <version>2.6.31</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>redis.clients</groupId>
  19. <artifactId>jedis</artifactId>
  20. <version>2.1.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. <exclusions>
  26. <exclusion>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-logging</artifactId>
  29. </exclusion>
  30. </exclusions>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-log4j</artifactId>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>

2、添加配置文件

在resources包下添加配置文件,分別為applicationContext.xml、disconf.properties,如果需要的話,還要添加log4j的配置文件,以便查看控制臺打印的信息。

applicationContext.xml,要註意修改紅色加粗部分,修改為自己創建的包路徑。

[html] view plain copy
  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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  6. http://www.springframework.org/schema/aop
  7. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  8. <aop:aspectj-autoproxy proxy-target-class="true"/>
  9. <!-- 使用disconf必須添加以下配置 -->
  10. <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
  11. destroy-method="destroy">
  12. <property name="scanPackage" value="<span style="color:#ff6666;"><strong>com.tgb.service</strong></span>"/>
  13. </bean>
  14. <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
  15. init-method="init" destroy-method="destroy">
  16. </bean>
  17. <bean id="configproperties_disconf"
  18. class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
  19. <property name="locations">
  20. <list>
  21. <value>classpath:/redis.properties</value>
  22. </list>
  23. </property>
  24. </bean>
  25. <bean id="propertyConfigurer"
  26. class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
  27. <property name="ignoreResourceNotFound" value="true" />
  28. <property name="ignoreUnresolvablePlaceholders" value="true" />
  29. <property name="propertiesArray">
  30. <list>
  31. <ref bean="configproperties_disconf" />
  32. </list>
  33. </property>
  34. </bean>
  35. </beans>

disconf.properties,具體的配置項可以參考官網:http://disconf.readthedocs.io/zh_CN/latest/config/src/client-config.html#disconf-client

[html] view plain copy
  1. # 是否使用遠程配置文件,true(默認)會從遠程獲取配置,false則直接從本地獲取配置
  2. disconf.enable.remote.conf=true
  3. # 配置服務器的HOST,用逗號分隔
  4. disconf.conf_server_host=192.168.111.130:8080
  5. # 版本號
  6. disconf.version=1.0.0
  7. # APP的名稱
  8. disconf.app=YZEvaluationSystem
  9. # 添加的配置文件所在的環境
  10. disconf.env=local
  11. # 忽略分布式配置
  12. disconf.ignore=
  13. # 獲取遠程配置重試次數
  14. disconf.conf_server_url_retry_times=1
  15. # 獲取遠程配置重試時休眠時間
  16. disconf.conf_server_url_retry_sleep_seconds=1
  17. # 用戶自定義的下載路徑
  18. disconf.user_define_download_dir=./config

log4j.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
  4. <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
  5. <layout class="org.apache.log4j.PatternLayout">
  6. <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
  7. </layout>
  8. </appender>
  9. <root>
  10. <level value="INFO" />
  11. <appender-ref ref="CONSOLE" />
  12. </root>
  13. </log4j:configuration>

3、創建相應的類

在創建好的包路徑下創建一個JedisConfig類和DisconfRunMain類。

JedisConfig類

[java] view plain copy
  1. package com.tgb.service;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.context.annotation.Configuration;
  5. import com.baidu.disconf.client.common.annotations.DisconfFileItem;
  6. import com.baidu.disconf.client.common.update.IDisconfUpdate;
  7. @Configuration
  8. public class JedisConfig implements IDisconfUpdate {
  9. protected static final Logger LOGGER = LoggerFactory
  10. .getLogger(JedisConfig.class);
  11. // 代表連接地址
  12. private String host;
  13. // 代表連接port
  14. private int port;
  15. /**
  16. * 地址, 分布式文件配置
  17. *
  18. * @return
  19. */
  20. @DisconfFileItem(name = "redis.host", associateField = "host")
  21. public String getHost() {
  22. return host;
  23. }
  24. public void setHost(String host) {
  25. this.host = host;
  26. }
  27. /**
  28. * 端口, 分布式文件配置
  29. *
  30. * @return
  31. */
  32. @DisconfFileItem(name = "redis.port", associateField = "port")
  33. public int getPort() {
  34. return port;
  35. }
  36. public void setPort(int port) {
  37. this.port = port;
  38. }
  39. public void reload() throws Exception {
  40. LOGGER.info("host: " + host);
  41. }
  42. }

DisconfRunMain類

[java] view plain copy
  1. package com.tgb.service;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ImportResource;
  5. @SpringBootApplication
  6. @ImportResource({ "classpath:applicationContext.xml" })
  7. // 引入disconf
  8. public class DisconfRunMain{
  9. public static void main(String args[]) {
  10. SpringApplication.run(Application.class, args);
  11. }
  12. }

4、執行main方法

在main方法上右鍵run as Java Application即可啟動,啟動過程會打印Spring Boot啟動信息、Disconf的初始化配置信息、連接Zookeeper的信息以及打印出redis.properties文件的內容信息。從文件夾中查看項目,會發現項目路徑下多出一個config文件夾,裏面是下載到本地的redis.properties文件,是因為在disconf.properties中設置了下載路徑為./config。

Disconf使用簡單Demo