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包。
- <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。
Disconf使用简单Demo的更多相关文章
- Docker下使用disconf:细说demo开发
Docker下的disconf实战全文链接 <Docker搭建disconf环境,三部曲之一:极速搭建disconf>: <Docker搭建disconf环境,三部曲之二:本地快速构 ...
- 设计模式之单例模式的简单demo
/* * 设计模式之单例模式的简单demo */ class Single { /* * 创建一个本类对象. * 和get/set方法思想一样,类不能直接调用对象 * 所以用private限制权限 * ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- 使用Spring缓存的简单Demo
使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...
- Managed DirectX中的DirectShow应用(简单Demo及源码)
阅读目录 介绍 准备工作 环境搭建 简单Demo 显示效果 其他 Demo下载 介绍 DirectX是Microsoft开发的基于Windows平台的一组API,它是为高速的实时动画渲染.交互式音乐和 ...
- angular实现了一个简单demo,angular-weibo-favorites
前面必须说一段 帮客户做了一个过渡期的项目,唯一的要求就是速度,我只是会点儿基础的php,于是就用tp帮客户做了这个项目.最近和客户架构沟通,后期想把项目重新做一下,就用现在最流行的技术,暂时想的使用 ...
- Solr配置与简单Demo[转]
Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...
- 二维码简单Demo
二维码简单Demo 一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...
- android JNI 简单demo(2)它JNI demo 写
android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...
随机推荐
- java异常处理的面试题
package test; public class Test { public static int method(int i) throws Exception { try { return 10 ...
- POJ 1694 An Old Stone Game
题目: Description There is an old stone game, played on an arbitrary general tree T. The goal is to pu ...
- SCU - 4110 - PE class
先上题目: 4110: PE class Submit your solution Discuss this problem Best solutions Description ...
- HDU - 2833 - WuKong
先上题目: WuKong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Internal Temporary Tables
8.4.4 How MySQL Uses Internal Temporary Tables 这是MySQL手册中的一节,尝试补充了一些解释.用的版本是MySQL5.6.15社区版 In some c ...
- 非form表单提交的数据就要用@requestbody注解获取http传过来的值,尤其json
非form表单提交的数据就要用@requestbody注解获取http传过来的值,尤其json
- HDU 2457
直接从root遍历扩展DP,当扩展到的字母和字符串字母相同时,不用修改,不同时,要求修改加1 注意不要扩展危险结点. #include <iostream> #include <cs ...
- Git .gitignore文件忽略
Git .gitignore文件忽略 学习了:http://blog.csdn.net/yonnangel/article/details/50115059 http://www.cnblogs.co ...
- C++编程->pair(对组)
pair 是 一种模版类型.每一个pair 能够存储两个值.这两种值无限制,能够是tuple.vector ,string,struct等等. 首先来看一下pair的函数 初始化.复制等相关操作例如以 ...
- netty 学习记录一
近期在学习netty相关知识,认为<netty 权威指南>这本书还是挺好的,适合我这样的刚開始学习的人.加上netty本身自带的很多样例,学起来还是挺有兴趣的.简单记录下, 一般serve ...