首页 > 其他 > 详细

zookeeper典型应用场景总结

时间:2020-03-13 21:08:49      阅读:79      评论:0      收藏:0      [点我收藏+]

一、一致性配置

  在集群环境下,挨个更改配置是比较繁琐的,使用zookeeper可以实现同步配置。

技术分享图片

 

1、配置信息

技术分享图片
 1 package com.zk;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 模拟公共配置类
 7  * 
 8  * @author Zomi
 9  */
10 public class DbConfig implements Serializable{
11     private static final long serialVersionUID = -4483388642208582886L;
12     
13     // 数据库配置,有默认值
14     private String url = "jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=utf-8";
15     private String username = "root";
16     private String password = "root";
17     private String driverClass = "com.mysql.jdbc.Driver";
18 
19 
20     public DbConfig(String url, String username, String password, String driverClass) {
21         this.url = url;
22         this.username = username;
23         this.password = password;
24         this.driverClass = driverClass;
25     }
26 
27     public DbConfig() {}
28     
29     public String getUrl() {
30         return url;
31     }
32 
33     public void setUrl(String url) {
34         this.url = url;
35     }
36 
37     public String getUsername() {
38         return username;
39     }
40 
41     public void setUsername(String username) {
42         this.username = username;
43     }
44 
45     public String getPassword() {
46         return password;
47     }
48 
49     public void setPassword(String password) {
50         this.password = password;
51     }
52 
53     public String getDriverClass() {
54         return driverClass;
55     }
56 
57     public void setDriverClass(String driverClass) {
58         this.driverClass = driverClass;
59     }
60 
61     @Override
62     public String toString() {
63         return "CommConfig [url=" + url + ", username=" + username + ", password=" + password + ", driverClass="
64                 + driverClass + "]";
65     }
66 
67 }
View Code

2、配置管理服务

技术分享图片
 1 package com.zk;
 2 
 3 import org.I0Itec.zkclient.ZkClient;
 4 
 5 /**
 6  * zk配置管理服务器,用于将配置信息的修改同步到zk上
 7  * @author Zomi
 8  */
 9 public class ZkConfigMng {
10     private String nodePath = "/dbConfig";
11     private DbConfig dbConfig;
12     private ZkClient zkClient;
13     
14     public ZkConfigMng() {
15         this.zkClient = new ZkClient("192.168.31.130:2181");
16     }
17     //更新配置
18     public DbConfig update(DbConfig dbConfig) {
19         this.dbConfig = dbConfig;
20         syncConfigToZookeeper();//将配置变更同步给zk
21         return dbConfig;
22     }
23     private void syncConfigToZookeeper() {
24         if(!zkClient.exists(nodePath)) {
25             zkClient.createPersistent(nodePath);
26         }
27         zkClient.writeData(nodePath, dbConfig);
28     }
29 }
View Code

3、模拟应用服务集群,具备监听配置变更的功能

技术分享图片
 1 package com.zk;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.I0Itec.zkclient.IZkDataListener;
 6 import org.I0Itec.zkclient.ZkClient;
 7 import org.I0Itec.zkclient.ZkConnection;
 8 
 9 /**
10  * 模拟多服务器
11  * 
12  * @author Zomi
13  */
14 public class ZkConfigClient implements Runnable {
15 
16     private String nodePath = "/dbConfig";
17     private DbConfig dbConfig;
18     
19     @Override
20     public void run() {
21         ZkClient zkClient = new ZkClient(new ZkConnection("192.168.31.130:2181", 5000));
22         while (!zkClient.exists(nodePath)) {
23             System.out.println("配置节点不存在!");
24             try {
25                 TimeUnit.SECONDS.sleep(1);
26             } catch (InterruptedException e) {
27                 e.printStackTrace();
28             }
29         }
30         dbConfig = (DbConfig)zkClient.readData(nodePath);
31         System.out.println(Thread.currentThread().toString() +"原数据为=="+ dbConfig);
32         
33         //监听配置数据的改变
34         zkClient.subscribeDataChanges(nodePath, new IZkDataListener() {
35             @Override
36             public void handleDataDeleted(String dataPath) throws Exception {
37                 System.out.println(Thread.currentThread().toString() + "监听到节点:" + dataPath + "被删除了!");
38             }
39             @Override
40             public void handleDataChange(String dataPath, Object data) throws Exception {
41                 System.out.println(Thread.currentThread().toString() + "监听到节点:" + dataPath + ", 数据:" + data + " - 更新");
42                 dbConfig = (DbConfig) data;
43             }
44         });
45     }
46 }
View Code
技术分享图片
 1 package com.zk;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class BusinessServers {
 7     public static void main(String[] args) {
 8         ExecutorService executorService = Executors.newFixedThreadPool(3);
 9         // 模拟多个服务器获取配置
10         executorService.submit(new ZkConfigClient());
11         executorService.submit(new ZkConfigClient());
12         executorService.submit(new ZkConfigClient());
13     }
14 }
View Code

4、更新配置信息进行测试

技术分享图片
 1 package com.zk;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 public class MngServer {
 6 
 7     public static void main(String[] args) throws InterruptedException {
 8         
 9         DbConfig dbConfig = new DbConfig();
10         ZkConfigMng zkConfigMng = new ZkConfigMng();
11         DbConfig xx = zkConfigMng.update(dbConfig);//初始化节点及数据
12         System.out.println(xx);
13         
14         TimeUnit.SECONDS.sleep(10);
15         
16         // 修改值        
17         zkConfigMng.update(new DbConfig("jdbc:mysql://192.168.6.6:3306/mydata?"
18                 + "useUnicode=true&characterEncoding=utf-8","admin", "admin", "com.mysql.jdbc.Driver"));
19         
20     }
21 
22 }
View Code

5、测试结果

技术分享图片

 

zookeeper典型应用场景总结

原文:https://www.cnblogs.com/zomicc/p/12488758.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!