日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ⅶ:教你一招利用zookeeper作为服务的配置中心

發布時間:2024/10/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ⅶ:教你一招利用zookeeper作为服务的配置中心 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2021最新zookeeper系列

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

Ⅰ:zookeeper的單機安裝 - 詳細教程:https://blog.csdn.net/Kevinnsm/article/details/116134397?spm=1001.2014.3001.5501

Ⅱ:zookeeper的相關shell命令:https://blog.csdn.net/Kevinnsm/article/details/116137602?spm=1001.2014.3001.5501

Ⅲ:zookeeper之查看節點的狀態信息:https://blog.csdn.net/Kevinnsm/article/details/116143218?spm=1001.2014.3001.5501

Ⅳ:zookeeper的acl權限控制:https://blog.csdn.net/Kevinnsm/article/details/116167394?spm=1001.2014.3001.5501

Ⅴ:zookeeper的相關Java Api:https://blog.csdn.net/Kevinnsm/article/details/116462557?spm=1001.2014.3001.5501

Ⅵ:zookeeper的Watcher事件監聽機制:https://blog.csdn.net/Kevinnsm/article/details/116501842?spm=1001.2014.3001.5501

Ⅶ:教你一招利用zookeeper作為服務的配置中心:https://blog.csdn.net/Kevinnsm/article/details/116542974?spm=1001.2014.3001.5501

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

文章目錄

  • 前置:--》把握住Watcher流程《--
  • 一、環境搭建
    • Ⅰ、使用工具
    • Ⅱ、創建節點
    • Ⅲ、代碼編寫


前置:–》把握住Watcher流程《–

1、連接zookeeper服務器
2、連接時必須使當前線程等待(等待其他線程創建連接zookeeper服務成功,使用計數器實現)
3、執行回調函數process
4、釋放當前線程

一、環境搭建

演示mysql的連接配置(將mysql連接屬性配置到zookeeper服務器)

Ⅰ、使用工具

使用云服務器作為zookeeper的演示環境,本地使用xshell遠程連接
如果不知,請移步:https://blog.csdn.net/Kevinnsm/article/details/116134397?spm=1001.2014.3001.5501

Ⅱ、創建節點

create /config “config”
create /config/mysql “datasource”
create /config/mysql/driver "com.mysql.cj.jdbc.Driver
create /config/mysql/url "jdbc:mysql://127.0.0.1/test?serverTimezone=UTC
create /config/mysql/username “root”
create /config/mysql/password “xxxxxxx”

Ⅲ、代碼編寫

package com.zookeeper.config;import com.zookeeper.watcher.WatcherConnection; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.junit.Test;import java.io.IOException; import java.util.concurrent.CountDownLatch;/*** @author:抱著魚睡覺的喵喵* @date:2021/5/8* @description:*/ public class ZookeeperConfigCenter implements Watcher { //mysql的本地變量private String driver;private String url;private String username;private String password;static ZooKeeper zooKeeper;static CountDownLatch countDownLatch = new CountDownLatch(1);final static String IP = "8.140.37.103:2181"; //構造函數,只要一創建該類,就執行getProperty()方法public ZookeeperConfigCenter() {getProperty();}//重寫回調函數@Overridepublic void process(WatchedEvent watchedEvent) {try {if (watchedEvent.getType() == Event.EventType.None) {if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {System.out.println("連接成功");//如果連接成功,放行當前線程countDownLatch.countDown();} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {System.out.println("連接中斷");} else if (watchedEvent.getState() == Event.KeeperState.Expired) {System.out.println("連接超時");//如果連接超時就重新進行連接zooKeeper = new ZooKeeper(IP, 5000, new WatcherConnection());} else if (watchedEvent.getState() == Event.KeeperState.AuthFailed) {System.out.println("驗證失敗");}//如果監聽到節點數據發生了變化,就重新連接讀取節點數據} else if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {getProperty();}} catch (IOException e) {e.printStackTrace();}} //核心方法public void getProperty() {try {//連接zookeeper服務zooKeeper = new ZooKeeper(IP, 20000, this);//等待其他線程連接zookeeper服務countDownLatch.await();this.driver = new String(zooKeeper.getData("/config/mysql/driver", true, null));this.url = new String(zooKeeper.getData("/config/mysql/url", true, null));this.username = new String(zooKeeper.getData("/config/mysql/username", true, null));this.password = new String(zooKeeper.getData("/config/mysql/password", true, null));} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (KeeperException e) {e.printStackTrace();}}@Testpublic void test() {try {ZookeeperConfigCenter configCenter = new ZookeeperConfigCenter();for (int i = 1; i < 6; i++) {System.out.println(configCenter.driver);System.out.println(configCenter.url);System.out.println(configCenter.username);System.out.println(configCenter.password);//休眠10秒Thread.sleep(10000);}} catch (Exception e) {e.printStackTrace();}} }

另外也用到了一個簡單的連接類作為zookeeper連接超時的回調處,然后進性重新連接(實現了watcher接口

public class WatcherConnection implements Watcher {static CountDownLatch countDownLatch = new CountDownLatch(1);//計數器static ZooKeeper zooKeeper;public static void main(String[] args) {try {zooKeeper = new ZooKeeper("123.57.252.59:2181", 5000, new WatcherConnection());countDownLatch.await();Thread.sleep(10000);zooKeeper.close();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}@Overridepublic void process(WatchedEvent watchedEvent) {try {if (watchedEvent.getType() == Event.EventType.None) {if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {System.out.println("連接成功!");countDownLatch.countDown();} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {System.out.println("斷開連接");} else if (watchedEvent.getState() == Event.KeeperState.Expired) {System.out.println("超時了");} else if (watchedEvent.getState() == Event.KeeperState.AuthFailed) {System.out.println("認證失敗!");}}} catch (Exception e) {e.printStackTrace();}}}

測試

可以看出當監聽到/config/mysql/username節點的數據發生變更時,控制臺就會重新獲取節點值

如果控制臺出現了ConnectionLoss錯誤,可能是網絡差導致連接斷開或者服務器宕機

總結

以上是生活随笔為你收集整理的Ⅶ:教你一招利用zookeeper作为服务的配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。