基于ZooKeeper的服务注册实现

简介

本文介绍在本地环境搭建ZooKeeper的伪集群环境的步骤,并且在Spring Boot环境下,如何使用ZooKeeper来注册服务。

本文参考了《架构探险》轻量级微服务架构 这本书。

本文示例代码:zookeeper-demo

搭建ZooKeeper伪集群

下载并安装

在官网下载ZooKeeper相关包后,解压到/etc/zookeeper下,并复制3份(ZooKeeper构建集群时,官网建议部署奇数个节点)。


安装ZooKeeper

修改配置

将conf/zoo_sample.cfg重命名为zoo.cfg,并将节点1修改为如下配置,具体意思请百度,这里不详细展开,因为这里配置的伪集群,所以要求各端口都不一样。

注意:在路径/home/billjiang/zookeeper/zkServer1目录下需要建立一个myid文件,并在文件中写入内容1

节点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

节点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

节点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

批量启动集群shell脚本

为了启动集群,可以一个一个启动,也可以编写shell脚本批量启动:

zookeeper_start.sh

    #!/bin/bash  
    SERVERS="zkServer1 zkServer2 zkServer3"  
      
    for SERVER in $SERVERS  
    do  
          echo "当前"$SERVER"正在启动...................."  
           #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"启动结束--------------------------------------------"                                                                         
    done  

当然也可以编写批量停止的shell脚本。执行批量启动命令后,如下:


启动ZooKeeper集群

启动集群后,可使用bin/zkCli.sh命令查看ZooKeeper的节点数据。

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

服务注册实现

为了演示服务在ZooKeeper上的注册过程,本文这里启动了一个Maven项目zookeeper-learn,项目包含三个module

  • core 注册的核心逻辑
  • client 客户端服务1
  • client2 客户端服务2
zookeeper-learn

其中client/client2项目都依赖了core项目。在它们的pom.xml配置了该依赖

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

core项目的核心代码

定义服务注册接口ServiceRegistry

package com.example.core;

public interface ServiceRegistry {
    /**
     * 注册服务信息
     *
     * @param serviceName    服务名称
     * @param serviceAddress 服务地址
     */
    void register(String serviceName, String serviceAddress);

}

服务注册实现ServiceRegistryImpl
该服务实现将连接ZooKeeper集群,创建节点,并把服务的调用地址作为节点的值存储在该节点上。

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);
            }
            //创建服务节点(持久节点)
            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);
            }
            //创建地址节点
            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();
    }
}

客户端注册服务

客户端在启动时,会将自身服务节点注册到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

服务注册配置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;
    }
}

用来测试的服务接口:TestController

@RestController
public class TestController {

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

在项目启动的时候会将注解中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){
                //注册服务
                serviceRegistry.register(serviceName,String.format("%s:%d",serverAddress,serverPort));
            }
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

同样在client2项目中,相同的代码,唯一的区别就是client2的application.properties的server.port=8082

同时启动两个client2项目后,通过bin/zkCli.sh命令,连接到任意的一台ZooKeeper节点(因为ZooKeeper几点之间数据会保持同步)。显示如下信息:

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

使用get查看子节点的值

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

当停掉一台客户端client后,再次使用ls 命令,只显示address-0000000004

以上代码完成了ZooKeeper的服务注册。

推荐阅读更多精彩内容