溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

分布式服務(wù)框架Zookeeper如何配置管理

發(fā)布時(shí)間:2021-12-07 14:37:19 來源:億速云 閱讀:222 作者:小新 欄目:云計(jì)算

這篇文章主要介紹了分布式服務(wù)框架Zookeeper如何配置管理,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

實(shí)現(xiàn)說明:

客戶端實(shí)現(xiàn)zookeeper的watcher事件,監(jiān)聽節(jié)點(diǎn)的更新事件,zookeeper管理的節(jié)點(diǎn)配置有更新時(shí),客戶端會(huì)接收到更新事件,并作出相應(yīng)的處理,本文代碼只是一個(gè)簡單的示例

服務(wù)端代碼示例

/**
 * 包名:com.lencee.demo.zookeeper.config
 * 文件名:ConfigManager.java
 * 版本信息:
 * 日期:2015年1月23日-下午1:28:55
 * 
 */
package com.lencee.demo.zookeeper.config;
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;
/**
 * 
 * <p>配置管理類<p>
 * <p>維護(hù)分布式配置</p>
 * @version 2015年1月23日 下午1:28:55
 * 
 */
public class ConfigManager {
 
 private static ConfigManager configManager = null;
 
 //Zookeeper集群服務(wù)地址與端口
 private static String zkUrl = "192.168.0.101:11001";
 //配置結(jié)點(diǎn)根路徑
 private final static String ROOT = "/myConf";
 //結(jié)點(diǎn)鑒權(quán)方式
 private final static String AUTH_TYPE = "digest";
 //結(jié)點(diǎn)鑒權(quán)密碼
 private final static String AUTH_PWD = "password";
 
 private ZooKeeper zk = null;
 
 private ConfigManager(){}
 
 public synchronized static ConfigManager getInstance() throws Exception{
  if (configManager == null) {
   configManager = new ConfigManager();
   ZooKeeper zk = new ZooKeeper(zkUrl, 3000, new Watcher() {
    @Override
    public void process(WatchedEvent event) {
     System.out.println("事件類型:" + event.getType());
    }
   });
   while (zk.getState() != ZooKeeper.States.CONNECTED) {
    Thread.sleep(3000);
   }
   
   //給這個(gè)鏈接添加認(rèn)證信息
   zk.addAuthInfo(AUTH_TYPE, AUTH_PWD.getBytes());
   
   configManager.setZk(zk);
   
   String rootValue = "測試環(huán)境配置";
   if(zk.exists(ROOT, true)==null){
    //結(jié)點(diǎn)不存在
    zk.create(ROOT , rootValue.getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
   }
  }
  return configManager;
 }
 
 public void addNode(String path,byte[] data,CreateMode createMode) throws Exception{
  if(!path.startsWith("/")){
   throw new Exception("傳入的路徑?jīng)]有以'/'開始");
  }
  if(this.zk.exists(ROOT + path, true)==null){
   //結(jié)點(diǎn)不存在
   this.zk.create(ROOT + path, data, Ids.CREATOR_ALL_ACL, createMode);
  }
 }
 public void setDate(String path,byte[] data) throws Exception{
  if(this.zk.exists(ROOT + path, true)==null){
   addNode(path, data, CreateMode.PERSISTENT);
  }else{
   zk.setData(ROOT + path, data, -1);
  }
  
 }
 /**
  * zk
  *
  * @return  the zk
  * @since   1.0.0
  */
 public ZooKeeper getZk() {
  return zk;
 }
 /**
  * @param zk the zk to set
  */
 public void setZk(ZooKeeper zk) {
  this.zk = zk;
 }
 
 public static void main(String[] args) throws Exception {
  ConfigManager cfm = ConfigManager.getInstance();
  
  //添加數(shù)據(jù)庫配置節(jié)點(diǎn)
  String path = "/mysql";
  String value = "測試環(huán)境Mysql配置";
  cfm.setDate(path, value.getBytes());
  
  //添加項(xiàng)目配置節(jié)點(diǎn)
  String octopusPath = "/mysql/octopus";
  String octopusValue = "資源系統(tǒng)";
  cfm.setDate(octopusPath, octopusValue.getBytes());
  
  //添加連接URL的配置
  String urlPath = "/mysql/octopus/url";
  String urlValue = "jdbc:mysql://test.xxx.com:3306/octopus?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
  cfm.setDate(urlPath, urlValue.getBytes());
  //添加連接數(shù)據(jù)庫的用戶名配置
  String userPath = "/mysql/octopus/username";
  String userValue = "octopus";
  cfm.setDate(userPath, userValue.getBytes());
  //添加連接數(shù)據(jù)庫的密碼配置
  String pwdPath = "/mysql/octopus/pwd";
  String pwdValue = "octopus111";
  cfm.setDate(pwdPath, pwdValue.getBytes());
 }
}

客戶端代碼

/**
 * 包名:com.lencee.demo.zookeeper.config
 * 文件名:ConfigClient.java
 * 版本信息:
 * 日期:2015年1月23日-下午2:15:49
 * 
 */
package com.lencee.demo.zookeeper.config;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
/**
 * 
 * <p>配置應(yīng)用端<p>
 * <p>讀取分布式配置</p>
 * @version 2015年1月23日 下午2:15:49
 * 
 */
public class ConfigClient implements Watcher{
 
