兔子。路由配置最佳实践

发布于 2025-02-12 18:27:43 字数 359 浏览 1 评论 0 原文

我在我的宠物项目(基于弹簧靴)中使用RabbitMQ。在中@configuration 我声明bean,例如 queue binding direct> directexchange 。因此,当我运行应用程序时,将自动创建所有这些与队列的交换和绑定。我担心这是否是配置这些与RabbitMQ相关的“实体”的正确方法。在应用程序启动之前,我应该将其分为单独的步骤吗?例如,在应用程序启动之前,将 curl 的系列调用http api将HTTP API召集到管理HTTP API,以创建所有必需的队列(带有交换和绑定)。创建/配置与路由相关的东西的最佳实践是什么?

I'm using RabbitMQ in my pet project (Spring Boot based). In @Configuration I declare beans like Queue,Binding,DirectExchange. So, when I run the application all these exchanges and bindings with queues are created automatically. I'm concerned about whether this is the correct way to configure these RabbitMQ-related "entities". Should I separate this into separate steps before application startup? For example, calling series of curl to the management HTTP API to create all needed queues (with exchanges and bindings) before application startup. What are the best practices for creating/configuring routing-related stuff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-02-19 18:27:43

问题是没有一种使用兔子的方法。但是,在与任何经纪人合作之前,我总是问自己一些问题。我在这里将它们应用于您的问题。

让我们询问方法:

  • 在创建交换,绑定,队列等方面:

尝试了解您使用的元素是否耐用。如果是这样,那么您可以在启动&的代码中创建此此功能。应用简单的健康检查。您还想检查您的RabbitMQ服务器是否持续数据。如果不是,那么您必须创建队列,交换&每次绑定。

  • 关于路由&与交流的绑定队列:

您需要考虑有两个主要问题,要考虑

  1. 延迟有多少重要?

如果延迟确实很重要,请尝试尽可能多地使用直接交换。原因很简单。您只是从交易所到队列而不必路由您的消息。路由增加了延迟,永远不会忘记!如果这些额外的MS无法为您带来影响,那么需要牢记以下问题才能了解如何定义交换,绑定和amp;队列。

  1. 我将如何使用经纪人?

有些人使用经纪人简单地进行酒吧/子消息。这是一个完全合理的用例。在这种情况下,风扇交换将是最可行的选择。如果您试图最大程度地减少您创建主题交换的队列数量,也可能很有趣。

但是更重要的是,您是专门用于服务间沟通的经纪人还是您的服务将成为生产者和消费者?地狱,这是前两种情况之间的混合吗?需要清楚地定义此边界。否则,您会陷入混乱,突然间,您会发现自己正在消耗另一个库可以内部处理的消息,或者简单地将参数传递给函数。

如何应用它的示例:

案例:我们有伐木服务&用户服务:

1。 使用经纪人:

我将如何在服务通信之间

2。我需要传递哪些消息?

  • crud操作
  • 登录/注销

3。 路由表看起来像(草稿)?

名称 类型 绑定 队列
用户 主题 user.cmd。
名称 交换 交换 交换

我 使用一个简单队列的CRUD操作。这是最有效的方法吗?这取决于您的服务。 user.cmd.create 应该转到用户:创建更好。这是您需要定义的另一个边界。

还需要提及的是,您应该使用队列&路由键作为信息。调试微服务可能是地狱般的。因此,应用一般命名约定是最合适的。没有一个命名约定,因此这再次取决于您的用例。

结论:

总体而言,最适合任何经纪人的做法显然是定义了您的经纪人及其基本要素的范围。如果做得不正确,那么您是否在启动时进行健康检查都无关紧要。在兔子报价的复杂性和有趣的功能中很容易迷失。尝试使它尽可能简单,然后问自己:“以后会花费我很多时间来重构/调试/修复吗?”如果答案是肯定的,请回到本段中的第一句话。

文档:

amqp概念:

一些一般最佳实践: https:// https://www.cloudamq.com/blog/ part2-rabbitmq-test practice-for-thigh-performance.html

路由: https://wwww.rabbitmq.com/tutorials/tutorials/tutorials/tutorials/tutorials/tutorials/tutorial-four--- python.html

延迟&吞吐量: https://blog.rabbitmq.com/posts/2012/05/some-queuing-ysome-queuing-theory-quey-theory-throughput-latency-and-bandwidth

The thing is there is no one way of using RabbitMQ. However there are a few questions I always ask myself before working with any broker. I'll apply them to your question here.

Let's question the approach:

  • In regards to creating the exchanges, bindings, queues etc:

Try to understand if the elements you are using are durable. If so, then you could create this within your code on startup & apply a simple health check. You also want to check if your RabbitMQ server persists data. If not, then you'll HAVE to create your queues, exchanges & bindings every time.

  • In regards to routing & binding queues with exchanges:

There are two major questions you need to consider

  1. How much does latency matter?

If latency does matter, try to use direct exchanges as much as you can. The reason for this is simple. You're simply going from exchange to queue instead of having to route your message. Routing adds latency, never forget! If those few extra ms won't make the difference for you, then the following question needs to be kept in mind to understand how to define your exchanges, bindings & queues.

  1. How will I use my broker?

Some people use their broker to simply pub/sub messages. This is a perfectly reasonable use case. In this case a fanout exchange would be the most viable option. If you're trying to minimize the amount of queues you're creating a topic exchange may be interesting as well.

However more important, are you using your broker exclusively for between-service communication or is your service going to be both the producer and consumer? Hell, is it a mix between the previous two cases? This boundary needs to be defined clearly. Else you'll run into a mess where you suddenly notice you're consuming messages that can actually be handled internally by another library or simply passing arguments to functions.

Example of how to apply it:

Case: we have a logging service & user service:

1. How will I use my broker:

between service communication.

2. Which messages do I need to get across?

  • CRUD operations
  • login/logout

3. How would my routing table look like (draft)?

Exchange Name Exchange Type Binding Queue
user topic user.cmd.* user:crud
user topic user.event.login user:login

Above you can clearly see we can handle all CRUD operations using one simple queue. Is this the most efficient approach? It depends on your service. It may be better that user.cmd.create should go to user:created. This is another boundary that you'll need to define.

Something that also needs to be mentioned is that you should use your Queues & routing keys as pieces of information. Debugging a micro service can be hellish. So applying a general naming convention would be most appropriate. There is no one naming convention, so this depends on your use case once again.

Conclusion:

In general the practice that is best in regards to any broker is clearly defining the scope of your broker and its underlying elements. If not done properly, it does not matter if you do a health check or not on startup. It's easy to get lost in the complexity and interesting features rabbitMQ offers. Try to keep it as simple as possible at first and ask yourself: "is it going to cost me a lot of time to refactor/debug/fix later?" If the answer is yes go back to the first sentence in this paragraph.

Documentation:

AMQP concepts: https://www.rabbitmq.com/tutorials/amqp-concepts.html

Some general best practices: https://www.cloudamqp.com/blog/part2-rabbitmq-best-practice-for-high-performance.html

Routing: https://www.rabbitmq.com/tutorials/tutorial-four-python.html

Latency & throughput: https://blog.rabbitmq.com/posts/2012/05/some-queuing-theory-throughput-latency-and-bandwidth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文