Spring is integrated with RabbitMQ, just use spring-Rabbit to operate RabbitMQ. Making the website

Using RabbitMQ in SpringBoot is much easier. Add the AMQP initiator to the POM, configure the RabbitMQ address in the application.yml, and use RabbitTemplate to send messages. To receive messages, use @rabbitListener.

producers

Note: Producers only care about which switch or queue they are sent to, not the binding between the switch and queue (which is what consumers need to care about).

Pom file

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > against 2.4.1 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>producer</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency>  <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Start the class

package com.example.producer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); }}Copy the code

The controller that sends the message

package com.example.producer.controller; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("/sendMessage") public String sendMessage(String exchange, String routingKey, String message, Integer times) { if (times == null) { times = 1; } int realTimes = 0; for (int i = 0; i < times; i++) { rabbitTemplate.convertAndSend(exchange, routingKey, message + i); realTimes++; } return "realTimes + realTimes + realTimes" ; }}Copy the code

Application. Yml configuration file

Server: port: 8081 Spring: rabbitmq: host: 192.168.2.100 port: 5672 username: dev Password: 123456 virtual-host: /devCopy the code

Test to initiate a request in the browser

http://127.0.0.1:8081/sendMessage?exchange=testTopic&routingKey=test.halo&message=HelloWorld! &times=100Copy the code
  • The method does not report errors if the switch from which the producer sent the message does not exist, but RabbitMQ does not automatically generate the switch and the message is lost.

  • So start the consumer first, or add the switch declaration to the producer project.

consumers

When creating switches, queues, and bindings, there are two methods: builder mode and normal new objects. Placing the binding relationship on the consumer indicates which messages the consumer wants to consume.

Pom file

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > against 2.4.1 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>consumer</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Start the class

package com.example.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Copy the code

Switch, queue, binding configuration class

package com.example.consumer.config; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public Exchange topicExchange() { // return new TopicExchange("testTopic", true, false); return ExchangeBuilder.topicExchange("testTopic").durable(true).build(); } @Bean public Queue topicQueue() { // return new Queue("testQueue", true, false, false); return QueueBuilder.durable("testQueue").build(); } @Bean public Binding topicBinding(Exchange topicExchange, Queue topicQueue) { // return new Binding("testQueue", Binding.DestinationType.QUEUE, "testTopic", "test.#", null); return BindingBuilder.bind(topicQueue).to(topicExchange).with("test.#").noargs(); }}Copy the code

Listener to receive messages

package com.example.consumer.listener; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class TestListener { @RabbitListener(queues = "testQueue") public void consume(String message) { System.out.printf("Message is %s.\n", message); }}Copy the code

Application. Yml configuration file

Spring: rabbitmq: host: 192.168.2.100 Port: 5672 username: dev Password: 123456 virtual-host: /devCopy the code
  • The input type in the consume method is determined by the type of message sent by the producer. For example, the producer sends a character array byte[], and the consumer receives byte[].

So easy~~~