住房和城乡建设部网站电话,网站建设和网站开发,网站推广做哪个比较好,阿里云装wordpress慢一、理论部分1. 主题交换机#xff08;Topic Exchange#xff09;简介主题交换机是RabbitMQ中最灵活也是最强大的交换机类型。它结合了扇形交换机的广播能力和直连交换机的精确匹配能力#xff0c;同时引入了模式匹配的概念。主题交换机的工作方式#xff1a;消息仍然带有路…一、理论部分1. 主题交换机Topic Exchange简介主题交换机是RabbitMQ中最灵活也是最强大的交换机类型。它结合了扇形交换机的广播能力和直连交换机的精确匹配能力同时引入了模式匹配的概念。主题交换机的工作方式消息仍然带有路由键Routing Key但路由键必须是由点号分隔的单词列表如usa.news、europe.weather.alert。队列通过绑定键Binding Key 绑定到交换机绑定键也使用相同的点号分隔格式。绑定键支持两种通配符进行模式匹配。2. 通配符规则主题交换机的强大之处在于绑定键支持通配符*星号匹配恰好一个单词示例*.orange.* 可以匹配 quick.orange.rabbit但不能匹配 quick.orange.fox.jumps#井号匹配零个或多个单词示例lazy.# 可以匹配 lazy、lazy.fox、lazy.brown.fox、lazy.pink.fox.jumps.over3. 路由键格式最佳实践路由键通常采用层次结构便于模式匹配facility.severityauth.info、kernel.errorregion.service.eventusa.payment.success、europe.order.cancelledcategory.subcategory.actionnews.sports.update、weather.alert.severe4. 使用场景主题交换机适用于需要复杂、灵活的消息路由场景新闻订阅系统用户可以根据兴趣订阅特定主题如sports.*、*.finance物联网设备监控按设备类型、地理位置、告警级别路由消息微服务事件总线基于事件类型和来源进行精细路由二、实操部分构建智能新闻分发系统我们将构建一个新闻分发系统其中生产者发送带有分类路由键的新闻消息消费者可以根据兴趣订阅特定模式的新闻第1步创建项目创建一个新的解决方案。添加一个控制台应用程序项目作为生产者EmitLogTopic。添加多个消费者项目ReceiveNewsAll - 接收所有新闻ReceiveSportsNews - 接收所有体育新闻ReceiveUSNews - 接收所有美国新闻ReceiveCriticalAlerts - 接收所有紧急警报ReceiveWeatherUpdates - 接收所有天气更新为所有项目添加RabbitMQ.Client NuGet包。第2步编写新闻生产者EmitLogTopic.cs复制代码using System.Text;using RabbitMQ.Client;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){// 声明主题交换机channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);// 路由键格式category.region.severity// 示例news.usa.info, sports.europe.alert, weather.asia.criticalvar routingKey (args.Length 0) ? args[0] : anonymous.info;var message (args.Length 1) ? string.Join( , args.Skip(1).ToArray()) : Hello World!;var body Encoding.UTF8.GetBytes(message);channel.BasicPublish(exchange: topic_logs,routingKey: routingKey,basicProperties: null,body: body);Console.WriteLine($ [x] Sent {routingKey}:{message});}Console.WriteLine( Press [enter] to exit.);Console.ReadLine();复制代码第3步编写接收所有新闻的消费者ReceiveNewsAll.cs复制代码using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);var queueName channel.QueueDeclare().QueueName;// 使用 # 匹配所有消息channel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: #);Console.WriteLine($ [*] Waiting for ALL news. Queue: {queueName});var consumer new EventingBasicConsumer(channel);consumer.Received (model, ea) {var body ea.Body.ToArray();var message Encoding.UTF8.GetString(body);Console.WriteLine($ [x] [ALL] Received {ea.RoutingKey}:{message});};channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);Console.WriteLine( Press [enter] to exit.);Console.ReadLine();}复制代码第4步编写接收体育新闻的消费者ReceiveSportsNews.cs复制代码using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);var queueName channel.QueueDeclare().QueueName;// 匹配所有体育相关的新闻sports.*.*channel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: sports.#);Console.WriteLine($ [*] Waiting for SPORTS news. Queue: {queueName});var consumer new EventingBasicConsumer(channel);consumer.Received (model, ea) {var body ea.Body.ToArray();var message Encoding.UTF8.GetString(body);Console.WriteLine($ [x] [SPORTS] Received {ea.RoutingKey}:{message});};channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);Console.WriteLine( Press [enter] to exit.);Console.ReadLine();}复制代码第5步编写接收美国新闻的消费者ReceiveUSNews.cs复制代码using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);var queueName channel.QueueDeclare().QueueName;// 匹配所有美国相关的新闻*.usa.*channel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: *.usa.*);Console.WriteLine($ [*] Waiting for USA news. Queue: {queueName});var consumer new EventingBasicConsumer(channel);consumer.Received (model, ea) {var body ea.Body.ToArray();var message Encoding.UTF8.GetString(body);Console.WriteLine($ [x] [USA] Received {ea.RoutingKey}:{message});};channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);Console.WriteLine( Press [enter] to exit.);Console.ReadLine();}复制代码第6步编写接收紧急警报的消费者ReceiveCriticalAlerts.cs复制代码using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);var queueName channel.QueueDeclare().QueueName;// 匹配所有紧急级别的消息*.*.criticalchannel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: *.*.critical);Console.WriteLine($ [*] Waiting for CRITICAL alerts. Queue: {queueName});var consumer new EventingBasicConsumer(channel);consumer.Received (model, ea) {var body ea.Body.ToArray();var message Encoding.UTF8.GetString(body);Console.WriteLine($ [x] [CRITICAL] Received {ea.RoutingKey}:{message});Console.WriteLine( - Sending emergency notification!);};channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);Console.WriteLine( Press [enter] to exit.);Console.ReadLine();}复制代码第7步编写接收天气更新的消费者ReceiveWeatherUpdates.cs复制代码using System.Text;using RabbitMQ.Client;using RabbitMQ.Client.Events;var factory new ConnectionFactory() { HostName localhost, UserName myuser, Password mypassword };using (var connection factory.CreateConnection())using (var channel connection.CreateModel()){channel.ExchangeDeclare(exchange: topic_logs, type: ExchangeType.Topic);var queueName channel.QueueDeclare().QueueName;// 匹配所有天气相关的更新weather.*// 一个队列可以绑定多个模式channel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: weather.#);channel.QueueBind(queue: queueName, exchange: topic_logs, routingKey: *.alert); // 也接收所有警报Console.WriteLine($ [*] Waiting for WEATHER updates and ALERTS. Queue: {queueName});var consumer new EventingBasicConsumer(channel);consumer.Received (model, ea) {var body ea.Body.ToArray();var message Encoding.UTF8.GetString(body);Console.WriteLine($ [x] [WEATHER/ALERT] Received {ea.RoutingKey}:{message});};channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);Console.WriteLine( Press [enter] to exit.);Console.ReadLine();}复制代码第8步运行与演示启动所有消费者打开六个终端窗口分别运行所有消费者程序。发送各种类型的新闻消息复制代码cd EmitLogTopic# 发送体育新闻dotnet run sports.usa.score Team USA wins gold medaldotnet run sports.europe.update Champions League finals scheduled# 发送美国相关新闻dotnet run news.usa.politics Election results announceddotnet run tech.usa.innovation Silicon Valley startup raises $10M# 发送紧急警报dotnet run weather.usa.critical Tornado warning for Midwestdotnet run safety.europe.critical Security alert: System maintenance# 发送天气更新dotnet run weather.asia.update Monsoon season beginsdotnet run news.europe.alert Breaking: Major announcement# 发送其他消息dotnet run entertainment.hollywood.gossip Celebrity wedding announced复制代码观察路由结果并分析模式匹配消息路由键 ALL SPORTS USA CRITICAL WEATHER/ALERTsports.usa.score ✅ ✅ ✅ ❌ ❌sports.europe.update ✅ ✅ ❌ ❌ ❌news.usa.politics ✅ ❌ ✅ ❌ ❌tech.usa.innovation ✅ ❌ ✅ ❌ ❌weather.usa.critical ✅ ❌ ✅ ✅ ✅safety.europe.critical ✅ ❌ ❌ ✅ ✅ (*.alert)weather.asia.update ✅ ❌ ❌ ❌ ✅news.europe.alert ✅ ❌ ❌ ❌ ✅ (*.alert)entertainment.hollywood.gossip ✅ ❌ ❌ ❌ ❌测试复杂场景发送 weather.alert.severe.critical - 观察哪些消费者能收到发送 sports.alert - 测试多个模式的匹配在管理后台查看绑定关系理解通配符的实际效果第9步通配符规则详解示例为了更好理解通配符让我们看一些匹配示例绑定键 *.orange.* 的匹配情况✅ quick.orange.rabbit (匹配)✅ lazy.orange.elephant (匹配)❌ quick.orange.fox.lazy (不匹配 - 四个单词)❌ orange (不匹配 - 只有一个单词)❌ quick.brown.fox (不匹配 - 中间不是orange)绑定键 lazy.# 的匹配情况✅ lazy (匹配)✅ lazy.fox (匹配)✅ lazy.brown.fox (匹配)✅ lazy.pink.fox.jumps.over (匹配)❌ quick.lazy.fox (不匹配 - 第一个单词不是lazy)本章总结在这一章中我们深入学习了RabbitMQ中最强大的主题交换机掌握了基于模式匹配的复杂消息路由主题交换机Topic Exchange理解了基于通配符的模式匹配路由机制。通配符规则掌握了*匹配一个单词和#匹配零个或多个单词的使用方法。路由键设计学习了使用点号分隔的层次化路由键设计最佳实践。复杂路由场景实现了支持多维度过滤的智能新闻分发系统。多重模式绑定掌握了单个队列绑定多个模式的高级用法。