How to implement a round-robin queue consumer in Spring boot

时光毁灭记忆、已成空白 提交于 2019-12-06 07:59:16

RabbitMQ treats all consumers the same - it knows no difference between multiple consumers in one container Vs. one consumer in multiple containers (e.g. on different hosts). Each is a consumer from Rabbit's perspective.

If you want more control over server affinity, you need to use multiple queues with each container listening to its own queue.

You then control the distribution on the producer side - e.g. using a topic or direct exchange and specific routing keys to route messages to a specific queue.

This tightly binds the producer to the consumers (he has to know how many there are).

Or you could have your producer use routing keys rk.0, rk.1, ..., rk.29 (repeatedly, resetting to 0 when 30 is reached).

Then you can bind the consumer queues with multiple bindings -

consumer 1 gets rk.0 to rk.9, 2 gets rk.10 to rk.19, etc, etc.

If you then decide to increase the number of consumers, just refactor the bindings appropriately to redistribute the work.

The container will scale up to maxConcurrentConsumers on demand but, practically, scaling down only occurs when the entire container is idle for some time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!