当前位置:网站首页>How to use redis' publish subscribe (MQ) in.Netcore 3.1 project
How to use redis' publish subscribe (MQ) in.Netcore 3.1 project
2022-07-24 08:44:00 【Bird of paradise in the wind】
1.Redis Publish subscribe ( understand )
Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels .
The picture below shows the channel channel1 , And three clients that subscribe to this channel —— client2 、 client5 and client1 The relationship between :
When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :
2. Turn on Redis service ( Virtual system CentOS7 Up operation )

3. dotnetCore 3.1 Project main code
1. Subscribers ( Get message , Perform some operations according to the message )
using System;
using StackExchange.Redis;
namespace Sub
{
class Program
{
static void Main(string[] args)
{
// Create connection
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.153.131:6379"))
{
ISubscriber sub = redis.GetSubscriber();
// The subscription is called messages The passage of
sub.Subscribe("redisChat", (channel, message) => {
// Output received messages
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {message}");
if (message == "yes")
{
todoSomething();
}
else
{
Console.WriteLine(" The specified information was not obtained , Do nothing !");
}
});
Console.WriteLine(" Subscribed redisChat");
Console.ReadKey();
}
}
/// <summary>
/// Define what you want to do
/// </summary>
public static void todoSomething()
{
Console.WriteLine(" Do what you want to do ....");
Console.WriteLine(" You have finished your operation .");
}
}
}
- Publisher ( Release the news , Trigger source )
using System;
using StackExchange.Redis;
namespace Pub
{
class Program
{
static void Main(string[] args)
{
// Create connection
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.153.131:6379"))
{
ISubscriber sub = redis.GetSubscriber();
Console.WriteLine(" Please enter any character , Input exit sign out ");
string input;
do
{
input = Console.ReadLine();
sub.Publish("redisChat", input);
} while (input != "exit");
}
}
}
}
Required in the project NuGet introduce StackExchange package :
4.redis Disadvantages of message publishing and subscription
The publisher and the acceptor must be online at the same time , Otherwise, there will be Missing information . For example, the receiving end is disconnected , During the offline period, the messages published by the publisher will not be cached until the receiver goes online , But if you miss it completely, there will be no more . So? , For those that want to use MQ Complete important business processing , This is not enough , After all redis The message mechanism in is not professional . Professionals will automatically move to rabbitMQ And other professional message oriented middleware .
There is another drawback , It is said that too many news releases will cause Jam so that it collapses . This is not tested . Also said this bug It has been fixed in the new version .
边栏推荐
- M-dao creates a one-stop Dao platform, allowing hundreds of millions of players to join Dao space
- 4、 Midway integrates swagger and supports JWT bearers
- 【一起上水硕系列】June总结+no 焦虑+July计划+如何考试+如何提升
- Aquanee: the true meaning of "p2e"
- Is gamefi in decline or in the future?
- Figure storage geabase
- 3587. Connected graph (Jilin University postgraduate entrance examination machine test question)
- How difficult is it to build a digital collection platform?
- SOA and microservice examples
- Rk3566 add project under external
猜你喜欢

Realize page return to parent directory based on cookies

"Problem solution" with score

3587. Connected graph (Jilin University postgraduate entrance examination machine test question)

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao

JMX Console 未授权访问漏洞

Play to earn: a new and more promising game paradigm in the future

Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案

Encryption market ushers in a new historical cycle. Look at jpex's "stability" and "health"

Treap

Confusing defer, return, named return value, anonymous return value in golang
随机推荐
Is gamefi in decline or in the future?
林业调查巡检数据采集解决方案
Hack the box - Web requests module detailed Chinese tutorial
Move protocol global health declaration, step into Web3 in sports
Xiaobai learns oauth2
Four data interaction modes of go grpc
Will Plato become the risk target of the meta universe? Platofarm has great opportunities
JS problem summary
2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway
Hack the box - Introduction to networking module detailed Chinese tutorial
SOA and microservice examples
Precautions for using kettle excel input
Grpc learning notes
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
Some mistakes that Xiaobai often makes (update from time to time, and promise not to make them again next time)
Kotlin学习笔记1——变量、函数
Online lover
Shell script backup mongodb database
Treap
Meta tags let search engines search your website