您的位置:首页 > 数据库 > Redis

Redis的订阅发布机制

2017-02-14 18:24 447 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。

Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted
set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

第一步:Redis服务器端(Linux环境)安装
1、下载Redis安装包
访问Redis官网(http://redis.io/download)下载合适的版本。解压安装包,命令为tar -zxvf redis-3.*.*.tar.gz。

2、安装tcl
安装命令为 yum install tcl

3、安装gcc
安装命令为yum install gcc

4、安装Redis
安装命令为cd src && make all,安装过程中有时会报“error:jemalloc/jemalloc.h...”错误,在控制台执行一下“make MALLOC=libc”命令就行了。具体请参看参考文档2。
5、启动(关闭)Redis
启动命令为src/redis-server  redis.conf,如果为了避免退出启动窗口也退出服务,可以将redis.conf文件中的daemonize no 设置为打开(yes)。
关闭命令为src/redis-cli shutdown。
第二步:Redis池创建并数据常见操作

[java] view
plain copy

 





import java.io.IOException;  

import java.io.InputStream;  

import java.util.Properties;  

  

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;  

import org.apache.log4j.Logger;  

  

import redis.clients.jedis.JedisCluster;  

import redis.clients.jedis.JedisPool;  

  

public class JedisTemplate {  

    private static Logger logger = Logger.getLogger(JedisCluster.class);  

  

    private static String redisPropertiesName = "redis-configures.properties";  

    private static String redisHost = "redis.host";  

    private static String redisPort = "redis.port";  

    private static String maxTotalKey = "redis.maxtotal";  

    private static String maxIdleKey = "redis.maxIdle";  

  

    private static Properties redisProperties;  

    private static JedisPool jedisPool;  

  

    static {  

        redisProperties = new Properties();  

        InputStream inputStream = JedisTemplate.class.getResourceAsStream("/" + redisPropertiesName);  

        if (null == inputStream) {  

            throw new RuntimeException("no configures found: " + redisPropertiesName);  

        }  

        try {  

            redisProperties.load(inputStream);  

        } catch (IOException e) {  

            logger.error(e.getMessage(), e);  

            throw new RuntimeException("load redis configures failed...");  

        }  

        load();  

    }  

  

    private static void load() {  

        GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();  

  

        String maxTotal = redisProperties.getProperty(maxTotalKey);  

        String maxIdle = redisProperties.getProperty(maxIdleKey);  

  

        redisConfig.setMaxTotal(Integer.valueOf(maxTotal));  

        redisConfig.setMinIdle(Integer.valueOf(maxIdle));  

        redisConfig.setMaxIdle(Integer.valueOf(maxIdle));  

  

        String host = redisProperties.getProperty(redisHost);  

        String port = redisProperties.getProperty(redisPort);  

        try {  

            jedisPool = new JedisPool(redisConfig, host, Integer.valueOf(port));  

        } catch (Exception e) {  

            throw new RuntimeException("init redis failed: " + e.getMessage());  

        }  

    }  

  

    public static JedisPool getJedisPool() {  

        return jedisPool;  

    }  

  

}  

常见数据操作

[java] view
plain copy

 





import java.util.Map;  

  

import redis.clients.jedis.Jedis;  

import redis.clients.jedis.JedisPool;  

  

public class RedisCacheImpl{  

    private static JedisPool jedisPool;  

  

    static {  

        jedisPool = JedisTemplate.getJedisPool();  

    }  

  

    public void set(String key, String value) {  

        try (Jedis jedis = jedisPool.getResource()) {  

            jedis.set(key, value);  

        }  

    }  

  

    public void expire(String key, int seconds) {  

        try (Jedis jedis = jedisPool.getResource()) {  

            jedis.expire(key, seconds);  

        }  

  

    }  

  

    public String get(String key) {  

        try (Jedis jedis = jedisPool.getResource()) {  

            return jedis.get(key);  

        }  

    }  

  

    public void remove(String key) {  

        try (Jedis jedis = jedisPool.getResource()) {  

            jedis.del(key);  

        }  

    }  

  

}  

第三步:订阅与发布机制总结
Redis 通过 PUBLISH 、 SUBSCRIBE 和 PSUBSCRIBE 等命令实现发布和订阅功能。这些命令被广泛用于构建即时通信应用,比如网络聊天室(chatroom)和实时广播、实时提醒(比如滴滴快的等打车软件的抢单功能)等。
1、订阅频道命令
SUBSCRIBE channelName
2、发布命令

PUBLISH channelName message
3、模式订阅命令(Redis支持模式匹配订阅,*为模糊匹配符)
PSUBSCRIBE news.*  订阅以new.开头的所有频道
4、取消普通订阅和模式订阅的命令
UNSUBSCRIBE   bar

PUNSUBSCRIBE  ba*

补充:取消在官方提供的连接工具中无法模拟的。
5、查看订阅信息(Redis2.8以上版本才有的命令)
pubsub channels [pattern]
//返回当前服务器被订阅的所有频道

参考文档:
1、http://blog.csdn.NET/miyatang/article/details/47257209
 Redis安装与配置
2、http://www.phperz.com/article/14/1219/42002.html Redis安装报错error:jemalloc/jemalloc.h...解决办法
3、http://lhj0206.blog.163.com/blog/static/12378679201402483948901/  Linux下Redis服务器安装配置
4、http://blog.csdn.Net/freebird_lb/article/details/7778959
 Redis发布/订阅
5、http://www.cnblogs.com/mushroom/p/4470006.html  Redis发布订阅及客户端编程

此文章来自于【http://blog.csdn.net/tongdao/article/details/50284339】??
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Redis