基于ZooKeeper的服務(wù)注冊實(shí)現(xiàn)

簡介

本文介紹在本地環(huán)境搭建ZooKeeper的偽集群環(huán)境的步驟,并且在Spring Boot環(huán)境下,如何使用ZooKeeper來注冊服務(wù)。

本文參考了《架構(gòu)探險(xiǎn)》輕量級(jí)微服務(wù)架構(gòu) 這本書。

本文示例代碼:zookeeper-demo

搭建ZooKeeper偽集群

下載并安裝

在官網(wǎng)下載ZooKeeper相關(guān)包后,解壓到/etc/zookeeper下,并復(fù)制3份(ZooKeeper構(gòu)建集群時(shí),官網(wǎng)建議部署奇數(shù)個(gè)節(jié)點(diǎn))。


安裝ZooKeeper

修改配置

將conf/zoo_sample.cfg重命名為zoo.cfg,并將節(jié)點(diǎn)1修改為如下配置,具體意思請百度,這里不詳細(xì)展開,因?yàn)檫@里配置的偽集群,所以要求各端口都不一樣。

注意:在路徑/home/billjiang/zookeeper/zkServer1目錄下需要建立一個(gè)myid文件,并在文件中寫入內(nèi)容1

節(jié)點(diǎn)1配置

# The number of milliseconds of each tick
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/billjiang/zookeeper/zkServer1
clientPort=2181
#cluster
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

節(jié)點(diǎn)2配置

# The number of milliseconds of each tick
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/billjiang/zookeeper/zkServer2
clientPort=2182
#cluster
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

節(jié)點(diǎn)3配置

# The number of milliseconds of each tick
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/billjiang/zookeeper/zkServer3
clientPort=2183
#cluster
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

批量啟動(dòng)集群shell腳本

為了啟動(dòng)集群,可以一個(gè)一個(gè)啟動(dòng),也可以編寫shell腳本批量啟動(dòng):

zookeeper_start.sh

    #!/bin/bash  
    SERVERS="zkServer1 zkServer2 zkServer3"  
      
    for SERVER in $SERVERS  
    do  
          echo "當(dāng)前"$SERVER"正在啟動(dòng)...................."  
           #ssh root@$SERVER "source /etc/profile;/usr/apps/zookeeper-3.4.9/bin/zkServer.sh start"  
          sudo /etc/zookeeper/$SERVER/bin/zkServer.sh start
          echo $SERVER"啟動(dòng)結(jié)束--------------------------------------------"                                                                         
    done  

當(dāng)然也可以編寫批量停止的shell腳本。執(zhí)行批量啟動(dòng)命令后,如下:


啟動(dòng)ZooKeeper集群

啟動(dòng)集群后,可使用bin/zkCli.sh命令查看ZooKeeper的節(jié)點(diǎn)數(shù)據(jù)。

以上完成了ZooKeeper偽集群的搭建。

服務(wù)注冊實(shí)現(xiàn)

為了演示服務(wù)在ZooKeeper上的注冊過程,本文這里啟動(dòng)了一個(gè)Maven項(xiàng)目zookeeper-learn,項(xiàng)目包含三個(gè)module

  • core 注冊的核心邏輯
  • client 客戶端服務(wù)1
  • client2 客戶端服務(wù)2
zookeeper-learn

其中client/client2項(xiàng)目都依賴了core項(xiàng)目。在它們的pom.xml配置了該依賴

<dependency>
            <groupId>com.cnpc</groupId>
            <artifactId>core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
</dependency>

core項(xiàng)目的核心代碼

定義服務(wù)注冊接口ServiceRegistry

package com.example.core;

public interface ServiceRegistry {
    /**
     * 注冊服務(wù)信息
     *
     * @param serviceName    服務(wù)名稱
     * @param serviceAddress 服務(wù)地址
     */
    void register(String serviceName, String serviceAddress);

}

服務(wù)注冊實(shí)現(xiàn)ServiceRegistryImpl
該服務(wù)實(shí)現(xiàn)將連接ZooKeeper集群,創(chuàng)建節(jié)點(diǎn),并把服務(wù)的調(diào)用地址作為節(jié)點(diǎn)的值存儲(chǔ)在該節(jié)點(diǎn)上。

package com.example.core;

import org.apache.zookeeper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

@Component
public class ServiceRegistryImpl implements ServiceRegistry, Watcher {
    private static Logger logger = LoggerFactory.getLogger(ServiceRegistryImpl.class);
    private static CountDownLatch latch = new CountDownLatch(1);
    private ZooKeeper zk;
    private static final int SESSION_TIMEOUT = 5000;
    public ServiceRegistryImpl() {

    }

