springboot整合zookeeper

在springboot中全部的整合都是以bean的形式注入對象,從數據庫coon、redis conn、再到整合的zookeeper,依然是依照bean注入鏈接對象,經過zookeeper api對zookeeper中node 數據進行增刪改查等操做,從而實現配置同步。這篇文章只是初步使用web服務,在服務啓動時註冊服務,並將配置文件內容寫入zookeeper,經過api接口獲取配置內容。至於多節點配置文件同步一致性,則是之後須要深刻研究的主題。java

在zookeeper_caesar建立兩個模塊,core和client,core中存放zookeeper鏈接和相關操做方法,client是正常的web服務。在springboot分層思想中core至關於DAO層,進行zookeeper操做,在client的service層和controller層進行調用和處理。node

其中client依賴core模塊,在其pom.xml中添加core模塊信息,其後在client中添加spring模塊spring-boot-starter-web(spring對servlet封裝的模塊和嵌入式tomcat)python

        <dependency>
            <groupId>com.soft.caesar</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

1.client分析

 

在RegistryConfig.java中建立須要的bean  serviceRegistry 即zookeeper鏈接對象 zk = new ZooKeeper(zkServers,SESSION_TIMEOUT,this)git

在TestController.java中調用serviceRestry即core中定義操做getValue,獲取zookeeper中數據github

在WebListener.java中監聽web服務啓動,啓動時將服務存入zookeeperweb

ClientApplication.py 啓動服務主函數redis

2.core分析

@Component
public class ServiceRegistryImpl implements ServiceRegistry,Watcher {

    private static CountDownLatch latch = new CountDownLatch(1); # 多線程時,等待,直到一個線程時,在latch被喚醒
    private ZooKeeper zk;
    private static final int SESSION_TIMEOUT=5000;
    private static final String REGISTRY_PATH = "/registry";

    public ServiceRegistryImpl() {
    }

    public ServiceRegistryImpl(String zkServers) {
        try {
            zk = new ZooKeeper(zkServers,SESSION_TIMEOUT,this);
            latch.await();# latch等待喚醒
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void register(String serviceName, String serviceAddress) {
        try {
            String registryPath = REGISTRY_PATH;
            if (zk.exists(registryPath, false) == null) {
                zk.create(registryPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); #持久化建立/registry node
            }
            //建立服務節點(持久節點)
            String servicePath = registryPath + "/" + serviceName;
            if (zk.exists(servicePath, false) == null) {
                zk.create(servicePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            //建立地址節點
            String addressPath = servicePath + "/address-"; # 此處節點是瞬態的節點,當服務斷開zookeeper鏈接時,節點消失,從新鏈接時,address- 以序列添加末尾序列值。
這種序列方法能夠判斷註冊服務的主被,先註冊的數字小,後註冊的數字大,每次從主上同步數據到被。在服務異常時,節點自動消失,能夠探測服務狀態 String addressNode = zk.create(addressPath, serviceAddress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); } catch (Exception e){ e.printStackTrace(); } } @Override public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) latch.countDown(); }

驗證

啓動zookeeper,啓動以上web服務spring

java客戶端登陸zookeeper,查詢註冊服務的註冊信息數據庫

調用接口查詢zookeeper數據api

 以上是關於zookeeper的初步探索,能夠參考https://github.com/CaesarLinsa/zookeeper_caesar,在version1.0分支中去掉core模塊,添加到service層中,添加對zookeeper操做接口,實如今接口修改zookeeper同時,配置文件發生變動。固然如此須要每一個服務的守護進程中存在相似socket通訊,在server端發生變化時,在watch中向守護進程中發送相關命令,促使配置變動,服務啓動或者不啓動加載配置。

相關文章
相關標籤/搜索