Redis的订阅发布机制

来源:互联网 发布:td-scdma知乎 编辑:程序博客网 时间:2024/06/05 21:17

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
 在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.util.Properties;  
  4.   
  5. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;  
  6. import org.apache.log4j.Logger;  
  7.   
  8. import redis.clients.jedis.JedisCluster;  
  9. import redis.clients.jedis.JedisPool;  
  10.   
  11. public class JedisTemplate {  
  12.     private static Logger logger = Logger.getLogger(JedisCluster.class);  
  13.   
  14.     private static String redisPropertiesName = "redis-configures.properties";  
  15.     private static String redisHost = "redis.host";  
  16.     private static String redisPort = "redis.port";  
  17.     private static String maxTotalKey = "redis.maxtotal";  
  18.     private static String maxIdleKey = "redis.maxIdle";  
  19.   
  20.     private static Properties redisProperties;  
  21.     private static JedisPool jedisPool;  
  22.   
  23.     static {  
  24.         redisProperties = new Properties();  
  25.         InputStream inputStream = JedisTemplate.class.getResourceAsStream("/" + redisPropertiesName);  
  26.         if (null == inputStream) {  
  27.             throw new RuntimeException("no configures found: " + redisPropertiesName);  
  28.         }  
  29.         try {  
  30.             redisProperties.load(inputStream);  
  31.         } catch (IOException e) {  
  32.             logger.error(e.getMessage(), e);  
  33.             throw new RuntimeException("load redis configures failed...");  
  34.         }  
  35.         load();  
  36.     }  
  37.   
  38.     private static void load() {  
  39.         GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();  
  40.   
  41.         String maxTotal = redisProperties.getProperty(maxTotalKey);  
  42.         String maxIdle = redisProperties.getProperty(maxIdleKey);  
  43.   
  44.         redisConfig.setMaxTotal(Integer.valueOf(maxTotal));  
  45.         redisConfig.setMinIdle(Integer.valueOf(maxIdle));  
  46.         redisConfig.setMaxIdle(Integer.valueOf(maxIdle));  
  47.   
  48.         String host = redisProperties.getProperty(redisHost);  
  49.         String port = redisProperties.getProperty(redisPort);  
  50.         try {  
  51.             jedisPool = new JedisPool(redisConfig, host, Integer.valueOf(port));  
  52.         } catch (Exception e) {  
  53.             throw new RuntimeException("init redis failed: " + e.getMessage());  
  54.         }  
  55.     }  
  56.   
  57.     public static JedisPool getJedisPool() {  
  58.         return jedisPool;  
  59.     }  
  60.   
  61. }  

常见数据操作

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.Map;  
  2.   
  3. import redis.clients.jedis.Jedis;  
  4. import redis.clients.jedis.JedisPool;  
  5.   
  6. public class RedisCacheImpl{  
  7.     private static JedisPool jedisPool;  
  8.   
  9.     static {  
  10.         jedisPool = JedisTemplate.getJedisPool();  
  11.     }  
  12.   
  13.     public void set(String key, String value) {  
  14.         try (Jedis jedis = jedisPool.getResource()) {  
  15.             jedis.set(key, value);  
  16.         }  
  17.     }  
  18.   
  19.     public void expire(String key, int seconds) {  
  20.         try (Jedis jedis = jedisPool.getResource()) {  
  21.             jedis.expire(key, seconds);  
  22.         }  
  23.   
  24.     }  
  25.   
  26.     public String get(String key) {  
  27.         try (Jedis jedis = jedisPool.getResource()) {  
  28.             return jedis.get(key);  
  29.         }  
  30.     }  
  31.   
  32.     public void remove(String key) {  
  33.         try (Jedis jedis = jedisPool.getResource()) {  
  34.             jedis.del(key);  
  35.         }  
  36.     }  
  37.   
  38. }  


第三步:订阅与发布机制总结

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】??

0 0
原创粉丝点击