Objective:
Because the project needs to use RABBITMQ, a few days ago looked at RABBITMQ knowledge, recorded Springboot integration RABBITMQ process.
Give two URLs:
RABBITMQ Official Tutorial: http://www.rabbitmq.com/getstarted.html
Springboot entire RABBITMQ tutorial: https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-FEATURES-AMQP
Body: 1: The simplest HelloWorld
First of all, add Springboot's reliance.
1 <!--RABBITMQ -2 <Dependency>3 <groupId>Org.springframework.boot</groupId>4 <Artifactid>Spring-boot-starter-amqp</Artifactid>5 </Dependency>
In fact, we have to declare a queue, where the queue holds the producer's message, and then the consumer comes in and takes it out.
1 @Configuration2 Public classrabbitbeanconf {3 4 Public Static FinalString queue = "queue";5 6 @Bean7 PublicQueue Queue () {8 return NewQueue (Queue,true);9 }Ten}
Producers:
1 @Service2 Public classRabbitsender {3 4 @Autowired5 amqptemplate amqptemplate;6 7 Public voidSend (String msg) {8 amqptemplate.convertandsend (Rabbitbeanconf.queue, msg);9System.out.println ("producer produced a message:" + msg + "" +NewDate (). GetTime ());Ten } One}
Consumers:
1 @Service 2 public class Rabbitreceiver { 4 @RabbitListener (queues = Rabbitbeanconf.queue) 5 public void receive (String msg) { 6 7 System.out.println ("The consumer received a message:" + msg + "" + new Date (). GetTime ()); 8 9 }
2: Broadcast mode (fanout mode)
This pattern is different from the above pattern, and this pattern is for all consumers to receive when a single producer produces a message.
The simplest example is when you send a message, but all of your friends can receive the message.
RABBITMQ configuration:
1 @Configuration2 Public classFanoutrabbitmqconfig {3 4 //default Persistent durable is true5 //Firstfanoutqueue () bound according to the method name6 @Bean7 PublicQueue Firstfanoutqueue () {8 return NewQueue ("Firstfanoutqueue");9 }Ten One @Bean A PublicQueue Secondfanoutqueue () { - return NewQueue ("Secondfanoutqueue"); - } the - //Default Persistent Durbale is True - //Exchange Switches - @Bean + PublicFanoutexchange Fanoutexchange () { - return NewFanoutexchange ("Fanoutexchange"); + } A at /** - * The following two ways to succeed - * - * @return - * June 11, 2018 - */ in @Bean - PublicBinding Bindingfirst () { to returnBindingbuilder.bind (Firstfanoutqueue ()). to (Fanoutexchange ()); + } - the @Bean * PublicBinding Bindingsecond (fanoutexchange fanoutexchange, Queue secondfanoutqueue) { $ returnBindingbuilder.bind (Secondfanoutqueue) to (Fanoutexchange);Panax Notoginseng } -}
Here I explain that the authorities have introduced something called an "exchange" switch .
I understand this: the switch is mainly to understand the coupling production and consumer direct strong contact. The producer does not send things directly into the queue, and sends things to exchange, and then the exchange binds the queue that wants to send messages, and then the consumer listens. main decoupling.
Producers:
1 @Service2 Public classFanoutsender {3 4 @Autowired5 amqptemplate amqptemplate;6 7 Public voidSend (String msg) {8 9Amqptemplate.convertandsend ("Fanoutexchange", "" ", msg);TenSystem.out.println ("producer produced a message:" + msg + "" +NewDate (). GetTime ()); One } A}
Consumers:
1 @Service2 Public classFanoutrecevi {3 4@RabbitListener (queues = {"Firstfanoutqueue", "Secondfanoutqueue"})5 Public voidreceive (String msg) {6 7System.out.println ("The consumer has received a message:" + msg + "" +NewDate (). GetTime ());8 }9}
3:topic mode
Feel is a bit like direct mode. It just uses something like a regular, and an exchange matches the rules-compliant queue.
Topic is similar to direct, except that "mode" is supported on the match, and in the Routing_key form of "dot", two wildcard characters can be used:
-
*
Represents a word.
-
#
Represents 0 or more words.
4:header mode
Headers is also based on rule matching, and headers is a type of custom matching rule that uses routing_key in a fixed manner compared to direct and topic.
When a queue is bound to a switch, a set of key-value pairs is set, and the message includes a set of key-value pairs (the headers attribute) that are delivered to the corresponding queue when the pair of key-value pairs, or all of them, match.
"Springboot Series 5" Springboot Integrated RABBITMQ