|
|
package com.yoho.datasync.consumer.handler.config;
|
|
|
|
|
|
import com.netflix.config.DynamicWatchedConfiguration;
|
|
|
import com.netflix.config.source.ZooKeeperConfigurationSource;
|
|
|
import org.apache.curator.framework.CuratorFramework;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import java.util.Properties;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
import static com.netflix.config.ConfigurationManager.install;
|
|
|
|
|
|
/**
|
|
|
* 读取classpath下的config.properties文件,并且写入到zookeeper中
|
|
|
*/
|
|
|
@Component
|
|
|
public class ConfigMangerRegister implements InitializingBean {
|
|
|
|
|
|
@javax.annotation.Resource
|
|
|
private CuratorFramework curatorFramework;
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ConfigMangerRegister.class);
|
|
|
private static final String CONFIG_ROOT_PATH = "/yh/config";
|
|
|
private static final String LOCAL_CONFIG_FILE = "global.properties";
|
|
|
private static final String CACHE_LOCAL_CONFIG_FILE = "cache.properties";
|
|
|
|
|
|
|
|
|
private static AtomicBoolean inited =new AtomicBoolean(false);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
|
start();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 1. 注册zookeeper配置的支持到netflix archaius中
|
|
|
* 2. 读取config.properties的配置,添加到zookeeper中
|
|
|
*/
|
|
|
public void start() throws Exception {
|
|
|
/**
|
|
|
* return directly if zk client not be configured
|
|
|
*/
|
|
|
if(null == curatorFramework){
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* create path if not exist
|
|
|
*/
|
|
|
if (curatorFramework.checkExists().forPath(CONFIG_ROOT_PATH) == null) {
|
|
|
curatorFramework.create().creatingParentContainersIfNeeded().forPath(CONFIG_ROOT_PATH);
|
|
|
}
|
|
|
|
|
|
//register to archaius
|
|
|
this.registerZookeeperConfig();
|
|
|
|
|
|
//register all configurations to zookeeper
|
|
|
this.registerConfigurations();
|
|
|
|
|
|
// register cache configurations to zookeeper
|
|
|
this.registerCacheConfigurations();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* register zookeeper config source to netflix
|
|
|
*
|
|
|
* @see <a href = "http://github.com/Netflix/archaius/wiki/ZooKeeper-Dynamic-Configuration"> netflix archaius </a>
|
|
|
*/
|
|
|
private void registerZookeeperConfig() {
|
|
|
|
|
|
if(!inited.compareAndSet(false, true)){
|
|
|
return;
|
|
|
}
|
|
|
ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorFramework, CONFIG_ROOT_PATH);
|
|
|
try {
|
|
|
zkConfigSource.start();
|
|
|
} catch (Exception e) {
|
|
|
logger.error("start zk config source error !");
|
|
|
}
|
|
|
|
|
|
DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource);
|
|
|
install(zkDynamicConfig);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 读取配置,并且注册到zk中
|
|
|
*/
|
|
|
private void registerConfigurations() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
//check if resource existed
|
|
|
Resource resource = new ClassPathResource(LOCAL_CONFIG_FILE);
|
|
|
if(!resource.exists()) {
|
|
|
logger.info("can not find {} at classpath.", LOCAL_CONFIG_FILE);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Properties props = PropertiesLoaderUtils.loadProperties(resource);
|
|
|
|
|
|
for (String propertyName : props.stringPropertyNames()) {
|
|
|
final String path = ConfigMangerRegister.CONFIG_ROOT_PATH + "/" + propertyName;
|
|
|
//if path is not existed, create
|
|
|
if (this.curatorFramework.checkExists().forPath(path) == null) {
|
|
|
String value = String.valueOf(props.getProperty(propertyName));
|
|
|
this.curatorFramework.create().forPath(path, value.getBytes("UTF-8"));
|
|
|
|
|
|
logger.debug("register configuration: {} --> {} success", propertyName, value);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
logger.info("register all configurations success");
|
|
|
|
|
|
}catch (Exception e){
|
|
|
logger.error("register all configurations to zookeeper failed with exception.", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 读取配置,并且注册到zk中
|
|
|
*/
|
|
|
private void registerCacheConfigurations() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
//check if resource existed
|
|
|
Resource resource = new ClassPathResource(CACHE_LOCAL_CONFIG_FILE);
|
|
|
if(!resource.exists()) {
|
|
|
logger.info("can not find {} at classpath.", CACHE_LOCAL_CONFIG_FILE);
|
|
|
return;
|
|
|
}
|
|
|
Properties props = PropertiesLoaderUtils.loadProperties(resource);
|
|
|
for (String propertyName : props.stringPropertyNames()) {
|
|
|
final String path = ConfigMangerRegister.CONFIG_ROOT_PATH + "/" + propertyName;
|
|
|
//if path is not existed, create
|
|
|
if (this.curatorFramework.checkExists().forPath(path) == null) {
|
|
|
String value = String.valueOf(props.getProperty(propertyName));
|
|
|
this.curatorFramework.create().forPath(path, value.getBytes("UTF-8"));
|
|
|
|
|
|
logger.debug("register configuration: {} --> {} success", propertyName, value);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
logger.info("register all configurations success");
|
|
|
}catch (Exception e){
|
|
|
logger.error("register all configurations to zookeeper failed with exception.", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
//called by spring
|
|
|
@PreDestroy
|
|
|
public void destroy() {
|
|
|
curatorFramework.close();
|
|
|
}
|
|
|
} |
...
|
...
|
|