    public ServiceRegistryImpl(String zkServers) {
        try {
            zk = new ZooKeeper(zkServers, SESSION_TIMEOUT, this);
            latch.await();
            logger.debug("connected to zookeeper");
        } catch (Exception ex) {
            logger.error("create zookeeper client failure", ex);
        }
    }

    private static final String REGISTRY_PATH = "/registry";

    @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);
                logger.debug("create registry node:{}", registryPath);
            }
            //創(chuàng)建服務(wù)節(jié)點(diǎn)(持久節(jié)點(diǎn))
            String servicePath = registryPath + "/" + serviceName;
            if (zk.exists(servicePath, false) == null) {
                zk.create(servicePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                logger.debug("create service node:{}", servicePath);
            }
            //創(chuàng)建地址節(jié)點(diǎn)
            String addressPath = servicePath + "/address-";
            String addressNode = zk.create(addressPath, serviceAddress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            logger.debug("create address node:{} => {}", addressNode, serviceAddress);
        } catch (Exception e) {
            logger.error("create node failure", e);
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
            latch.countDown();
    }
}

客戶端注冊服務(wù)

客戶端在啟動(dòng)時(shí),會(huì)將自身服務(wù)節(jié)點(diǎn)注冊到ZooKeeper集群中。

application.properties配置

server.address=127.0.0.1
server.port=8080
registry.servers=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

服務(wù)注冊配置RegistryConfig
這樣客戶端可以讀取appliaction.properties的ZooKeeper配置

package com.example.client;

import com.example.core.ServiceRegistry;
import com.example.core.ServiceRegistryImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties(prefix = "registry")
public class RegistryConfig {

    private String servers;

    @Bean
    public ServiceRegistry serviceRegistry() {
        return new ServiceRegistryImpl(servers);
    }

    public void setServers(String servers) {
        this.servers = servers;
    }
}

用來測試的服務(wù)接口:TestController

@RestController
public class TestController {

    @RequestMapping(name="HelloService",method = RequestMethod.GET,path = "/hello")
    public String hello(){
        return "Hello";
    }
}

在項(xiàng)目啟動(dòng)的時(shí)候會(huì)將注解中name屬性有值的方法注冊到ZooKeeper集群中,

package com.example.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.example.core.ServiceRegistry;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

@Component
public class WebListener implements ServletContextListener {

    @Value("${server.address}")
    private String serverAddress;

    @Value("${server.port}")
    private int serverPort;

    @Autowired
    public ServiceRegistry serviceRegistry;


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext=sce.getServletContext();
        ApplicationContext applicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        RequestMappingHandlerMapping mapping=applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo,HandlerMethod> infoMap=mapping.getHandlerMethods();
        for (RequestMappingInfo info : infoMap.keySet()) {
            String serviceName=info.getName();
            System.out.println("-----------------"+serviceName);
            if(serviceName!=null){
                //注冊服務(wù)
                serviceRegistry.register(serviceName,String.format("%s:%d",serverAddress,serverPort));
            }
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

同樣在client2項(xiàng)目中,相同的代碼,唯一的區(qū)別就是client2的application.properties的server.port=8082

同時(shí)啟動(dòng)兩個(gè)client2項(xiàng)目后,通過bin/zkCli.sh命令,連接到任意的一臺(tái)ZooKeeper節(jié)點(diǎn)(因?yàn)閆ooKeeper幾點(diǎn)之間數(shù)據(jù)會(huì)保持同步)。顯示如下信息:

[zk: localhost:2181(CONNECTED) 18] ls /registry/HelloService
[address-0000000004, address-0000000003]

使用get查看子節(jié)點(diǎn)的值

get /registry/HelloService/address-0000000003
127.0.0.1:8080
cZxid = 0x100000026
ctime = Wed Aug 09 18:00:56 CST 2017
mZxid = 0x100000026
mtime = Wed Aug 09 18:00:56 CST 2017
pZxid = 0x100000026
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x15dc5782fe8000d
dataLength = 14
numChildren = 0

get /registry/HelloService/address-0000000004
127.0.0.1:8081
cZxid = 0x100000028
ctime = Wed Aug 09 18:03:05 CST 2017
mZxid = 0x100000028
mtime = Wed Aug 09 18:03:05 CST 2017
pZxid = 0x100000028
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x15dc5782fe8000e
dataLength = 14
numChildren = 0

當(dāng)停掉一臺(tái)客戶端client后,再次使用ls 命令,只顯示address-0000000004

以上代碼完成了ZooKeeper的服務(wù)注冊。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容