创建配置文件

在敲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包。

  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,要注意修改红色加粗部分,修改为自己创建的包路径。

  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

  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

  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类

  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类

  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的更多相关文章

  1. Docker下使用disconf:细说demo开发

    Docker下的disconf实战全文链接 <Docker搭建disconf环境,三部曲之一:极速搭建disconf>: <Docker搭建disconf环境,三部曲之二:本地快速构 ...

  2. 设计模式之单例模式的简单demo

    /* * 设计模式之单例模式的简单demo */ class Single { /* * 创建一个本类对象. * 和get/set方法思想一样,类不能直接调用对象 * 所以用private限制权限 * ...

  3. Spring的简单demo

    ---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...

  4. 使用Spring缓存的简单Demo

    使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...

  5. Managed DirectX中的DirectShow应用(简单Demo及源码)

    阅读目录 介绍 准备工作 环境搭建 简单Demo 显示效果 其他 Demo下载 介绍 DirectX是Microsoft开发的基于Windows平台的一组API,它是为高速的实时动画渲染.交互式音乐和 ...

  6. angular实现了一个简单demo,angular-weibo-favorites

    前面必须说一段 帮客户做了一个过渡期的项目,唯一的要求就是速度,我只是会点儿基础的php,于是就用tp帮客户做了这个项目.最近和客户架构沟通,后期想把项目重新做一下,就用现在最流行的技术,暂时想的使用 ...

  7. Solr配置与简单Demo[转]

    Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...

  8. 二维码简单Demo

    二维码简单Demo 一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...

  9. android JNI 简单demo(2)它JNI demo 写

    android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...

随机推荐

  1. LID&LDS 的另外一种算法

    参见:LIS,LDS的另类算法(原) 然后讲讲我的想法:  有结论不上升子序列的个数=最长上升子序列的长度.....至于为什么,在下面讲 上代码: #include <iostream> ...

  2. 0818基于360开源数据库流量审计MySQL Sniffer

    开源数据库流量审计MySQL Sniffer 我最推崇的数据库安全产品就是基于流量的数据库审计,因为它不需要更改网络结构,并且也是最关键的是,不影响数据库服务器性能,不用苦口婆心的劝数据库管理员安装监 ...

  3. 如何实现网卡bond

    https://jingyan.baidu.com/article/375c8e19da666325f2a229f7.html

  4. Spring MVC-静态页面示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_static_pages.htm 说明:示例基于Spring MVC 4.1.6. ...

  5. 【JS】垃圾回收和块级作用域

    垃圾回收: JavaScript中,开发者不必关心内存分配和回收的问题.这和Java语言相似.有一个垃圾自己主动回收机制.那么JavaScript内部到底是如何回收垃圾的呢? 使用标记回收法:就是说. ...

  6. print puts p 用法

    print puts p 用法 print "a\n" puts "a" p "a\n" 输出为 a a "a\n" p ...

  7. pagex,screenx,clientx的差别

    screenX:參照物为电脑的屏幕左上角,距离电脑屏幕的水平距离 clientX:參照物是内容区域左上角,距离内容区域左上角的水平距离,会随着滚动栏的移动而移动 pageX:參照物也是内容区域的左上角 ...

  8. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  9. 根据图片url地址获取图片的宽高

    /** * 根据img获取图片的宽高 * @param img 图片地址 * @return 图片的对象,对象中图片的真实宽高 */ public BufferedImage getBufferedI ...

  10. Cocos2d-x 3.0final 终结者系列教程13-贪食蛇游戏案例(全)

    快过节了.谢谢了屈原,我们爱你. 应该多几个向屈大人一样跳江的,这样我们就能够放假纪念啦. ---------------------------------快过节了.弄个案例,大家最好还是假期做做, ...