 // Zookeeper集群服務(wù)地址與端口
 private static String zkUrl = "192.168.0.101:11001";
 // 配置結(jié)點(diǎn)根路徑
 private final static String ROOT = "/myConf";
 // 結(jié)點(diǎn)鑒權(quán)方式
 private final static String AUTH_TYPE = "digest";
 // 結(jié)點(diǎn)鑒權(quán)密碼
 private final static String AUTH_PWD = "password";
 private ZooKeeper zk = null;
 
 private String url;
 private String username;
 private String pwd;
 
 public ConfigClient() {
  try {
   ZooKeeper zk = new ZooKeeper(zkUrl, 3000,this);
   while (zk.getState() != ZooKeeper.States.CONNECTED) {
    Thread.sleep(3000);
    System.out.println();
   }
   
   //給這個(gè)鏈接添加認(rèn)證信息
   zk.addAuthInfo(AUTH_TYPE, AUTH_PWD.getBytes());
   this.zk = zk;
   //讀取服務(wù)端的配置
   reflushValue();
  } catch (Exception e) {
   e.printStackTrace();
  } 
 }
 /**
  * 
  * reflushValue:<p>將配置服務(wù)設(shè)置的配置更新到對(duì)象中</p> 
  * @since  1.0.0
  */
 public void reflushValue() {
  try {
   this.url = new String(this.zk.getData(ROOT + "/mysql/octopus/url", true, null));
   this.username = new String(this.zk.getData(ROOT + "/mysql/octopus/username", true, null));
   this.pwd = new String(this.zk.getData(ROOT + "/mysql/octopus/pwd", true, null));
  } catch (Exception e) {
   e.printStackTrace();
  } 
 }
 
 public void printValues(){
  System.out.println("----------------當(dāng)前配置---------------");
  System.out.println("mysql.url:"+this.url);
  System.out.println("mysql.username:"+this.username);
  System.out.println("mysql.pwd:"+this.pwd);
 }
 
 @Override
 public void process(WatchedEvent event) {
  EventType eventType = event.getType();
  if(Watcher.Event.EventType.None==eventType){
   System.out.println("事件:連接服務(wù)成功");
  }else if(Watcher.Event.EventType.NodeCreated==eventType){
   System.out.println("事件:節(jié)點(diǎn)創(chuàng)建成功");
  }else if(Watcher.Event.EventType.NodeChildrenChanged==eventType){
   System.out.println("事件:子節(jié)點(diǎn)更新成功");
   reflushValue();
   printValues();
  }else if(Watcher.Event.EventType.NodeDataChanged==eventType){
   System.out.println("事件:節(jié)點(diǎn)更新成功");
   reflushValue();
   printValues();
  }else if(Watcher.Event.EventType.NodeDeleted==eventType){
   System.out.println("事件:節(jié)點(diǎn)刪除成功");
  }
  
 }
 /**
  * url
  *
  * @return  the url
  * @since   1.0.0
  */
 public String getUrl() {
  return url;
 }
 /**
  * username
  *
  * @return  the username
  * @since   1.0.0
  */
 public String getUsername() {
  return username;
 }
 /**
  * pwd
  *
  * @return  the pwd
  * @since   1.0.0
  */
 public String getPwd() {
  return pwd;
 }
 public static void main(String[] args) throws Exception {
  ConfigClient cc = new ConfigClient();
  System.out.println("客戶端開始運(yùn)行"+cc);
  while(true){
   Thread.sleep(3000);
  }
 }
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“分布式服務(wù)框架Zookeeper如何配置管理”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI