博客
关于我
Basic Tutorials of Redis(7) -Publish and Subscribe
阅读量:416 次
发布时间:2019-03-06

本文共 3923 字,大约阅读时间需要 13 分钟。

  This post is mainly about the publishment and subscription in Redis.I think you may subscribe some offiial

accounts on wechat,and when the authors published something new to their accounts,you will get them in

your wechat.The instance can make you understand this pattern easily.You will find there only 6 commands

for publishment and subscription.

   This post introduces the basic usages as well.I will show you how to publish a message to a channel.publish

is the command to publish sometings.I publish a channel named news-nba with the message nba.And the client

return me a integer 0.What is the meaning of the return value?It means that there is nobody subscribe this channel.

publish news-nba nba

   I open a new client for subscriber.In this client,I subscribe the above channel news-nba.It will return something

about this channel.

subscribe news-nba

   OK,let's publish a new message to the news-nba to see the changes in the both clients.After publishing lakers to

the news-nba channel,this client returns 1.It means that the channel has a subscriber.

publish news-nba lakers

   Let's turn to the subscriber's client.You will find the message lakers was already in the client.So amazing it is.

  

  Opening the second client to subscribe this channel,and you will find something similar with the above example.

  

   publish a new message to the news-nba,it returns 2 meaning ...(you understand it)

  

  the both subscribers' client are as follows:

  

  

   All right,let's see the other commands of this feature in Redis.Seeing the sql first:

 select * from channels where channelname like 'news%' To execute this sql,you will get all of the channel whoes 

name start with news.Redis can also do that to help us subscibe many channels by matching their name vaguely.

For example I want to subscribe all the channels of news.I will use psubscribe to do this job.

psubscribe news-*

  Let's publish some message to the others channel started with news- to find out the changes.
publish news-tech tech

   The subscriber's client will receive the message from the channel news-tech.

   Again!!Publishing a message to a new channel.

   As you can see,the client receives this message as well.

  
The client of Redis can not show you the unsubscription.So turning to the StackExchange.Redis.The

following code demonstrates the usage in C#.

1             //publisher 2             var sub = redis.GetSubscriber(); 3             //subscriber 4             var sub2 = redis.GetSubscriber(); 5              6             sub.Publish("news-nba", "nba"); 7             sub.Publish("news-tech", "tech"); 8             9             //sub10             sub2.Subscribe("news-nba", (channel, value) =>11             {12                 Console.WriteLine(string.Format("{0} has been published to {1}!",value,channel));13             });14             sub2.Subscribe("news-tech", (channel, value) =>15             {16                 Console.WriteLine(string.Format("{0} has been published to {1}!", value, channel));17             });18 19             sub.Publish("news-nba", "Lakers");20             sub.Publish("news-nba", "Kobe");21 22             sub.Publish("news-tech", "lumia");23 24             System.Threading.Thread.Sleep(2000);25             Console.WriteLine("unscribe the news-nba");26             sub2.Unsubscribe("news-nba");//unsub27 28             sub.Publish("news-tech", "surface pro3");29 30             System.Threading.Thread.Sleep(2000);31             sub.Publish("news-nba", "james");32 33             System.Threading.Thread.Sleep(2000);34             Console.WriteLine("unscribe all channels");35             sub2.UnsubscribeAll();36             37             sub.Publish("news-nba", "paul");38             sub.Publish("news-tech", "surface pro4");
  Debuging the code ,you will see the result as follow. 

  

  The next post of this series is the basic opreation of transaction in Redis.Thanks for your reading.

转载地址:http://prxkz.baihongyu.com/

你可能感兴趣的文章
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_处理器介绍_处理过程说明---大数据之Nifi工作笔记0019
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_实际操作---大数据之Nifi工作笔记0020
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_实际操作_02---大数据之Nifi工作笔记0032
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_操作方法说明_01_EvaluteJsonPath处理器---大数据之Nifi工作笔记0031
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_使用NIFI表达式语言_来获取自定义属性中的数据_NIFI表达式使用体验---大数据之Nifi工作笔记0024
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_内嵌ZK模式集群2_实际操作搭建NIFI内嵌模式集群---大数据之Nifi工作笔记0016
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_实际操作_03---大数据之Nifi工作笔记0035
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_01---大数据之Nifi工作笔记0033
查看>>
NIFI大数据进阶_实时同步MySql的数据到Hive中去_可增量同步_实时监控MySql数据库变化_操作方法说明_02---大数据之Nifi工作笔记0034
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>