服务器动态上下线程序的工作机制
服务器代码:
补充:volatile关键字:java中一切都是对象,当多个线程操作同一个对象时候,该对象会放在堆内存中,而多个线程相当于在多个栈中,当A线程想要去除对象中的数据并修改,往往不是直接拿对象的值直接改变其内容,而是先把中的对象赋值一份到A线程栈中,然后再对赋值的对象进行修改,最后把赋值对象与堆中的对象进行比较,不同则修改堆中对象,这样当多个线程访问对象时会存在,当A线程修改了堆中赋值对象的值,但还没来得及修改堆中的对象,而B线程此时拿到的仍然是原对象值并没有发生更改,为了避免这种问题。因此同volatile关键字让线程直接获取对象并修改内容无需赋值一份再去修改。
package cn.itcast.bigdata.zkdist;import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;public class DistributedServer {private static final String connectString = "mini1:2181,mini2:2181,mini3:2181";private static final int sessionTimeout = 2000;private static final String parentNode = "/servers";private ZooKeeper zk = null;/*** 创建到zk的客户端连接* * @throws Exception*/public void getConnect() throws Exception {zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {// 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)System.out.println(event.getType() + "---" + event.getPath());try {zk.getChildren("/", true);} catch (Exception e) {}}});}/*** 向zk集群注册服务器信息* * @param hostname* @throws Exception*/public void registerServer(String hostname) throws Exception {String create = zk.create(parentNode + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);//当服务断掉时ZooKeeper将此临时节点删除,这样client就不会得到服务的信息了System.out.println(hostname + "is online.." + create);}/*** 业务功能* * @throws InterruptedException*/public void handleBussiness(String hostname) throws InterruptedException {System.out.println(hostname + "start working.....");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws Exception {// 获取zk连接DistributedServer server = new DistributedServer();server.getConnect();// 利用zk连接注册服务器信息server.registerServer(args[0]);// 启动业务功能server.handleBussiness(args[0]);}}
输出:当args[0]等于weijie1时候,输出如下
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
45null
weijie1is online/servers/server0000000006
weijie1 is starting
客户端代码:
package com.itcast.zookpeer.zk;import java.io.IOException;
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.ZooKeeper;public class DistributedClient {private static final String connectString = "weijie1:2181,weijie2:2181,weijie3:2181";private static final int sessionTimeout = 2000;private static final String parentNode = "/servers";private static volatile List<String> serverList; ZooKeeper zk = null;//创建客户端连接private void getConnection() throws Exception {zk = new ZooKeeper(connectString, sessionTimeout, new Watcher(){@Overridepublic void process(WatchedEvent event) {System.out.println(event.getType()+"--"+event.getPath());try{getServerList();}catch(Exception e){}}});}//获取服务器信息列表private void getServerList() throws Exception {//获取服务器子节点的信息,并且对父节点进行监听List<String> server = zk.getChildren(parentNode, true);//先创建一个list来存储服务器信息List<String> servers = new ArrayList<String>();for (String string : server) {byte[] data = zk.getData(parentNode+"/"+string, false, null);servers.add(new String(data));//new String对data进行解析转成字符串类型}serverList = servers;System.out.println(serverList);}private void BundleBusiness() throws Exception {System.out.println("is starting");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws Exception {DistributedClient distribute = new DistributedClient();distribute.getConnection();distribute.getServerList();distribute.BundleBusiness();}}
输出:
- 当启动一次服务器时候此时输出结果
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
None--null
[weijie1]
is starting
[weijie1] - 再启动一次DistributedServer时候输出
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
None--null
[weijie1]
is starting
[weijie1]
NodeChildrenChanged--/servers
[weijie1, weijie1]
分析:当启动两次服务器此时在servers目录下又创建了一个临时带序号的节点,此时总共有两个临时节点,因此输出主机名两次,因为在List<String> server = zk.getChildren(parentNode, true);
中多父节点”/servers”进行了监听。