C# Redis系列(三)-Redis發布訂閱及用戶端編程

來源:互聯網
上載者:User

標籤:

  1. 發布訂閱模型
  2. Redis中的發布訂閱
  3. 用戶端編程樣本
  4. 0.3版本Hredis
發布訂閱模型

在應用級其作用是為了減少依賴關係,通常也叫觀察者模式。主要是把耦合點單獨抽離出來作為第三方,隔離易變化的發送方和接收方。

發送方:只負責向第三方發送訊息。(雜誌社把讀者雜誌交給郵局)
接收方:被動接收訊息。(1:向郵局訂閱讀者雜誌,2:門口去接郵過來的雜誌)
第三方作用是:儲存訂閱雜誌的接收方,並在雜誌過來時送給接收方。 (郵局)

C#樣本,發送方把雜誌放到郵局裡面:

    if (QA.AddBug())            EmailNotify();
接收方到郵局登記地址,有雜誌過來時送貨上門:
    EmailNotify += () => { Console.WriteLine("A君"); };    EmailNotify += () => { Console.WriteLine("B君"); };

第三方郵局接受讀者雜誌訂閱,收到雜誌時進行派送:

    public delegate void MessageHandler();    public static event MessageHandler  EmailNotify;        if (QA.AddBug())            EmailNotify();

當我們把觀察者模式放大到系統級時,就是發布訂閱(pub/sub)了。 主要是用來降低發行者和訂閱者的耦合,提高前端系統輸送量。結構

Redis中的發布訂閱

Redis實現完整的發布訂閱範式,就是說任何一台redis伺服器,啟動後都可以當做發布訂閱伺服器。

普通訂閱

啟動訂閱者client。

redis-cli.exe -h 127.0.0.1 -p 6379

訂閱bar頻道。格式:SUBSCRIBE name1 name2。
成功訂閱回複,分別對應訂閱類型、訂閱頻道、訂閱數量。

127.0.0.1:6379> SUBSCRIBE barReading messages... (press Ctrl-C to quit)1) "subscribe"2) "bar"3) (integer) 1

新起個發行者client,發送訊息。格式:publish channelName Message。

127.0.0.1:6379> publish bar val(integer) 1

訂閱client回複,分別對應訊息類型,頻道,訊息。

1) "message"2) "bar"3) "val"

圖例

模式訂閱

Redis支援模式比對訂閱,*為模糊比對符。
訂閱所有頻道的訊息

PSUBSCRIBE *  

訂閱以news.開頭的所有頻道。

PSUBSCRIBE news.*
取消訂閱

取消普通訂閱和取消模式訂閱的命令。

UNSUBSCRIBE  barPUNSUBSCRIBE  ba*

取消在官方提供的串連工具中無法類比的。

查看訂閱資訊

查看訂閱訊息是redis在2.8中心增加的命令之一。

pubsub channels [pattern] 。

返回當前伺服器被訂閱的所有頻道。

127.0.0.1:6379> pubsub channels1) "bar"

指定匹配參數,返回與模式比對的所有頻道。

127.0.0.1:6379> pubsub channels ba*1) "bar"
pubsub numsub [channel-1 channel-2 ...channel-n]

接受任意多個頻道作為輸入參數,返回這些頻道的訂閱者數量。

127.0.0.1:6379> pubsub numsub  bar bar21) "bar"2) (integer) 13) "bar2"4) (integer) 0
用戶端編程樣本
            RedisPubSub client = new RedisPubSub("127.0.0.1", 6381);            client.OnUnSubscribe += (obj) => {                Console.WriteLine();            };            client.OnMessage = (sender, arcgs) =>{                Console.WriteLine(arcgs);            };            client.OnError = (Exception) => {                 Console.WriteLine(Exception.Message);            };            client.Subscribe("bar");            Console.ReadLine();
0.3版本HRedis基本使用
    using (RedisClient client = new RedisClient("127.0.0.1", 6381))        {            client.Set("key", "value");            client.Get("key");        }
使用串連池,自動回收串連。
        PoolRedisClient prc = new PoolRedisClient(new PoolConfiguration());        prc.Single.Set("key", "value");        prc.Single.Get("key");
及上面的訂閱。

 

開源地址 https://github.com/mushroomsir/HRedis

C# Redis系列(三)-Redis發布訂閱及用戶端編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.