十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了分布式服务框架Zookeeper如何配置管理,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联主要从事成都做网站、网站制作、成都外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务江川,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
实现说明:
客户端实现zookeeper的watcher事件,监听节点的更新事件,zookeeper管理的节点配置有更新时,客户端会接收到更新事件,并作出相应的处理,本文代码只是一个简单的示例
服务端代码示例
/** * 包名: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; /** * *配置管理类
*
维护分布式配置
* @version 2015年1月23日 下午1:28:55 * */ public class ConfigManager { private static ConfigManager configManager = null; //Zookeeper集群服务地址与端口 private static String zkUrl = "192.168.0.101:11001"; //配置结点根路径 private final static String ROOT = "/myConf"; //结点鉴权方式 private final static String AUTH_TYPE = "digest"; //结点鉴权密码 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); } //给这个链接添加认证信息 zk.addAuthInfo(AUTH_TYPE, AUTH_PWD.getBytes()); configManager.setZk(zk); String rootValue = "测试环境配置"; if(zk.exists(ROOT, true)==null){ //结点不存在 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("传入的路径没有以'/'开始"); } if(this.zk.exists(ROOT + path, true)==null){ //结点不存在 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(); //添加数据库配置节点 String path = "/MySQL"; String value = "测试环境Mysql配置"; cfm.setDate(path, value.getBytes()); //添加项目配置节点 String octopusPath = "/mysql/octopus"; String octopusValue = "资源系统"; 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()); //添加连接数据库的用户名配置 String userPath = "/mysql/octopus/username"; String userValue = "octopus"; cfm.setDate(userPath, userValue.getBytes()); //添加连接数据库的密码配置 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; /** * *配置应用端
*
读取分布式配置
* @version 2015年1月23日 下午2:15:49 * */ public class ConfigClient implements Watcher{ // Zookeeper集群服务地址与端口 private static String zkUrl = "192.168.0.101:11001"; // 配置结点根路径 private final static String ROOT = "/myConf"; // 结点鉴权方式 private final static String AUTH_TYPE = "digest"; // 结点鉴权密码 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(); } //给这个链接添加认证信息 zk.addAuthInfo(AUTH_TYPE, AUTH_PWD.getBytes()); this.zk = zk; //读取服务端的配置 reflushValue(); } catch (Exception e) { e.printStackTrace(); } } /** * * reflushValue:将配置服务设置的配置更新到对象中
* @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("----------------当前配置---------------"); 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("事件:连接服务成功"); }else if(Watcher.Event.EventType.NodeCreated==eventType){ System.out.println("事件:节点创建成功"); }else if(Watcher.Event.EventType.NodeChildrenChanged==eventType){ System.out.println("事件:子节点更新成功"); reflushValue(); printValues(); }else if(Watcher.Event.EventType.NodeDataChanged==eventType){ System.out.println("事件:节点更新成功"); reflushValue(); printValues(); }else if(Watcher.Event.EventType.NodeDeleted==eventType){ System.out.println("事件:节点删除成功"); } } /** * 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("客户端开始运行"+cc); while(true){ Thread.sleep(3000); } } }
感谢你能够认真阅读完这篇文章,希望小编分享的“分布式服务框架Zookeeper如何配置管理”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!