1. 程式人生 > >Redis 發布/訂閱模式

Redis 發布/訂閱模式

eal ole valid ets console all [] nco 訂閱

一.命令簡介

1.PSUBSCRIBE 訂閱一個或多個符合給定模式的頻道。
2.PUBLISH 將信息 message 發送到指定的頻道 channel 。
3.PUBSUB 是一個查看訂閱與發布系統狀態的內省命令, 它由數個不同格式的子命令組成
4.PUNSUBSCRIBE 指示客戶端退訂所有給定模式。
5.SUBSCRIBE 訂閱給定的一個或多個頻道的信息。
6.UNSUBSCRIBE 指示客戶端退訂給定的頻道。

二.例子

1.訂閱msg

技術分享

2.發送信息

技術分享

技術分享

三.代碼實現

using Newtonsoft.Json;
using StackExchange.Redis;
using
System; namespace ResdisPubSub.PubSub { /// <summary> /// 通過redis實現的訂閱-發布機制 /// </summary> public class RedisSubscribe : ISubscribeService { //鏈接 static ConnectionMultiplexer redis; static RedisSubscribe() { ConfigurationOptions config
= new ConfigurationOptions() { AbortOnConnectFail = false, ConnectRetry = 10, ConnectTimeout = 5000, ResolveDns = true, SyncTimeout = 5000, EndPoints = { { "127.0.0.1:6379" } }, Password
= "111111", AllowAdmin = true, KeepAlive = 180 }; redis = ConnectionMultiplexer.Connect(config); } /// <summary> /// 發布消息 /// </summary> /// <typeparam name="T">消息類型</typeparam> /// <param name="channel">頻道:消息的名稱</param> /// <param name="msg">消息內容</param> /// <returns></returns> public void Publish<T>(string channel, T msg) { try { if (redis != null && redis.IsConnected) { redis.GetSubscriber().Publish(channel, JsonConvert.SerializeObject(msg)); } } catch (InvalidOperationException ex) { Console.WriteLine("redis服務錯誤,詳細信息:" + ex.Message + ",來源:" + ex.Source); } } /// <summary> /// 訂閱消息 /// </summary> /// <param name="subChannel">頻道:消息的名稱</param> /// <param name="action">收到消息後的處理</param> public void Subscribe(string subChannel, Action<string> action) { try { if (redis != null && redis.IsConnected) { redis.GetSubscriber().Subscribe(subChannel, (channel, message) => { action(message); }); } } catch (InvalidOperationException ex) { Console.WriteLine("redis服務錯誤,詳細信息:" + ex.Message + ",來源:" + ex.Source); } } /// <summary> /// 取消訂閱 /// </summary> /// <param name="channel">頻道:消息的名稱</param> public void Unsubscribe(string channel) { try { if (redis != null && redis.IsConnected) { redis.GetSubscriber().Unsubscribe(channel); } } catch (InvalidOperationException ex) { Console.WriteLine("redis服務錯誤,詳細信息:" + ex.Message + ",來源:" + ex.Source); } } /// <summary> /// 取消全部訂閱 /// </summary> public void UnsubscribeAll() { try { if (redis != null && redis.IsConnected) { redis.GetSubscriber().UnsubscribeAll(); } } catch (InvalidOperationException ex) { Console.WriteLine("redis服務錯誤,詳細信息:" + ex.Message + ",來源:" + ex.Source); } } } }

    class Program
    {
        static ISubscribeService client = new RedisSubscribe();
        static void Main(string[] args)
        {
            client.Subscribe("bigbigChannel", m => { Console.WriteLine($"我是bigbigChannel,接收到信息:{m}"); });
            Thread t = new Thread(Run);
            t.Start();
        }

        static void Run()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1000);
                client.Publish("bigbigChannel", i.ToString());
            }
       
        }
    }

技術分享

源碼下載:https://github.com/lgxlsm/ResdisPubSub

Redis 發布/訂閱模式