zookeeper 管理 项目配置文件
项目的目录结构
- Config.java
package com.pkl.common.zookeeper.config;
public interface Config {
byte[] getConfig(String host, String path) throws Exception;
}
- ZooKeeperConfig.java
package com.pkl.common.zookeeper.config;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.data.Stat;
public class ZooKeeperConfig implements Config {
@Override
public byte[] getConfig(String host, String path) throws Exception {
CuratorFramework client = ZooKeeperFactory.get(host);
if (!exists(client, path)) {
throw new RuntimeException("Path " + path + " does not exists.");
}
return client.getData().forPath(path);
}
private boolean exists(CuratorFramework client, String path) throws Exception {
Stat stat = client.checkExists().forPath(path);
return !(stat == null);
}
}
- ZooKeeperFactory.java
package com.pkl.common.zookeeper.config;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class ZooKeeperFactory {
public static final String CONNECT_STRING = "172.16.3.42:2181,172.16.3.65:2181,172.16.3.24:2181";
public static final int MAX_RETRIES = 3;
public static final int BASE_SLEEP_TIMEMS = 3000;
public static final String NAME_SPACE = "zookeeper";
public static CuratorFramework get(String host) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIMEMS, MAX_RETRIES);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(host)
.retryPolicy(retryPolicy)
.namespace(NAME_SPACE)
.build();
client.start();
return client;
}
}
- ZooKeeperPropertyPlaceholderConfigurer.java
package com.pkl.common.zookeeper.config.spring;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.pkl.common.zookeeper.config.Config;
import com.pkl.common.zookeeper.config.ZooKeeperConfig;
public class ZooKeeperPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
public static final String PATH = "zoo.paths";
public static final String ZK_HOST = "zkhost";
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
try {
fillCustomProperties(props);
System.out.println(props);
}
catch (Exception e) {
e.printStackTrace();
}
super.processProperties(beanFactoryToProcess, props);
}
private void fillCustomProperties(Properties props) throws Exception {
byte[] data = getData(props);
fillProperties(props, data);
}
private void fillProperties(Properties props, byte[] data) throws UnsupportedEncodingException {
String cfg = new String(data, "UTF-8");
if (StringUtils.isNotBlank(cfg)) {
// 完整的应该还需要处理:多条配置、value中包含=、忽略#号开头
String[] cfgItems = StringUtils.split(cfg, "\n");//s.split("\n");
for (String cfgItem : cfgItems) {
cfgItem = StringUtils.trim(cfgItem);
if (!cfgItem.startsWith("#")) {
String[] item = StringUtils.split(cfgItem, "=");
if (item.length == 2) {
props.put(StringUtils.trim(StringUtils.trim(item[0])), StringUtils.trim(item[1]));
}
}
}
}
}
private byte[] getData(Properties props) throws Exception {
String path = props.getProperty(PATH);
String host = props.getProperty(ZK_HOST);
Config config = new ZooKeeperConfig();
return config.getConfig(host, path);
}
}
- application.properties
zoo.paths=/properties/dubbo-provider
zkhost=172.16.190.128:2181
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean class="com.pkl.common.zookeeper.config.spring.ZooKeeperPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
</beans>
- pom.xml dependency (spring.version : 4.0.2.RELEASE)
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- ZooKeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.4.2</version>
</dependency>
- 将此项目打成jar 放在maven仓库中(使用的命令 clean install)
在其他项目中使用。
1:依赖添加
2:替换掉原来加载配置文件的 配置
3:注意两个项目的jar包冲突。 由于我的两个项目中引用的zookeeper的版本是不一样的。所以我在添加依赖的时候 把zookeeper的依赖 exclude掉了