为什么要用统一配置?
我们做项目时用到的配置比如数据库配置等...我们都是写死在项目里面,如果需要更改,那么也是的修改配置文件然后再投产上去,那么问题来了,如果做集群的呢,有100台机器,这时候做修改那就太不切实际了;那么就需要用到统一配置管理啦。
解决思路
1.把公共配置抽取出来
2.对公共配置进行维护
3.修改公共配置后应用不需要重新部署
采用方案
1.公共配置抽取存放于zookeeper中并落地数据库
2.对公共配置修改后发布到zookeeper中并落地数据库
3.对应用开启配置实时监听,zookeeper配置文件一旦被修改,应用可实时监听到并获取
代码
- pom.xml
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
- ZkConfig
package com.qibo.base.controller.zookeeper;
import java.io.Serializable;
public class ZkConfig implements Serializable {
private String userNm;
private String userPw;
public ZkConfig() {
}
public ZkConfig(String userNm, String userPw) {
this.userNm = userNm;
this.userPw = userPw;
}
public String getUserNm() {
return userNm;
}
public void setUserNm(String userNm) {
this.userNm = userNm;
}
public String getUserPw() {
return userPw;
}
public void setUserPw(String userPw) {
this.userPw = userPw;
}
@Override
public String toString() {
return "ZkConfig [userNm=" + userNm + ", userPw=" + userPw + "]";
}
}
- ZkConfigMag (写入)
package com.qibo.base.controller.zookeeper;
import org.I0Itec.zkclient.ZkClient;
public class ZkConfigMag {
private ZkConfig config;
/**
* 从数据库加载配置
*/
public ZkConfig downLoadConfigFromDB() {
// getDB
config = new ZkConfig("steven", "123456");
return config;
}
/**
* 配置文件上传到数据库
*/
public void upLoadConfigToDB(String nm, String pw) {
if (config == null)
config = new ZkConfig();
config.setUserNm(nm);
config.setUserPw(pw);
// updateDB
}
/**
* 配置文件同步到zookeeper
*/
public void syncConfigToZk() {
ZkClient zk = new ZkClient("192.168.10.10:2181");
if (!zk.exists("/zkConfig")) {
zk.createPersistent("/zkConfig", true);
}
zk.writeData("/zkConfig", config);
zk.close();
}
}
- ZkGetConfigClient(读出)
package com.qibo.base.controller.zookeeper;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
public class ZkGetConfigClient {
public ZkConfig config;
public ZkConfig getConfig() {
ZkClient zk = new ZkClient("192.168.10.10:2181");
config = (ZkConfig) zk.readData("/zkConfig");
System.out.println("加载到配置:" + config.toString());
// 监听配置文件修改
zk.subscribeDataChanges("/zkConfig", new IZkDataListener() {
@Override
public void handleDataChange(String arg0, Object arg1) throws Exception {
config = (ZkConfig) arg1;
System.out.println("监听到配置文件被修改:" + config.toString());
}
@Override
public void handleDataDeleted(String arg0) throws Exception {
config = null;
System.out.println("监听到配置文件被删除");
}
});
return config;
}
}
- ZkController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ZkController {
@RequestMapping("/zkSet")
public void zkSet() {
ZkConfigMag mag = new ZkConfigMag();
mag.downLoadConfigFromDB();
mag.syncConfigToZk();
}
@RequestMapping("/zkGet")
public void zkGet() {
ZkGetConfigClient client = new ZkGetConfigClient();
client.getConfig();
System.out.println(client.config.toString());
}
}
- zk客户端查看
zkCli.sh -server 192.168.10.10:2181