zookeeper,spring整合


spring-content.xml配置文件

<bean id="zookeeperClient" class="ZookeeperClient">  

    <constructor-arg index="0" value="10.139.8.40:2181" />
        <property name="mainPath" value="/camsso" />  
  </bean>
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="zooKeeperPropertyPlaceholderConfigurer" class="ZooKeeperPropertyPlaceholderConfigurer">  
   <property name="configurationClient" ref="zookeeperClient"></property>  
   <property name="order" value="1" />  
   <property name="ignoreUnresolvablePlaceholders" value="true" />  

</bean>


讀properties

import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {

private static Properties staticProperties;

@Override
protected void processProperties(
final ConfigurableListableBeanFactory beanFactory,
final Properties props) throws BeansException {
super.processProperties(beanFactory, props);
staticProperties = props;
}
// static method for accessing context properties
public String getContextProperty(final String key) {
return staticProperties.getProperty("referer."+key);
}
}


zookeeper讀配置文件

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;


public class ZooKeeperPropertyPlaceholderConfigurer extends CustomizedPropertyPlaceholderConfigurer{
private static final Logger LOGGER = LoggerFactory
.getLogger(ZooKeeperPropertyPlaceholderConfigurer.class);


private ZookeeperClient configurationClient;


public void setConfigurationClient(final ZookeeperClient configurationClient) {


this.configurationClient = configurationClient;
}


@Override
protected void processProperties(
final ConfigurableListableBeanFactory beanFactoryToProcess,
final Properties props) {


final List<String> list = configurationClient.getChildren();
try {
for (final String key : list) {
final String value = configurationClient
.getData(configurationClient.getMainPath() + "/" + key);
if (!StringUtils.isBlank(value)) {
props.put(key, value);
}
}
} catch (final UnsupportedEncodingException e) {


LOGGER.error("processPropertiesException", e);
} finally {


configurationClient.close();
}
super.processProperties(beanFactoryToProcess, props);


}
}


zookeeper客戶端

import java.io.UnsupportedEncodingException;
import java.util.List;


import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer;


public class ZookeeperClient {
private ZkClient zkc;


   private String mainPath;


   public ZookeeperClient(final String servers) {


       zkc = new ZkClient(servers);
       zkc.setZkSerializer(new BytesPushThroughSerializer());
   }


   public ZkClient getZkClient() {


       return zkc;
   }


   public String getData(final String path) throws UnsupportedEncodingException {


       String result = null;
       final byte[] data = getZkClient().readData(path);
       if (null != data) {
           result = new String(data, "UTF-8");
       }
       return result;
   }


   public List<String> getChildren() {


       List<String> data = null;
       data = getZkClient().getChildren(mainPath);
       return data;
   }


   public void close() {


       getZkClient().close();
       if (null != zkc) {
           zkc = null;
       }
   }


   public String getMainPath() {


       return mainPath;
   }


   public void setMainPath(final String mainPath) {


       this.mainPath = mainPath;
   }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章