创建配置文件
在敲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包。
- <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,要注意修改红色加粗部分,修改为自己创建的包路径。
- <?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
- # 是否使用远程配置文件,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
- <?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类
- 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类
- 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。