Redis 教程 | 菜鸟教程
Redis 命令参考(红色)
Redis 命令参考(蓝色)
Redis 数据库学习教程
Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。
Redis 客户端可以订阅任意数量的频道。
订阅者(subscriber):如客户端
发布者(publisher):如服务器
频道(channel):频道
消息
通信模型
● RedisServer中可以创建若干channel
● 一个订阅者可以订阅多个channel
● 当发布者向一个频道中发布一条消息时,所有的订阅者都将会收到消息
● Redis的发布订阅模型没有消息积压功能,即新加入的订阅者收不到发布者之前发布的消息
● 当订阅者收到消息时,消息内容如下
● 第一行:固定内容message
● 第二行:channel的名称
● 第三行:收到的新消息
发布订阅的 API
Publish:发布消息
PUBLISH channel message 将信息发送到指定的channel(频道)
Subscribe:订阅消息
SUBSCRIBE channel [channel ...] 订阅一个或多个频道
UnSubscribe:取消订阅消息
UNSUBSCRIBE channel [channel ...] 取消订阅一个或多个频道
Redis 模式订阅 PSUBSCRIBE/PUNSUBSCRIBE
● PSUBSCRIBE pattern [pattern ...] 订阅一个或多个符合pattern给定模式的频道
● PUNSUBSCRIBE pattern [pattern ...] 取消订阅一个或多个频道
● PUBSUB subcommand [argument [argument ...]] 查看订阅与发布系统状态
支持的模式有:?,*,[abc],[^abc],[a-b]
● h?llo matches hello,hallo and hxllo 匹配包含 h 与 llo 之间,有“一个”任意字符 的频道
● h*llo matches hllo and heeeello 匹配包含 h 与 llo 之间,有任意“多个”字符 的频道
● h[ae]llo matches hello and hallo,but not hillo 匹配包含 h 与 llo 之间,是a 或者 e 的频道
● h[^e]llo matches hallo,hbllo,... but not hello 匹配包含 h 与 llo 之间,有一个字母并且不能是 e 的频道
● h[a-b]llo matches hallo and hbllo 匹配包含 h 与 llo 之间,a 到 b 之间的频道
查询订阅状态
PUBSUB CHANNELS [pattern]:返回符合模式(未指定情况下返回所有)的频道
PUBSUB NUMSUB [channel - 1 ... channel - N]:返回指定的频道的订阅数
PUBSUB NUMPAT:返回所有服务器中被订阅的模式数目
*、发布、订阅、取消订阅
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
namespace RedisTestApp
{
public partial class PublishForm : Form
{
public readonly string channelFirst = "channel-1";
public static PooledRedisClientManager prcm;
public static IRedisClientsManager redisClientManager;
public static IRedisClient redisClient;
public PublishForm()
{
InitializeComponent();
}
private void PublishForm_Load(object sender, EventArgs e)
{
//public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[]
//{
// //如果是Redis集群则配置多个{IP地址:端口号}即可
// //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
// "127.0.0.1:6379"
//});
if (prcm == null)
{
prcm = CreateManager(
new string[] { "127.0.0.1:6379" },
new string[] { "127.0.0.1:6379" });
}
prcm.GetClient();
}
/// <summary>
/// 发布
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPublish_Click(object sender, EventArgs e)
{
string message = $"hello {DateTime.Now}";
using (IRedisClient client = prcm.GetClient())
{
// 发布
client.PublishMessage(channelFirst, message);
}
}
/// <summary>
/// 订阅
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSubscription_Click(object sender, EventArgs e)
{
string message = $"hello {DateTime.Now}";
using (IRedisClient client = prcm.GetClient())
{
//创建订阅
IRedisSubscription subscription = client.CreateSubscription();
//接受到消息时的委托
subscription.OnMessage = (channel, msg) =>
{
textBox1.AppendText($"频道【{channel}】订阅客户端接收消息:{msg} [{DateTime.Now}] {Environment.NewLine}{Environment.NewLine}");
textBox1.AppendText($"订阅数:{subscription.SubscriptionCount} {Environment.NewLine}{Environment.NewLine}");
};
//订阅事件处理
subscription.OnSubscribe = channel =>
{
textBox1.AppendText($"订阅客户端:开始订阅 {channel} {Environment.NewLine}{Environment.NewLine}");
};
subscription.SubscribeToChannels(channelFirst);
}
}
/// <summary>
/// 取消订阅
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUnSubscribe_Click(object sender, EventArgs e)
{
using (IRedisClient client = prcm.GetClient())
{
//创建订阅
IRedisSubscription subscription = client.CreateSubscription();
//取消订阅事件处理
subscription.OnUnSubscribe = a =>
{
textBox1.AppendText($"订阅客户端:取消订阅 {Environment.NewLine}{Environment.NewLine}");
};
//注意:订阅信道的时候 会开启阻塞模式,所以,需要将监听放到单独的线程里
subscription.SubscribeToChannels(channelFirst);
}
}
#region redis
public PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
new RedisClientManagerConfig
{
MaxWritePoolSize = 5,//“写”链接池链接数
MaxReadPoolSize = 5,//“读”链接池链接数
AutoStart = true,
});
}
#endregion
}
}
*
*
*