1. Add maven dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code
2. Configure the rabbitmq
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /users
username: users
password: 1234
Copy the code
3. Producer test classes
Note: Calling the producer test class alone does not produce messages if there is no consumer listening
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class RabbitMqProviderTest { @Autowired private RabbitTemplate rabbitTemplate; Public void sendMsg() {for (int I =0; i<10; i++) { rabbitTemplate.convertAndSend("hello", i+" hello world"); }} @test // Switch mode/broadcast mode Send message, import switch, Routing is empty public void sendExchangeMsg () {rabbitTemplate. ConvertAndSend (" logs ", ""," this is a log "); } @test // Routing mode sends messages, compared to switch mode, Send the routing key public void sendRoutingMsg () {rabbitTemplate. ConvertAndSend (" directs ", "info", "this is a info log"); rabbitTemplate.convertAndSend("directs", "error", "this is a error log"); } @test // Dynamic routing mode sends messages. In contrast to routing mode, the routing key can be multiple words and delimiters. Segmentation public void sendTopicMsg () {rabbitTemplate. ConvertAndSend (" switchable viewer ", "user. Save", "topic test"); rabbitTemplate.convertAndSend("topics", "user.dept.save", "topic test"); }}Copy the code
4. Point-to-point and work queue mode consumers
import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class HelloConsumer { @RabbitHandler @RabbitListener(queuesToDeclare = @Queue("hello")) public void receive1(String msg) { System.out.println("msg1=" + msg); } @RabbitHandler @RabbitListener(queuesToDeclare = @Queue("hello")) public void receive2(String msg) { System.out.println("msg2=" + msg); }}Copy the code
5. Switch mode/broadcast mode consumers
import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; @Component public class ExchangeConsumer { @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "logs", type = "fanout")) }) public void exchangeMsg1(String msg) { System.out.println("exchangeMsg1=" + msg); } @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "logs", type = "fanout")) }) public void exchangeMsg2(String msg) { System.out.println("exchangeMsg2=" + msg); }}Copy the code
6. Routing pattern consumers
import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; @Component public class RoutingConsumer { @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "directs"), key = {"info", "error"}) }) public void exchangeMsg1(String msg) { System.out.println("directMsg1=" + msg); } @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "directs"), key = {"error"}) }) public void exchangeMsg2(String msg) { System.out.println("directMsg2=" + msg); }}Copy the code
7. Dynamic routing pattern consumers
import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; @Component public class TopicConsumer { @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "topics", type = "topic"), key = {"user.*"}) }) public void exchangeMsg1(String msg) { System.out.println("topics1=" + msg); } @RabbitHandler @RabbitListener(bindings = { @QueueBinding(value = @Queue, exchange = @Exchange(value = "topics", type = "topic"), key = {"user.#"}) }) public void exchangeMsg2(String msg) { System.out.println("topics2=" + msg); }}Copy the code