java操作zookeeper
阿新 • • 發佈:2020-07-22
依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> </dependencies>
一些api的demo:
public class TestZookeeper { // 2181 : 訪問 zookeeper叢集的埠號 private String connectString = "172.20.10.14:2181,172.20.10.4:2181,172.20.10.5:2181"; private int sessionTimeout = 2000; private ZooKeeper zkClient; @Before public void init() throws IOException { zkClient= new ZooKeeper(connectString, 2000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); } //建立節點 @Test public void create() throws Exception { // 引數1:要建立的節點的路徑; // 引數2:節點資料 ; // 引數3:節點許可權 ; // 引數4:節點的型別 String nodeCreated = zkClient.create("/atguigu", "jinlian".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(nodeCreated); } //建立子節點並監控資料的變化 @Test public void getDataAndWatch() throws Exception { List<String> children = zkClient.getChildren("/", false); for (String s:children){ System.out.println(s); } } // 判斷znode是否存在 @Test public void exist() throws Exception { Stat stat = zkClient.exists("/sanguo", false); System.out.println(stat == null ? "not exist" : "exist"); } }
【案例】伺服器動態節點上下線
需求:
某分散式系統中,主節點可以有多臺,可以動態上下線,任意一臺客戶端都能實時
感受到主伺服器節點的上下線
過程:
程式碼實現
伺服器端:將自己的資訊註冊到zookeeper
public class DistributeServer { // 2181 : 訪問 zookeeper叢集的埠號 private String connectString = "172.20.10.14:2181,172.20.10.4:2181,172.20.10.5:2181"; private int sessionTimeout = 2000; private ZooKeeper zkClient; // 獲取與zookeeper叢集的連線 private void getConnect() throws IOException { zkClient = new ZooKeeper(connectString, 2000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); } private void regist(String hostName) throws Exception { String path = zkClient.create("/servers/server", hostName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostName + " is on line!"); } private void business() throws InterruptedException { Thread.sleep(Long.MAX_VALUE); //保證此程序不結束 } public static void main(String[] args) throws Exception { //1.連線伺服器叢集 DistributeServer server = new DistributeServer(); server.getConnect(); //2.註冊節點 server.regist(args[0]); //3 業務邏輯 server.business(); } }
客戶端:監聽zookeeper某個節點下,子節點的變化資訊
public class DistrubuteClient { // 2181 : 訪問 zookeeper叢集的埠號 private String connectString = "172.20.10.14:2181,172.20.10.4:2181,172.20.10.5:2181"; private int sessionTimeout = 2000; private ZooKeeper zkClient; // 獲取與zookeeper叢集的連線 private void getConnect() throws IOException { zkClient = new ZooKeeper(connectString, 2000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { try { getChildren(); } catch (Exception e) { e.printStackTrace(); } } }); } private void getChildren() throws Exception { List<String> children = zkClient.getChildren("/servers", true); //儲存伺服器節點主機名稱的集合 ArrayList<String> list = new ArrayList<>(); for(String child:children){ byte[] data = zkClient.getData("/servers/" + child, false, null); list.add(new String(data )); } //將所有主機名稱線上列印 System.out.println(list); } private void business() throws InterruptedException { Thread.sleep(Long.MAX_VALUE); //保證此程序不結束 } public static void main(String[] args) throws Exception { DistrubuteClient client =new DistrubuteClient(); // 獲取連線 client.getConnect(); //2.註冊監聽 client.getChildren(); client.business(); } }
這樣客戶端就能動態的監視服務端主機的變化情況