zookeeper的一致性配置管理

参考:https://blog.csdn.net/u013468915/article/details/80955110

 

配置:

public class CommonConfig implements Serializable{
	// 数据库连接配置
	private String dbUrl;
	private String username;
	private String password;
	private String driverClass;
	
	public CommonConfig() {}
	
	public CommonConfig(String dbUrl, String username, String password, String driverClass) {
		super();
		this.dbUrl = dbUrl;
		this.username = username;
		this.password = password;
		this.driverClass = driverClass;
	}
 
	public String getDbUrl() {
		return dbUrl;
	}
 
	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
	public String getDriverClass() {
		return driverClass;
	}
 
	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}
	
	@Override
	public String toString() {
		return "CommonConfig:{dbUrl:" + this.dbUrl +
				", username:" + this.username + 
				", password:" + this.password + 
				", driverClass:" + this.driverClass + "}";
	}
}

同步配置信息到Zookeeper服务器

public class ZkConfigMng {
	private String nodePath = "/commConfig";
	private CommonConfig commonConfig;
	private ZkClient zkClient;
	
	public CommonConfig initConfig(CommonConfig commonConfig) {
		if(commonConfig == null) {
			this.commonConfig = new CommonConfig("jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=utf-8",
					"root", "root", "com.mysql.jdbc.Driver");	
		} else {
			this.commonConfig = commonConfig;
		}
		return this.commonConfig;
	}
	
	/**
	 * 更新配置
	 * 
	 * @param commonConfig
	 * @return
	 */
	public CommonConfig update(CommonConfig commonConfig) {
		if(commonConfig != null) {
			this.commonConfig = commonConfig;
		}
		syncConfigToZookeeper();
		return this.commonConfig;
	}
	
	public void syncConfigToZookeeper() {
		if(zkClient == null) {
			zkClient = new ZkClient("127.0.0.1:2181");
		}
		if(!zkClient.exists(nodePath)) {
			zkClient.createPersistent(nodePath);
		}
		zkClient.writeData(nodePath, commonConfig);
	}
}

以上是提供者,下面我们需要一个客户端获取这些配置

public class ZkConfigClient implements Runnable {
	
	private String nodePath = "/commConfig";
	
	private CommonConfig commonConfig;
 
	@Override
	public void run() {
		ZkClient zkClient = new ZkClient(new ZkConnection("127.0.0.1:2181", 5000));
		while (!zkClient.exists(nodePath)) {
			System.out.println("配置节点不存在!");
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 获取节点
		commonConfig = (CommonConfig)zkClient.readData(nodePath);
		System.out.println(commonConfig.toString());
		zkClient.subscribeDataChanges(nodePath, new IZkDataListener() {
			
			@Override
			public void handleDataDeleted(String dataPath) throws Exception {
				if(dataPath.equals(nodePath)) {
					System.out.println("节点:" + dataPath + "被删除了!");
				}
			}
			
			@Override
			public void handleDataChange(String dataPath, Object data) throws Exception {
				if(dataPath.equals(nodePath)) {
					System.out.println("节点:" + dataPath + ", 数据:" + data + " - 更新");
					commonConfig = (CommonConfig) data;
				}
			}
		});
	}
 
}

 

 

下面启动Main函数:配置管理服务启动

public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(ZookeeperApiDemoApplication.class, args);
		
		ZkConfigMng zkConfigMng = new ZkConfigMng();
		zkConfigMng.initConfig(null);
		zkConfigMng.syncConfigToZookeeper();
		TimeUnit.SECONDS.sleep(10);
		
		// 修改值
		zkConfigMng.update(new CommonConfig("jdbc:mysql://192.168.1.122:3306/mydata?useUnicode=true&characterEncoding=utf-8",
				"root", "wxh", "com.mysql.jdbc.Driver"));
	}
}

客户端启动:

public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(ZookeeperApiDemoApplication.class, args);
 
		ExecutorService executorService = Executors.newFixedThreadPool(3);
		// 模拟多个客户端获取配置
		executorService.submit(new ZkConfigClient());
		executorService.submit(new ZkConfigClient());
		executorService.submit(new ZkConfigClient());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值