【ZooKeeper】分布式系统服务器上下线自动感知程序开发

来源:互联网 发布:nginx alias用法 编辑:程序博客网 时间:2024/05/21 00:15

需求概述

一个集群来提供服务。集群中的服务器会动态变化,服务器会上线、下线或宕机。此时客户端应该能够知道服务器状态的变化情况以选择一个最好的服务器提供服务。

客户端能获知服务器上下线状态的变化。

解决方案

使用ZooKeeper框架。

服务器启动时先向ZooKeeper集群注册,写入自己的信息。注册的节点是一种临时节点。最好也是序列化的节点

客户端启动时获取服务器父目录下的子节点列表并注册监听,获取到当前在线的服务器列表并根据连接策略选择服务器连接。在收到服务器状态变化的监听信息后重新获取服务器列表并注册监听。

代码实现

分布式服务器 DistributedServer.java

package com.mrbcy.bigdata.zk;import java.io.IOException;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.data.Stat;public class DistributedServer {    private static String connectString = "amaster:2181,anode1:2181,anode2:2181";    private static int sessionTimeout = 2000;    private ZooKeeper zk = null;    private String hostName;    private String groupName = "/servers";    public DistributedServer(String hostName) throws Exception{        this.hostName = hostName;        this.zk = new ZooKeeper(connectString , sessionTimeout, null);    }    // 将自己的信息注册到zk集群    public void registToZK() throws Exception {        // 判断父目录是否存在,不存在则创建        Stat groupStat = zk.exists(groupName, false);        if(groupStat == null){            zk.create(groupName, "distributed server list".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);        }        String registAddr = zk.create(groupName+"/server", hostName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);        System.out.println("Server is starting, reg addr:" + registAddr);    }    // 下线    public void offline() throws Exception {        zk.close();    }}

客户端 DistributedClient.java

package com.mrbcy.bigdata.zk;import java.util.ArrayList;import java.util.List;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.EventType;import org.apache.zookeeper.ZooKeeper;public class DistributedClient {    private static String connectString = "amaster:2181,anode1:2181,anode2:2181";    private static int sessionTimeout = 2000;    private ZooKeeper zk = null;    private String groupName = "/servers";    // 开始监听服务器列表变化    public void startListenServerListChange() throws Exception{        this.zk = new ZooKeeper(connectString , sessionTimeout, new Watcher(){            @Override            public void process(WatchedEvent event) {//              System.out.println(event.toString());//              System.out.println(event.getType() + "---" + event.getPath());                // 重新注册监听                try {                    getServerList();                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        });    }    private void getServerList() throws Exception {        List<String> children = zk.getChildren(groupName, true);        List<String> servers = new ArrayList<String>();        for(String child : children) {            byte[] data = zk.getData(groupName+"/"+child, null, null);            servers.add(new String(data));        }        System.out.println("--------------当前服务器列表--------------");        for(String server : servers){            System.out.println(server);        }        System.out.println("-----------------------------------------");    }}

服务器启动程序 DynamicServerListTest.java

package com.mrbcy.bigdata.zk;import org.junit.Test;public class DynamicServerListTest {    @Test    public void testServerUp() throws Exception{        DistributedServer server1 = new DistributedServer("server01");        DistributedServer server2 = new DistributedServer("server02");        server1.registToZK();        server2.registToZK();        Thread.sleep(10000);        server1.offline();        System.out.println("server01下线...");        Thread.sleep(10000);        System.out.println("server02下线...");    }}

客户端启动程序 DynamicClientTest.java

package com.mrbcy.bigdata.zk;import org.junit.Test;public class DynamicClientTest {    @Test    public void startUpClient() throws Exception{        DistributedClient client = new DistributedClient();        client.startListenServerListChange();        Thread.sleep(1000000);    }}

测试结果

服务器端:

Server is starting, reg addr:/servers/server0000000014Server is starting, reg addr:/servers/server0000000015server01下线...server02下线...

客户端:

--------------当前服务器列表---------------------------------------------------------------------当前服务器列表--------------server02server01-------------------------------------------------------当前服务器列表--------------server02-------------------------------------------------------当前服务器列表-------------------------------------------------------
0 0