Zookeeper: 数据发布订阅

Zookeeper几大典型应用场景

1.数据发布订阅

2.负载均衡

3.命令服务

4.分布式协调/通知

5.Master选举

6.分布式锁

7.分布式队列

我想每个应用场景均用实例来做一遍加深自己的影响。

本次就写实例:数据发布订阅 .

需求:将一个jdbc的配置放在zookeeper集群下,通过发布订阅,来动态获取jdbc配置。

JDBC配置类

**
 * jdbc 配置的信息
 */
public class JdbcConfig {

    private String ip;

    private int port;

    private String username;

    private String password;

    private int maxConnect;


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    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 int getMaxConnect() {
        return maxConnect;
    }

    public void setMaxConnect(int maxConnect) {
        this.maxConnect = maxConnect;
    }

    @Override
    public String toString() {
        return "JdbcConfig{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", maxConnect=" + maxConnect +
                '}';
    }

序列化类.

public class MyZkSerializer implements ZkSerializer{


    public byte[] serialize(Object data) throws ZkMarshallingError {
        try {
            return String.valueOf(data).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

发布订阅类.

public class ZkSubScribe {

    /**
     * 节点名称,配置jdbc
     */
    private final String JDBC_CONFIG = "/jdbc_config" ;

    private final int CONNECT_TIMEOUT = 3000;

    private final int SESSION_TIMEOUT = 3000;

    private final String SERVERS = "172.16.168.200:2181,172.16.168.200:2182,172.16.168.200:2183";

    ZkClient zk;

    @Before
    public void init(){
        zk =  new ZkClient(SERVERS,SESSION_TIMEOUT,CONNECT_TIMEOUT,new MyZkSerializer());
        //判断节点是否存在 不存在就创建
        boolean isExist = zk.exists(JDBC_CONFIG);
        if(!isExist){
            JdbcConfig  config = new JdbcConfig();
            config.setIp("172.16.168.201");
            config.setPort(3306);
            config.setPassword("admin");
            config.setPassword("admin");
            config.setMaxConnect(100);
            zk.createPersistent(JDBC_CONFIG,config);
        }
    }

    @Test
    public void subscribe(){

        System.out.println(zk.readData(JDBC_CONFIG));
        zk.subscribeDataChanges(JDBC_CONFIG, new IZkDataListener() {
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("the date change");
                System.out.println("the path is "+dataPath);
                System.out.println("the new data is "+data);
                //这里可以进行后续的jdbc配置管理操作
            }

            public void handleDataDeleted(String dataPath) throws Exception {

            }
        });

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void changeData(){
        System.out.println("prepare to change data....");
        JdbcConfig newConfig = new JdbcConfig();
        newConfig.setIp("172.16.168.200");
        newConfig.setPort(3306);
        newConfig.setUsername("root");
        newConfig.setPassword("root");
        newConfig.setMaxConnect(100);
        zk.writeData(JDBC_CONFIG,newConfig);
    }

效果
www.nooringinal.com 不原创网

集中配置管理就是通过Zookeeper的发布订阅功能发来处理。当我们zookeeper管理的节点信息发生改变后,发送给订阅者。

你可能感兴趣的:(架构,zookeeper,负载均衡,分布式,数据发布订阅系统)