A list,

In the Spring project, you can use Spring-Rabbit to operate RabbitMQ

Especially in Spring Boot projects, you can use RabbitTemplate to send messages and annotations to receive messages by importing the corresponding AMQP initiator dependencies.

Generally during development:

Producer Engineering:

  1. The application. Yml file configures RabbitMQ information.
  2. Write configuration classes in the producer project to create switches and queues and bind them
  3. Inject a RabbitTemplate object to send messages to the switch

Consumer Engineering:

  1. The application.yml file configures RabbitMQ information
  2. Create a message processing class that receives and processes messages in the queue

Second, set up the producer project

① Create project

Create a producer project springboot-rabbitmq-producer

② Add a dependency

Modify the pom. XML file to the following:

<? 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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>  <version>2.14..RELEASE</version> 
    </parent> 
    <groupId>com.itheima</groupId> 
    <artifactId>springboot-rabbitmq-producer</artifactId> 
    <version>1.0-SNAPSHOT</version> 
        
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-amqp</artifactId> 
        </dependency> 
    </dependencies> 
</project>
Copy the code

(3) start the class

package com.itheima.rabbitmq; 

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); }}Copy the code

(4) to configure the RabbitMQ

The configuration file

Create application.yml as follows:

spring: 
    rabbitmq: 
        host: localhost 
        port: 5672 
        virtual-host: /itcast 
        username: caiyo
        password: caiyo
Copy the code

Bind switches and queues

Create the RabbitMQ queues and switches binding configuration com. Itheima. The RabbitMQ. Config. RabbitMQConfig

package com.itheima.rabbitmq.config; 

import org.springframework.amqp.core.*; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class RabbitMQConfig { 
    // Switch name
    public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange"; 
    
    // Queue name
    public static final String ITEM_QUEUE = "item_queue"; 
    
    // Declare a switch
    @Bean("itemTopicExchange") 
    public Exchange topicExchange(a){ 
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build(); 
    }
    
    // Declare a queue
    @Bean("itemQueue") 
    public Queue itemQueue(a){ 
        return QueueBuilder.durable(ITEM_QUEUE).build(); 
    }
    
    // Bind queues and switches
    @Bean 
    public Binding itemQueueExchange(@Qualifier( "itemQueue") Queue queue, @Qualifier("itemTopicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs(); }}Copy the code

⑤ Message sending Controller

Let’s create a SpringMVC Controller for our testing

package com.itheima.rabbitmq.controller; 

import com.itheima.rabbitmq.config.RabbitMQConfig; 
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.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 

/** * The test class that sends messages */ 
@RestController 
public class SendMsgController { 
    // Inject the RabbitMQ template
    @Autowired 
    private RabbitTemplate rabbitTemplate; 
    
    /** * test */ 
    @GetMapping("/sendmsg") 
    public String sendMsg(@RequestParam String msg, @RequestParam String key){ 
        /** * Send message * Parameter 1: switch name * Parameter 2: route key * Parameter 3: sent message */ 
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg); 
        Return "Message sent successfully!" ;}}Copy the code

Three, build consumer projects

① Create project

Create the consumer project Springboot -rabbitmq-consumer

② Add a dependency

Modify the pom. XML file to the following:

<? 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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>  <version>2.14..RELEASE</version> 
    </parent> 
        
    <groupId>com.itheima</groupId> 
    <artifactId>springboot-rabbitmq-consumer</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
    
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-amqp</artifactId> 
        </dependency> 
    </dependencies>
</project>
Copy the code

(3) start the class

package com.itheima.rabbitmq; 

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); }}Copy the code

(4) to configure the RabbitMQ

Create application.yml as follows:

spring: 
    rabbitmq: 
        host: localhost 
        port: 5672 
        virtual-host: /itcast 
        username: caiyo
        password: caiyo
Copy the code

⑤ Message listening processing class

Write a message listener com. Itheima. The rabbitmq. Listener. MyListener

package com.itheima.rabbitmq.listener; 

import org.springframework.amqp.rabbit.annotation.RabbitListener; 
import org.springframework.stereotype.Component; 

@Component 
public class MyListener { 
    /** * listen for messages on a queue *@paramMessage Indicates the received message */ 
    
    @RabbitListener(queues = "item_queue") 
    public void myListener1(String message){
        System.out.println("The consumer receives the following message:"+ message); }}Copy the code

6 test

Create a test class in producer project springboot-rabbitmq-producer and send a message:

package com.itheima.rabbitmq; 

import com.itheima.rabbitmq.config.RabbitMQConfig; 
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;

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class RabbitMQTest { 

    @Autowired 
    private RabbitTemplate rabbitTemplate; 
    
    @Test 
    public void test(a){ 
        rabbitTemplate.convertAndSend(
            RabbitMQConfig.ITEM_TOPIC_EXCHANGE, 
                "item.insert"."New item, routing key to item.insert"); 
        rabbitTemplate.convertAndSend(
            RabbitMQConfig.ITEM_TOPIC_EXCHANGE, 
                "item.update"."Item modification, routing key to item.update"); 
        rabbitTemplate.convertAndSend(
            RabbitMQConfig.ITEM_TOPIC_EXCHANGE, 
                "item.delete"."Delete item, routing key is item.delete"); }}Copy the code

Run the above tests (switches and queues are declared and bound first) and start the consumer; Check the console in the consumer project springboot-rabbitmq-consumer to see if the corresponding message is received.

Alternatively, you can see the switch and queue binding from the RabbitMQ management console: