二十、curator recipes之NodeCache

简介 

Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听。

官方文档:http://curator.apache.org/curator-recipes/node-cache.html

javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/cache/NodeCache.html

代码示例

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class NodeCacheDemo {
    private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 1));
    private static String path = "/nodeCache/test/0001";
    static {
        client.start();
    }

    public static void main(String[] args) throws Exception {
        if (client.checkExists().forPath(path) == null) {
            System.out.println("not exist");
            client.create().creatingParentsIfNeeded().forPath(path);
        }
        System.out.println("created");
        NodeCache nodeCache = new NodeCache(client, path, false);
        nodeCache.getListenable().addListener(() -> System.out.println("nodeChanged"));
        System.out.println("add listener");
        nodeCache.start(true);
        System.out.println("cache started");
        client.setData().forPath(path, "lay".getBytes());
        System.out.println("set data");
        client.delete().forPath(path);
        System.out.println("deleted");
        Thread.sleep(3000);
        nodeCache.close();
        System.out.println("listener closed");
        Thread.sleep(50000);
        client.close();
    }
}

 

转载于:https://www.cnblogs.com/lay2017/p/10280238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Curator提供了一个名为ConnectionStateManager的接口,它可以用于管理连接状态。我们可以使用它来实现在Curator注册的MySQL连接节点中存储实际的连接对象或连接状态。 以下是一个基本的实现代码: ```java import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.NodeCache; import org.apache.curator.framework.recipes.cache.NodeCacheListener; import org.apache.curator.framework.recipes.nodes.PersistentNode; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySQLConnectionStateManager implements ConnectionStateListener, NodeCacheListener { private final CuratorFramework curatorFramework; private final String nodePath; private final String connectionString; private final String username; private final String password; private final NodeCache nodeCache; private Connection connection; public MySQLConnectionStateManager(CuratorFramework curatorFramework, String nodePath, String connectionString, String username, String password) { this.curatorFramework = curatorFramework; this.nodePath = nodePath; this.connectionString = connectionString; this.username = username; this.password = password; this.nodeCache = new NodeCache(curatorFramework, nodePath); } public void start() throws Exception { // 创建持久节点 PersistentNode persistentNode = new PersistentNode(curatorFramework, PersistentNode.Mode.EPHEMERAL, nodePath); persistentNode.start(); // 添加连接状态监听器 curatorFramework.getConnectionStateListenable().addListener(this); // 添加节点数据监听器 nodeCache.getListenable().addListener(this); // 启动节点数据监听器 nodeCache.start(); // 初始化连接 connection = createConnection(nodeCache.getCurrentData()); } public void close() throws IOException { if (connection != null) { try { connection.close(); } catch (SQLException e) { // 处理关闭连接时发生的异常 } } nodeCache.close(); } private Connection createConnection(ChildData childData) throws SQLException { if (childData == null) { return null; } String connectionString = new String(childData.getData()); return DriverManager.getConnection(connectionString, username, password); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { switch (newState) { case CONNECTED: case RECONNECTED: try { // 连接恢复,重新创建连接对象 connection = createConnection(nodeCache.getCurrentData()); } catch (SQLException e) { // 处理连接创建时发生的异常 } break; case SUSPENDED: case LOST: // 连接中断或丢失,关闭连接对象 if (connection != null) { try { connection.close(); } catch (SQLException e) { // 处理关闭连接时发生的异常 } connection = null; } break; default: break; } } @Override public void nodeChanged() throws Exception { // 节点数据变化,重新创建连接对象 connection = createConnection(nodeCache.getCurrentData()); } } ``` 使用示例: ```java CuratorFramework curatorFramework = ...; // 创建CuratorFramework实例 String nodePath = "/mysql/connection"; // MySQL连接节点路径 String connectionString = "jdbc:mysql://localhost:3306/mydb"; // MySQL连接字符串 String username = "root"; // MySQL用户名 String password = "password"; // MySQL密码 MySQLConnectionStateManager connectionStateManager = new MySQLConnectionStateManager(curatorFramework, nodePath, connectionString, username, password); connectionStateManager.start(); // 启动连接状态管理器 // ...使用连接对象进行数据库操作 connectionStateManager.close(); // 关闭连接状态管理器 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值