SpringBoot整合RabbitMQ(生产者、消费者)

生产者:

  1. 创建生产者SpringBoot工程
  2. 引入依赖

<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.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>producer-springbootartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
    parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-amqpartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
    dependencies>
project>
  1. 编写yml配置文件
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: hehe
    password: hehe
    virtual-host: /
  1. 定义交换机,队列,以及绑定关系的配置类
package com.atheima.rabbitmq;


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

/**
 * @descriptions:
 * @author:
 * @date: 2021/6/11 18:13
 * @version: 1.0
 */
/*
*   从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,
* 这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,
* 初始化Spring容器。@Configuration的第二个作用是可以作为配置类,和@Component的作用一样,两者的差别是@Configuration
* 中所有带 @Bean 注解的方法都会被CGLIB动态代理,因此调用该方法返回的都是同一个实例。
 * */
@Configuration  //配置类
public class RabbitMQConfig {

    public static final String EXCHANGE_NAME="boot_topic_exchange";

    public static final String QUEUE_NAME="boot_queue";

    //交换机
    @Bean("bootExchange")
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //队列,duralbe:持久的
    @Bean
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    /*
    * 队列和交换机绑定
    * 1.知道那个队列
    * 2.知道那个交换机
    * 3.routing key
    *
    * */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange")Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();

    }

}

  1. 注入RabbitTemplate,调用方法,消息发送
package com.atheima.rabbitmq;

import com.atheima.rabbitmq.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;


/**
 * @descriptions:
 * @author:
 * @date: 2021/6/11 18:32
 * @version: 1.0
 */

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

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Test
    public void testSend(){
        //发消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello!!!!!!");
    }
}

注:springboot的启动类要和测试用的类放到一个包下要么会包注解错误(提示使用其他注解)

消费者:

  1. 创建消费者工程
  2. 导入依赖

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.1version>
        <relativePath/>
    parent>

    <groupId>consumer-springbootgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-amqpartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

  1. 编写yml配置
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: hehe
    password: hehe
    virtual-host: /
  1. 编写接收消息代码
package consumerspringboot.demo;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @descriptions:
 * @author:
 * @date: 2021/6/15 11:23
 * @version: 1.0
 */
@Component
public class RabbitMQListener {

    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message  message){
        System.out.println(message);
        //System.out.println( new String(message.getBody()));
    }
}

你可能感兴趣的:(RabbitMQ)