-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Tutorial1
这里以 disconf-demo 某个程序片段为例,详细介绍了一个 分布式的配置文件 的简单示例程序。
假设,我们的应用程序使用了Redis服务,我们将使用Jedis来进行编程。编程时,我们需要Redis的Host和Port,通常情况下,我们会把这两个参数放在配置文件里。
在 分布式配置服务器 disconf-web 上添加 redis.properties 配置文件信息。
我们需要一个 redis.properties 文件,里面含有 Host 和 Port。文件内容是:
redis.host=10.48.57.42
redis.port=8310
我们需要把此文件放在项目的ClassPath路径下。
我们撰写一个类JedisConfig,它与 redis.properties 相对应。整个类的完整代码如下:
package com.baidu.disconf.demo.config;
import org.springframework.stereotype.Service;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
/**
* Redis配置文件
*
* @author liaoqiqi
* @version 2014-6-17
*/
@Service
@DisconfFile(filename = "redis.properties")
public class JedisConfig {
// 代表连接地址
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;
}
}
具体步骤是:
- 为这个类 JedisConfig 定义 @DisconfFile 注解,必须指定文件名为 redis.properties 。
- 将redis.properties放在在项目的classpath目录下(最佳实践),这样当远程配置下载失败时,本地配置也可以生效。
- 定义域 host port,分别表示Host和Port。并使用Eclipse为其自动生成 get&set方法。
- 为这两个域的get方法上添加注解 @DisconfFileItem 。添加标记 name, 表示配置文件中的KEY名,这是必填的。标记associateField是可选的,它表示此get方法相关连的域的名字,如果此标记未填,则系统会自动分析get方法,猜测其相对应于域名。强烈建议添加associateField标记。
- 标记它为Spring托管的类 (使用@Service),且 "scope" 都必须是singleton的。
注意:
Eclipse自动生成的get方法,可能与Java的规范不同。这会导致很多问题。因此,建议加上 associateField 标记。
我们的初衷是使用Redis服务。因此,我们需要撰写一个连接Redis的Service类,它使用的是第二步里的分布式配置文件类。完整类的实现代码如下:
package com.baidu.disconf.demo.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import com.baidu.disconf.demo.config.JedisConfig;
import com.baidu.disconf.demo.utils.JedisUtil;
/**
* 一个简单的Redis服务
*
* @author liaoqiqi
* @version 2014-6-17
*/
@Service
public class SimpleRedisService implements InitializingBean, DisposableBean {
protected static final Logger LOGGER = LoggerFactory
.getLogger(SimpleRedisService.class);
// 代表
private Jedis jedis = null;
@Autowired
private JedisConfig jedisConfig;
/**
* 关闭
*/
public void destroy() throws Exception {
if (jedis != null) {
jedis.disconnect();
}
}
/**
* 进行连接
*/
public void afterPropertiesSet() throws Exception {
jedis = JedisUtil.createJedis(jedisConfig.getHost(),
jedisConfig.getPort());
}
/**
* 获取一个值
*
* @param key
* @return
*/
public String getKey(String key) {
if (jedis != null) {
return jedis.get(key);
}
return null;
}
public Jedis getJedis() {
return jedis;
}
public void setJedis(Jedis jedis) {
this.jedis = jedis;
}
}
具体步骤是:
- 此类实现了 InitializingBean, DisposableBean 两个接口,目的是在Bean初始化后进行Redis的连接。
- 为此类添加 @Service ,代表它是一个Bean。Spring托管的,且 "scope" 都必须是singleton的。
准备disconf.properties文件:
Disconf启动需要此文件,文件示例是:
#
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=127.0.0.1:8089
# 版本, 请采用 X_X_X_X 格式
version=1_0_0_0
# APP 请采用 产品线_服务名 格式
app=disconf_demo
# 环境
env=rd
# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
enable.remote.conf=true
# 获取远程配置 重试次数,默认是3次
conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
conf_server_url_retry_sleep_seconds=1
配置相关说明可参考:https://github.com/knightliao/disconf/wiki/%E9%85%8D%E7%BD%AE%E8%AF%B4%E6%98%8E
启动Disconf的方法:
在applicationContext.xml里添加Bean定义:
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destory">
<property name="scanPackage" value="com.baidu.disconf.demo" />
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destory">
</bean>
其中这里,我们定义 属性“scanPackage” 的值是 com.baidu.disconf.demo。这里需要填上你的项目的Package名。这与Spring的auto scan包名功能一样。
至此,分布式配置文件的撰写就已经写完了。
如果你的 分布式配置服务器 disconf-web 平台存活着,那么 程序启动后,你的程序便会去 disconf-web 平台上请求最新配置文件。
执行完毕后,Zookeeper上的组织方式是:
|----disconf
|----disconf_demo_1_0_0_0_rd
|----file
|----redis.properties "redis.host\u003d10.48.57.42\r\nredis.port\u003d8310" 20140804215429 20140804233253
|----liaoqiqi49843_0_5e35ca8b-c16b-450e-a9cb-6d17d8c7b2f8 {"redis.host":"10.48.57.42","redis.port":8310} 20140804233208 20140804233253