合肥做网站建设,漂亮的数据型网站,广西住房和城乡建设厅培训中心网,浙江微信网站建设报价服务器动态上下线程序的工作机制
服务器代码#xff1a; 补充#xff1a;volatile关键字#xff1a;java中一切都是对象#xff0c;当多个线程操作同一个对象时候#xff0c;该对象会放在堆内存中#xff0c;而多个线程相当于在多个栈中#xff0c;当A线程想要去除对…服务器动态上下线程序的工作机制
服务器代码 补充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 ListString 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 {//获取服务器子节点的信息并且对父节点进行监听ListString server zk.getChildren(parentNode, true);//先创建一个list来存储服务器信息ListString servers new ArrayListString();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目录下又创建了一个临时带序号的节点此时总共有两个临时节点因此输出主机名两次因为在ListString server zk.getChildren(parentNode, true);中多父节点”/servers”进行了监听。