Welcome 微信登录

首页 / 数据库 / MySQL / Redis发布订阅

启动订阅和发布客户端
在订阅客户端
redis 127.0.0.1:6379> PSUBSCRIBE shareReading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "share"
3) (integer) 1表示客户端订阅share通道其中1表示该客户端中连的接订阅通道数为1在发布客户端,为该通道发布一个消息
redis 127.0.0.1:6379> publish share "share"
(integer) 1其中1表示有1个连接接收到这个消息订阅客户端显示
1) "pmessage"//消息类型
2) "share"//我订阅的通道名
3) "share"//我接收的通道名
4) "share"//消息内容ps:另附java实现订阅代码:
  1. public static void main(String[] args) {  
  2.         String cmd = "subscribe share ";  
  3.         SocketChannel client = null;  
  4.         try {  
  5.             SocketAddress address = new InetSocketAddress("localhost"6379);  
  6.             client = SocketChannel.open(address);  
  7.             client.configureBlocking(false);// 设置为异步   
  8.             ByteBuffer buffer = ByteBuffer.allocate(100);  
  9.             buffer.put(cmd.getBytes());  
  10.             buffer.clear();  
  11.             client.write(buffer);  
  12.             System.out.println("发送数据: " + new String(buffer.array()));  
  13.   
  14.             while (true) {  
  15.                 buffer.flip();  
  16.                 int i = client.read(buffer);  
  17.                 if (i > 0) {  
  18.                     byte[] b = buffer.array();  
  19.                     System.out.println("接收数据: " + new String(b));  
  20.                     break;  
  21.                 }  
  22.             }  
  23.         } catch (Exception e) {  
  24.             try {  
  25.                 client.close();  
  26.             } catch (IOException e1) {  
  27.                 e1.printStackTrace();  
  28.             }  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
Redis数据类型及操作Redis配置文件redis.conf相关资讯      Redis配置 
  • Redis配置文件参数说明及命令操作  (今 12:05)
  • Linux下Redis主从配置  (05月18日)
  • redis配置详解(中英文)  (01月14日)
  • Redis集群配置笔记  (08月11日)
  • Linux服务器上Redis安装和配置  (04月08日)
  • Redis高级特性的配置及使用  (08/08/2015 13:08:56)
本文评论 查看全部评论 (0)
表情: 姓名: 字数

??

评论声明
    版权所有©石家庄振强科技有限公司2024 冀ICP备08103738号-5 网站地图