zk使用通知移除节点
前面:https://www.cnblogs.com/toov5/p/9899238.html 服务发生宕机 咋办?
发个事件通知,告知大家哟,
会有通知事件哦
看项目:
服务端:
package com.toov5.zkDubbo; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import org.I0Itec.zkclient.ZkClient; //##ServerScoekt服务端 public class ZkServerScoekt implements Runnable { private static int port = 18083; //socket 服务启动后的所使用的 端口号 public static void main(String[] args) throws IOException { ZkServerScoekt server = new ZkServerScoekt(port); //构造函数传入port regServer(); //去zk注册 Thread thread = new Thread(server); thread.start(); } public ZkServerScoekt(int port) { this.port = port; } //注册服务 public static void regServer(){ //1、建立zk连接 ZkClient zkClient = new ZkClient("192.168.91.5",5000,10000); //2.先创建父节点 String root = "/toov5"; if (!zkClient.exists(root)) { //如果父节点不存,直接创建父节点 zkClient.createPersistent(root); //持久节点 } //3、创建子节点 String nodeName = root+"/service_"+port; String nodeValue="127.0.0.1:"+port; if (zkClient.exists(nodeName)) { //如果存在 直接删除掉 zkClient.delete(nodeName); } zkClient.createEphemeral(nodeName,nodeValue); //字节的 临时节点 如果服务宕机了 找不到了 System.out.println("服务注册成功"+nodeName); } public void run() { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); System.out.println("Server start port:" + port); Socket socket = null; while (true) { socket = serverSocket.accept(); new Thread(new ServerHandler(socket)).start(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (serverSocket != null) { serverSocket.close(); } } catch (Exception e2) { } } } }
客户端
package com.toov5.zkDubbo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; import java.util.List; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.ZkClient; public class ZkServerClient { public static List<String> listServer = new ArrayList<String>(); public static void main(String[] args) { initServer(); ZkServerClient client= new ZkServerClient(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while (true) { String name; try { name = console.readLine(); if ("exit".equals(name)) { System.exit(0); } client.send(name); } catch (IOException e) { e.printStackTrace(); } } } // 注册所有server public static void initServer() { listServer.clear(); // listServer.add("127.0.0.1:18080"); //连接服务 放到list中 存放集群地址 做负载均衡用的 //建立zk连接 final ZkClient zkClient = new ZkClient("192.168.91.5",5000,10000); //读取父节点信息 final String root = "/toov5"; List<String> children=zkClient.getChildren(root); for(String pString : children){ //父+子 =完整的路径 String path = root+"/"+pString; String nodeValue =zkClient.readData(path); //把值放到集合中去 listServer.add(nodeValue); } //服务发现成功 System.out.println("服务发现:"+listServer.toString()); //然后给 serverCount赋值 serverCount=listServer.size(); //使用zk事件监听,如果服务发生宕机情况,重新读取新的节点 zkClient.subscribeChildChanges(root, new IZkChildListener() { public void handleChildChange(String arg0, List<String> children) throws Exception { //children移除之后 剩下的 System.out.println("有服务宕机了,重新读取新的节点信息"+listServer.toString()); listServer.clear(); //清除之前的 然后读取最新的 for(String pString : children){ //父+子 =完整的路径 String path = root+"/"+pString; String nodeValue =zkClient.readData(path); //把值放到集合中去 listServer.add(nodeValue); } serverCount=children.size(); System.out.println("服务发现"+listServer.toString()); } }); } //请求总数 private static int reqCount = 1; //服务个数 private static int serverCount = 0; //初始值是0 // 获取当前server信息 public static String getServer() { // return listServer.get(0); 不能写死 本地负载均衡轮询算法 String serverName = listServer.get(reqCount%serverCount ); System.out.println("客户端请求次数:"+reqCount+"对应服务器"+serverName); reqCount++; return serverName; } public void send(String name) { String server = ZkServerClient.getServer(); String[] cfg = server.split(":"); Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket(cfg[0], Integer.parseInt(cfg[1])); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); out.println(name); while (true) { String resp = in.readLine(); if (resp == null) break; else if (resp.length() > 0) { System.out.println("Receive : " + resp); break; } } } catch (Exception e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zkDobbo</groupId> <artifactId>com.toov5.zkDubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> </dependency> </dependencies> </project>
启动三个服务端,一个客户端。然后随机关闭一个服务端:
控制台:
当有新节点启动时候:
核心部分代码欣赏~~