首页 技术 正文
技术 2022年11月17日
0 收藏 973 点赞 2,573 浏览 3213 个字

分布式系统中经常涉及到配置资源的管理,比如,一个应用系统需要部署在多台服务器上,但是他们拥有某些的配置项是相同的,如果配置变更,需要修改这些配置,那么需要同时修改每台服务器,这样做比较麻烦而且容易出错。类似于这样的资源配置管理完全可以交给zookeeper进行管理,将需要配置的资源保存在zookeeper,当配置数据变化时,zookeeper会将变化信息发布到所有侦听zookeeper的服务器,服务器上的应用就可以从zookeeper中获取最新的配置信息。本文主要介绍如何通过Curator架构,使用zookeeper构建一个简单的配置资源管理系统。

系统架构

本文示例如下图所示,系统由三个部分组成,server端,zookeeper端和controller端,server端包含三个运行相同配置app的server,监听zookeeper中的conf节点。controller端用于操作zookeeper中的数据节点,当用户通过controller更新conf节点,三个server都会收到NodeDataChange消息。

Code

为演示简单起见,Serevr1, Srever2, Server3运行的代码一致,代码主要包含以下几个部分:

1.zookeeper需要存储的数据bean,这里我们使用json对bean 对象进行序列化。

package com.zte.zoo.keeper.configuration;/**
* Created by majun on 15/10/29.
*/
public class ConfigBean {
private String url;
private String port;
private String name;
private String pwd; public String getUrl() {
return url;
} public ConfigBean setUrl(String url) {
this.url = url;
return this;
} public String getPort() {
return port;
} public ConfigBean setPort(String port) {
this.port = port;
return this;
} public String getPwd() {
return pwd;
} public ConfigBean setPwd(String pwd) {
this.pwd = pwd;
return this;
} public String getName() {
return name;
} public ConfigBean setName(String name) {
this.name = name;
return this;
} @Override
public String toString() {
return GsonUtils.toJson(this);
}
}

2.Server端运行的app code如下所示,主函数使用curatorwatch监听zookeeper的conf节点,当收到数据变化事件通知,打印

public class Server{
public static void main(String[] args) {
private final String serverName="app2";
final CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
client.start();
try {
client.getData().usingWatcher(new CuratorWatcher() {
public void process(WatchedEvent watchedEvent) throws Exception {
if (Watcher.Event.EventType.NodeDataChanged == watchedEvent.getType()) {
String data = new String(client.getData().forPath(watchedEvent.getPath()));
System.out.println(serverName + " recv data:" + GsonUtils.fromJson(data, ConfigBean.class));
}
}
}).forPath("/conf");
} catch (Exception e) {
e.printStackTrace();
}
while (true) ;
}}

3.Controller端运行的控制代码,更新zookeeper中具有PERSISTENT属性的conf节点

public class Controller {
public static void main(String[] args) throws Exception {
CuratorFramework newClient = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
newClient.start();
ConfigBean configBean = new ConfigBean()
.setUrl("192.168.1.1")
.setPort("8000")
.setName("majun")
.setPwd("123");
// createNode(newClient, "/conf", CreateMode.PERSISTENT, configBean.toString()); updateNodeDate(newClient,"/conf",configBean.setName("xiaoya--").toString());
}
public static void createNode(CuratorFramework newClient, String path, CreateMode createMode, String data) {
try {
newClient.create().withMode(createMode).forPath(path, data.getBytes());
} catch (Exception e) {
System.out.println("创建节点失败, elog=" + e.getMessage());
}
}
public static void updateNodeDate(CuratorFramework newClient, String path, String data) {
try {
newClient.setData().forPath(path, data.getBytes());
} catch (Exception e) {
System.out.println("更新节点数据失败, elog=" + e.getMessage());
}
}}

4.json序列化工具类

public class GsonUtils {
private static Gson gson = new Gson(); public static String toJson(Object obj) {
return gson.toJson(obj);
}
public static Object fromJson(String json, Class cla) {
return gson.fromJson(json, cla);
}
}

Test

启动zookeeper, 使用controller中的创建节点函数创建conf节点,然后分别启动三个server,使用controller中的更新函数更新节点,会在三个server打印的更新数据信息。

Conclusion

本文和大家分享了如何基于curator框架使用zookeeper构建统一资源管理系统,希望能够对大家有所帮助。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898