Spring boot uses rabbitmq to implement topic topics

In the previous article, we implemented the flexible configuration of the message system. Instead of using the fan out switch configuration, the direct switch is used, and based on the routing key, it can selectively receive messages.

Although using direct switch can improve our system, it still has limitations. It can not realize multi conditional routing.

In our messaging system, we want to subscribe not only to queues based on routing keys, but also to sources based on production messages. These concepts come from the UNIX tool syslog. The log is based on strict (info / warn / crit...) And easy (auth / cron / Kern...) Routing method. Our example is simpler than this.

This example will give us a lot of flexibility. For example, we want to listen to both logs from 'cron' and all logs from 'Kern'.

To achieve this flexibility, we need to know more about topic switches.

Topic exchange

When using the topic switch, the routing key written arbitrarily cannot be used. The form of the routing key should be a word divided by points. Any word can be used. It usually means something. For example, "stock. USD. NYSE", "NYSE. Vmw", "quick. Orange. Rabbit". However, the word size is limited to a maximum of 255 bytes.

There are two points to pay attention to when using a topic switch to define routing keys

Define routing keys that match the subject switch

In this example, we will send all messages describing animals. This message will be sent with a routing key consisting of 3 words and 2 points. The first word describes speed, the second describes color, and the third describes category: "< speed >. < colour >. < species >".

Create three kinds of bindings, Q1 and the key "*. Orange. *" binding, Q2 and "*. Rabbit", "lazy. #" binding.

The three binding relationships are summarized as follows:

A message with the routing key set to "quick. Orange. Rabbit" will be delivered to Q1 and Q2 queues. The same goes for "lazy. Orange. Elephant". "Quick. Orange. Fox" will go to the first queue, "lazy. Brown. Fox" will go to the second queue, "lazy. Pink. Rabbit" will go to the second queue, and it matches the binding twice. "Quick. Brown. Fox" can't go anywhere because it doesn't match and will be discarded.

What about "orange" and "quick. Orange. Male. Rabbit"? Because there is no match to any binding, it will also be discarded.

What about "lazy. Orange. Male. Rabbit", which is also a four word routing key?, Because it matches lazy# This will be passed to the second queue.

Tips for topic exchange

The theme switch is awesome and behaves similar to other switches.

Code example

The code is no different from the previous routing code. Please see

Config. java

Receiver. java

Send. java

SendTest. java

I won't let go of the test results. Please check them yourself.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>