需求
- 创建一个Web项目,将数据库连接信息交给Zookeeper配置中心管理,即:当项目Web项目启动时,从
Zookeeper进行MySQL配置参数的拉取 - 要求项目通过数据库连接池访问MySQL(连接池可以自由选择熟悉的)
- 当Zookeeper配置信息变化后Web项目自动感知,正确释放之前连接池,创建新的连接池
思路
1.启动程序,使用默认的配置项zk中创建永久节点,并使用该配置连接数据库
2.使用zk监听文件内容变化,当数据变化时,释放之前的连接,使用新的配置进行连接
3.手动变更永久节点的值,查看日志,看连接是否发生改变
代码:
package com.lagou.zk;
import cn.hutool.json.JSONUtil;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ZkJdbc {
private static JDBCParam jdbcParam = new JDBCParam("jdbc:mysql://linux133:3306/test?characterEncoding=utf-8&useSSL=false","root","12345678");
private static Connection con =null;
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient = new ZkClient("linux131:2181");
System.out.println("zkclient is ready");
zkClient.setZkSerializer(new ZkStrSerializer());
getNewConnection();
boolean exists = zkClient.exists("/jdbc");
if(!exists){
zkClient.createPersistent("/jdbc",JSONUtil.toJsonStr(jdbcParam));
}
zkClient.subscribeDataChanges("/jdbc", new IZkDataListener() {
public void handleDataChange(String path, Object o) throws Exception {
System.out.println(path+"data is changed"+o.toString());
String s = o.toString();
JDBCParam newJdbc = JSONUtil.toBean(s, JDBCParam.class);
jdbcParam.setPassword(newJdbc.getPassword());
jdbcParam.setName(newJdbc.getName());
jdbcParam.setUrl(newJdbc.getUrl());
getNewConnection();
}
//数据节点删除
public void handleDataDeleted(String path) throws Exception {
System.out.println(path+"data is delete");
}
});
Thread.sleep(Integer.MAX_VALUE);
}
public static void getNewConnection(){
try {
if(con!=null){
con.close();
}
con = DriverManager.getConnection(jdbcParam.getUrl() , jdbcParam.getName() , jdbcParam.getPassword());
System.out.println(String.format("连接成功:host:%s name:%s password:%s",jdbcParam.getUrl() , jdbcParam.getName() , jdbcParam.getPassword()));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
配置类:
package com.lagou.zk;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import org.I0Itec.zkclient.ZkClient;
public class JDBCParam {
private String url;
private String name;
private String password;
public JDBCParam(String url, String name, String password) {
this.url = url;
this.name = name;
this.password = password;
}
public JDBCParam() {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static void main(String[] args) {
JDBCParam jdbcParam = new JDBCParam("jdbc:mysql://linux133:3306/azkaban?characterEncoding=utf-8&useSSL=false", "azkaban", "azkaban");
System.out.println(JSONUtil.toJsonStr(jdbcParam));
}
}
序列化
package com.lagou.zk;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
public class ZkStrSerializer implements ZkSerializer {
//序列化
public byte[] serialize(Object o) throws ZkMarshallingError {
return String.valueOf(o).getBytes();
}
//反序列化
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
return new String(bytes);
}
}
修改节点数据
set /jdbc {"url":"jdbc:mysql://linux133:3306/azkaban?characterEncoding=utf-8&useSSL=false","password":"azkaban","name":"azkaban"}
结果