RabbitMQ/RabbitMQPublisher/TopicPub.cs
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
namespace RabbitMQPublisher
{
using System;
using System.Collections.Generic;
using System.Text;
using CommonLib.RabbitMQ;
using RabbitMQ.Client;
/// <summary>
/// 路由模式(topic),消息会发送到exchange
/// topic与direct模式区别在于routingKey可以声明为模糊查询,RabbitMQ拥有两个通配符
/// #:匹配0-n个字符语句
/// *:匹配一个字符语句
/// </summary>
static class TopicPublisher
{
private static readonly string exchangeName = $"test.exchange.topic";
static void Main(string[] args)
{
using RabbitMQHelper mq = new(new string[] { "192.168.181.191" });
mq.UserName = "guest";
mq.Password = "guest";
mq.Port = 5672;
while (true)
{
Console.WriteLine("消息发布者:模式{topic}=>输入消息内容");
string message = Console.ReadLine();
if (!string.IsNullOrEmpty(message))
{
// routingKey = "test.one.one":只有test.topic.queue2可以收到消息,因为#匹配0个或多个单词
// routingKey = "test.one" :两个queue都可以收到消息
string routingKey = "test.one.one";
mq.Publish(exchangeName, routingKey, message, new ExchangeQueueOptions
{
Type = RabbitMQExchangeType.Topic,
QueueAndRoutingKey = new List<Tuple<string, string>>() {
new Tuple<string, string>("test.topic.queue1", "test.*"),
new Tuple<string, string>("test.topic.queue2", "test.#")
}
});
#if rabbitMQClient
ConnectionFactory factory = BasePublisher.CreateRabbitMqConnection();
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
// 声明交换机
string exchangeName = $"test.exchange.topic";
channel.ExchangeDeclare(exchange: exchangeName, type: "topic");
// 声明队列
string queue1 = "test.topic.queue1";
channel.QueueDeclare(queue1, false, false, false, null);
后面还有 28 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
