首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >哪个Java客户端支持Redis的分片+ pubsub?

哪个Java客户端支持Redis的分片+ pubsub?
EN

Stack Overflow用户
提问于 2015-03-26 09:19:59
回答 3查看 1.5K关注 0票数 1

有没有支持Redis分片和pubsub的Java客户端?我看了看绝地武士,它似乎缺乏这种支持。

EN

回答 3

Stack Overflow用户

发布于 2015-03-26 18:15:59

TLDR

  1. Jedis确实支持发布/订阅,我实际上正在使用它。
  2. 集群是一个Redis功能,Jedis最初支持

Jedis发布/订阅

请检查jedis pub/sub docs以获取扩展引用。作为一个简单的示例,您可以尝试执行以下操作:

代码语言:javascript
运行
复制
class MyListener extends JedisPubSub {
        public void onMessage(String channel, String message) {
        }

        public void onSubscribe(String channel, int subscribedChannels) {
        }

        public void onUnsubscribe(String channel, int subscribedChannels) {
        }

        public void onPSubscribe(String pattern, int subscribedChannels) {
        }

        public void onPUnsubscribe(String pattern, int subscribedChannels) {
        }

        public void onPMessage(String pattern, String channel,
            String message) {
        }
}

MyListener l = new MyListener();

jedis.subscribe(l, "foo");

群集

对于集群示例,首先,阅读redis cluster tutorial以了解这是否符合您的需求是个好主意。

Jedis为集群提供了一个初始API,但由于Redis功能本身正在进行中,因此这可能会随着时间的推移而改变:

代码语言:javascript
运行
复制
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNodes);
jc.set("foo", "bar");
String value = jc.get("foo");

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2015-06-12 21:47:39

使用Jedis集群实现pubsub特性的两种棘手方法。

  1. 创建一个集群,并从其中随机获取两个节点。一个用于pub,一个用于sub。限制是我们不知道节点什么时候会关闭,我们必须定期检查发布/订阅连接的可用性,并从群集节点获取新的连接。

代码语言:javascript
运行
复制
  //create cluster
  HashSet<HostAndPort> node = new HashSet<>();
  node.add(new HostAndPort("127.0.0.1", 7000));
  final JedisClusterPubSub cluster = new JedisClusterPubSub(node);

  //get all nodes
  Map<String, JedisPool> nodes = cluster.getClusterNodes();

  //pick two nodes from the cluster
  final JedisPool pool = nodes.values().iterator().next();
  final JedisPool pool2 = nodes.values().iterator().next();

  // publish 
  new Thread(new Runnable() {
    @Override
    public void run() {

      try {
        while (true) {
          Thread.sleep(3000);
          pool.getResource().publish("channel", "message");
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }).start();

  //subscribe
  pool2.getResource().subscribe(new RequestListener(), "channel");

  1. 破解JedisClusterPubSub,对其进行扩展并实现发布/订阅选项。这是不安全的方式。

代码语言:javascript
运行
复制
public class JedisClusterPubSub extends JedisCluster{
  public JedisClusterPubSub(Set<HostAndPort> nodes) {
    super(nodes);
  }

  public void subscribe(final JedisPubSub pubSub, final String ... channels){
    try {
      Field fieldCH = JedisCluster.class.getDeclaredField("connectionHandler");
      fieldCH.setAccessible(true);
      JedisClusterConnectionHandler connectionHandler = (JedisClusterConnectionHandler)fieldCH.get(this);

      Field fieldMR = JedisCluster.class.getDeclaredField("maxRedirections");
      fieldMR.setAccessible(true);

      int maxRedirections = (int)fieldMR.get(this);

      new JedisClusterCommand<Void>(connectionHandler, maxRedirections) {
        @Override
        public Void execute(Jedis connection) {
          connection.subscribe(pubSub, channels);
          return null;
        }
      }.run("meaningless");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void publish(final String channel, final String message){
    try {
      Field fieldCH = JedisCluster.class.getDeclaredField("connectionHandler");
      fieldCH.setAccessible(true);
      JedisClusterConnectionHandler connectionHandler = (JedisClusterConnectionHandler)fieldCH.get(this);

      Field fieldMR = JedisCluster.class.getDeclaredField("maxRedirections");
      fieldMR.setAccessible(true);

      int maxRedirections = (int)fieldMR.get(this);

      new JedisClusterCommand<Void>(connectionHandler, maxRedirections) {
        @Override
        public Void execute(Jedis connection) {
          connection.publish(channel, message);
          return null;
        }
      }.run("meaningless");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
票数 2
EN

Stack Overflow用户

发布于 2015-03-26 18:12:04

你应该看看redisson:

https://github.com/mrniko/redisson

它不是对redis的简单使用,更像是它的一个框架。它确实支持集群。绝地武士也支持发布/订阅。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29269469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档