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 ...
随机推荐
- Spring Cloud-Zuul(十)
个人理解 在微服务体系体系中 我们会有很多服务.在内部体系中 通过eureka实现服务的自动发现通过ribbon实现服务的调用.但是如果对外部体系提供接口 我们就会涉及到接口的安全性,我们不能可能对每 ...
- 0120Keeplived实现自动切换Mysql服务
转自http://biancheng.dnbcw.info/mysql/381020.html Keepalived+mysql 自动切换网络结构:VIP 192.168.88.200mysq11 1 ...
- 【ACM】hdu_1096_A+BVIII_201307261748
A+B for Input-Output Practice (VIII)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- HDU 4506
EASY题,快速幂... #include <iostream> #include <cstdio> #include <cstring> #include < ...
- HDU 4507
数位DP. 一般是利用DFS来求数位DP了,结合了记忆化搜索.设dp[i][j][k]为前i位,并且前i位的数位和mod7为j,前i位的数字的表示数字值mod7.为什么可以这样呢?因为继续DFS下去, ...
- [Cypress] Load Data from Test Fixtures in Cypress
When creating integration tests with Cypress, we’ll often want to stub network requests that respond ...
- Linux - 网络相关指令
系统时间与开关机 查看系统时间 date 查看硬件日期 hwclock 学习Linux不必全部指令都会,只要记住主要常用的几个就可以了.--MK 关机命令 shutdown init reboot p ...
- DNS通道检测 国内学术界研究情况——研究方法:基于特征或者流量,使用机器学习决策树分类算法居多
http://xuewen.cnki.net/DownloadArticle.aspx?filename=BMKJ201104017&dbtype=CJFD<浅析基于DNS协议的隐蔽通道 ...
- IP与以太网的包收发操作
你好,这是<网络是怎样连接的>的第3篇读书笔记,第二章<用电信号传输TCP/IP>后半部分:IP与以太网的包收发操作. 先看下经典的TCP/IP四层模型: 通常,下层模块支撑上 ...
- 爬虫中之Requests 模块的进阶
requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...