Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure and manage the distributed Service Framework Zookeeper

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the distributed service framework Zookeeper configuration management, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Implementation description:

The client implements the watcher event of zookeeper and listens for the update event of the node. When the node managed by zookeeper is configured with updates, the client will receive the update event and deal with it accordingly. The code in this article is just a simple example.

Server code example

/ * package name: com.lencee.demo.zookeeper.config * File name: ConfigManager.java * version Information: * date: January 23, 2015-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;/** * *

Configuration management class

*

Maintain distributed configuration

* @ version 1:28:55 on January 23rd, 2015 * * / public class ConfigManager {private static ConfigManager configManager = null; / / Zookeeper cluster service address and port private static String zkUrl = "192.168.0.101 virtual 11001"; / / configure node root path private final static String ROOT = "/ myConf"; / / node authentication method private final static String AUTH_TYPE = "digest"; / / node authentication password 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 Type: + event.getType ();}}); while (zk.getState ()! = ZooKeeper.States.CONNECTED) {Thread.sleep (3000) } / / add authentication information to this link zk.addAuthInfo (AUTH_TYPE, AUTH_PWD.getBytes ()); configManager.setZk (zk); String rootValue = "test environment configuration"; if (zk.exists (ROOT, true) = = null) {/ / node does not exist 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 ("incoming path does not start with'/'");} if (this.zk.exists (ROOT + path, true) = = null) {/ / node does not exist 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 (); / / add database configuration node String path = "/ mysql"; String value = "test environment Mysql configuration"; cfm.setDate (path, value.getBytes ()) / / add project configuration node String octopusPath = "/ mysql/octopus"; String octopusValue = "resource system"; cfm.setDate (octopusPath, octopusValue.getBytes ()); / / add configuration String urlPath = "/ mysql/octopus/url" for connecting 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 ()) / / add user name configuration String userPath = "/ mysql/octopus/username"; String userValue = "octopus"; cfm.setDate (userPath, userValue.getBytes ()); / / add password configuration String pwdPath = "/ mysql/octopus/pwd"; String pwdValue = "octopus111"; cfm.setDate (pwdPath, pwdValue.getBytes ());}}

Client code

/ * package name: com.lencee.demo.zookeeper.config * File name: ConfigClient.java * version Information: * date: January 23, 2015-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;/** * *

Configure the application side

*

Read distributed configuration

* @ version 2:15:49 on January 23rd, 2015 * * / public class ConfigClient implements Watcher {/ / Zookeeper cluster service address and port private static String zkUrl = "192.168.0.101 virtual 11001"; / / configure node root path private final static String ROOT = "/ myConf"; / / node authentication method private final static String AUTH_TYPE = "digest"; / / node authentication password 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); while (zk.getState ()! = ZooKeeper.States.CONNECTED) {Thread.sleep (3000); System.out.println ();} / / add authentication information zk.addAuthInfo (AUTH_TYPE, AUTH_PWD.getBytes ()) to this link; this.zk = zk / / read the server configuration reflushValue ();} catch (Exception e) {e.printStackTrace ();}} / * reflushValue:

Update the configuration of the configuration service settings to the object

* @ 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 (ROOT + "/ mysql/octopus/pwd", true, null);} catch (Exception e) {e.printStackTrace () }} public void printValues () {System.out.println ("- current configuration -"); 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 ("event: connection service successful");} else if (Watcher.Event.EventType.NodeCreated==eventType) {System.out.println ("event: node created successfully");} else if (Watcher.Event.EventType.NodeChildrenChanged==eventType) {System.out.println ("event: child node updated successfully"); reflushValue () PrintValues ();} else if (Watcher.Event.EventType.NodeDataChanged==eventType) {System.out.println ("event: node updated successfully"); reflushValue (); printValues ();} else if (Watcher.Event.EventType.NodeDeleted==eventType) {System.out.println ("event: node deleted successfully");}} / * * 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 ("client starts running" + cc) While (true) {Thread.sleep (3000);} Thank you for reading this article carefully. I hope the article "how to configure and manage the distributed Service Framework Zookeeper" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report