欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

ZooKeeper如何配置管理

這篇文章將為大家詳細(xì)講解有關(guān)ZooKeeper如何配置管理,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到港北網(wǎng)站設(shè)計(jì)與港北網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋港北地區(qū)。

   主要完成的目標(biāo)是:Spring PropertyPlaceholderConfigurer配置文件加載器集成ZooKeeper來(lái)實(shí)現(xiàn)遠(yuǎn)程配置讀取。

    配置管理(Configuration Management)。
    在集群服務(wù)中,可能都會(huì)遇到一個(gè)問(wèn)題:那就是當(dāng)需要修改配置的時(shí)候,必須要對(duì)每個(gè)實(shí)例都進(jìn)行修改,這是一個(gè)很繁瑣的事情,并且易出錯(cuò)。當(dāng)然可以使用腳本來(lái)解決,但這不是最好的解決辦法。

OK,Let's go!

我們先看看項(xiàng)目結(jié)構(gòu)

ZooKeeper如何配置管理

ZooKeeperPropertyPlaceholderConfigurer.java

繼承org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,重寫(xiě)processProperties(beanFactoryToProcess, props)來(lái)完成遠(yuǎn)端配置加載的實(shí)現(xiàn)

package org.bigmouth.common.zookeeper.config.spring;
 
import java.io.UnsupportedEncodingException;
import java.util.Properties;
 
import org.apache.commons.lang.StringUtils;
import org.bigmouth.common.zookeeper.config.Config;
import org.bigmouth.common.zookeeper.config.ZooKeeperConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 
 
public class ZooKeeperPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
     
    public static final String PATH = "zoo.paths";
 
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
         
        try {
            fillCustomProperties(props);
             
            System.out.println(props);
        }
        catch (Exception e) {
            // Ignore
            e.printStackTrace();
        }
    }
 
    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)) {
            // 完整的應(yīng)該還需要處理:多條配置、value中包含=、忽略#號(hào)開(kāi)頭
            String[] cfgItem = StringUtils.split(cfg, "=");
            props.put(cfgItem[0], cfgItem[1]);
        }
    }
 
    private byte[] getData(Properties props) throws Exception {
        String path = props.getProperty(PATH);
        Config config = new ZooKeeperConfig();
        return config.getConfig(path);
    }
 
}

Config.java

配置操作接口 

package org.bigmouth.common.zookeeper.config;
 
 
public interface Config {
 
    byte[] getConfig(String path) throws Exception;
}

Startup.java

程序啟動(dòng)入口

package org.bigmouth.common.zookeeper.config;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class Startup {
 
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("classpath:/config/applicationContext.xml");
    }
 
}

ZooKeeperConfig.java

配置操作接口ZooKeeper的實(shí)現(xiàn)

package org.bigmouth.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 path) throws Exception {
        CuratorFramework client = ZooKeeperFactory.get();
        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

管理ZooKeeper客戶端連接

package org.bigmouth.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 = "cfg";
 
    public static CuratorFramework get() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIMEMS, MAX_RETRIES);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(CONNECT_STRING)
                .retryPolicy(retryPolicy)
                .namespace(NAME_SPACE)
                .build();
        client.start();
        return client;
    }
}

applicationContext.xml

配置加載器使用我們自己創(chuàng)建的ZooKeeperPropertyPlaceholderConfigurer,因?yàn)樗貙?xiě)了processProperties方法。這個(gè)方法里會(huì)去讀取遠(yuǎn)程配置。

<?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="org.bigmouth.common.zookeeper.config.spring.ZooKeeperPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>
     
</beans>

application.properties

項(xiàng)目配置文件,里面除了配置ZooKeeper服務(wù)器地址和讀取的節(jié)點(diǎn)以外,其他所有的配置都應(yīng)該保存在ZooKeeper中。

zoo.paths=/properties

設(shè)置ZooKeeper數(shù)據(jù)

登錄ZooKeeper中為節(jié)點(diǎn) /cfg/properties 添加一條配置項(xiàng):

ZooKeeper如何配置管理
 

如圖所示:我創(chuàng)建了一個(gè)節(jié)點(diǎn) /cfg/properties 并設(shè)置內(nèi)容為:jdbc.driver=org.postgresql.Driver

運(yùn)行Startup.java

ZooKeeper如何配置管理

OK 了,zoo.paths是本地application.properties文件中的,jdbc.driver是遠(yuǎn)程ZooKeeper服務(wù)器中的。

項(xiàng)目中需要依賴的jar包

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.0.3.RELEASE</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>

關(guān)于“ZooKeeper如何配置管理”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)站名稱:ZooKeeper如何配置管理
瀏覽路徑:http://chinadenli.net/article48/ihoshp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)小程序開(kāi)發(fā)靜態(tài)網(wǎng)站動(dòng)態(tài)網(wǎng)站網(wǎng)站設(shè)計(jì)網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司