Redis发布订阅PHP与Java案例

零、前言

Redis的发布订阅是个强大而实用的功能,本文分别在php端与Java端进行了测试。相对memcache而言,Redis支持数据持久化备份恢复、密码安全验证等功能,比较吸引人。

一、测试环境

环境 版本
操作系统 Win7 64bit
服务器 UPUPW NAK v1.1.4
PHP版本 5.6.32
Redis版本 2.8.2101

二、php 客户端

1、发布

<?php  
$redis = new Redis();  
$redis->connect('127.0.0.1', 6379);  
$message='新年快乐';  
$ret=$redis->publish('redisChat',$message); 

2、订阅

<?php
ini_set('default_socket_timeout', -1);  //不超时  
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);  
$result=$redis->subscribe(array('redisChat'), 'callback');  
function callback($instance,$channelName,$message){  
    echo $message;  
} 

3、对象发布

数组对象转化成Json字符串数据发布

<?php
//发布  
$redis = new Redis();  
$redis->connect('127.0.0.1', 6379);  
$message['msg']='新年快乐';
$message['time']=time();
$ret=$redis->publish('redisChat',json_encode($message));

php可发布消息,java订阅接受消息进行处理;不建议php订阅不然会造成程序和用户浏览阻塞。

三、Java客户端

1、订阅

SubTest01.java

public class SubTest01 {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        RedisMsgPubSubListener listener = new RedisMsgPubSubListener();
        jedis.subscribe(listener, "redisChat");
        //other code
    }
}

注意:subscribe是一个阻塞的方法,在取消订阅该频道前,会一直阻塞在这,只有当取消了订阅才会执行下面的other code。

RedisMsgPubSubListener.java

public class RedisMsgPubSubListener extends JedisPubSub {

    @Override  
    public void unsubscribe() {  
        super.unsubscribe();  
    }  

    @Override  
    public void unsubscribe(String... channels) {  
        super.unsubscribe(channels);  
    }  

    @Override  
    public void subscribe(String... channels) {  
        super.subscribe(channels);  
    }  

    @Override  
    public void psubscribe(String... patterns) {  
        super.psubscribe(patterns);  
    }  

    @Override  
    public void punsubscribe() {  
        super.punsubscribe();  
    }  

    @Override  
    public void punsubscribe(String... patterns) {  
        super.punsubscribe(patterns);  
    }  

    @Override  
    public void onMessage(String channel, String message) {  
        System.out.println("channel:" + channel + "receives message :" + message);  
//        this.unsubscribe();  
    }  

    @Override  
    public void onPMessage(String pattern, String channel, String message) {  

    }  

    @Override  
    public void onSubscribe(String channel, int subscribedChannels) {  
        System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);  
    }  

    @Override  
    public void onPUnsubscribe(String pattern, int subscribedChannels) {  

    }  

    @Override  
    public void onPSubscribe(String pattern, int subscribedChannels) {  

    }  

    @Override  
    public void onUnsubscribe(String channel, int subscribedChannels) {  
        System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);  
    }  
}

2、发布

PubTest01.java

public class PubTest01 {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("localhost");  
        jedis.publish("redisChat", "我是天才");  
        Thread.sleep(5000);  
        jedis.publish("redisChat", "我牛逼");  
        Thread.sleep(5000);  
        jedis.publish("redisChat", "哈哈");  
    }
}

3、测试效果图

这里写图片描述

注意:Redis不支持直接发布对象,需要JSON化

参考网址
redis可视化管理
https://www.cnblogs.com/aimu/p/7047671.html
PHP https://blog.csdn.net/gxinkai/article/details/54630516
PHP https://blog.csdn.net/u012946588/article/details/79014492
Java http://www.runoob.com/redis/redis-java.html
Java https://blog.csdn.net/u011734144/article/details/51782085
redis备份与恢复
http://www.runoob.com/redis/redis-backup.html
Redis为什么是单线程
https://blog.csdn.net/qqqqq1993qqqqq/article/details/77538202

posted @ 2018-04-09 00:35  Leytton  阅读(168)  评论(0编辑  收藏  举报