Redis的订阅发布机制
来源:互联网 发布:s7 200 编程电缆 原装 编辑:程序博客网 时间:2024/06/05 09:40
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池创建并数据常见操作
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; }}
常见数据操作
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发布订阅及客户端编程
- Redis的订阅发布机制
- Redis的订阅发布机制
- redis 发布订阅机制
- Redis的事务机制和订阅发布
- redis发布与订阅机制
- Redis的发布订阅
- Redis的发布订阅
- Redis的发布订阅
- Redis的发布订阅
- redis的发布订阅
- Redis 发布/订阅机制原理分析
- Redis Pub发布/sub订阅实现机制
- redis利用pipline实现发布订阅机制
- Redis 发布/订阅机制原理分析
- Redis 的订阅和发布
- Redis的订阅与发布
- redis的发布/订阅模式
- 基于redis的发布订阅
- MySQL解压版安装及编码设置
- 常用linux命令
- CPP 6th——类(8)
- HDU 1056 HangOver 模拟 水题
- android api兼容/被加 的level 查询
- Redis的订阅发布机制
- 变(2)
- [ACM]判断正多边形
- Xcode 及 iOS 常用宏和常量
- 微服务架构在云端的应用
- Week5-7Dependency parsing
- hdu 5596GTW likes gt(最好倒着思考)
- Service Intent must be explitict
- android开发之SnackBar的使用