说一下 整合的流程:

创建一个配置类,管理 RabbitTemplate

然后 利用 RabbitTemplate的 convertAndSend 方法 发送消息

再利用 @RabbitListener 注解 去监听 消息

首先 Maven springboot 的 parent依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

项目的相关依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

然后创建 配置文件 yml

server:
  port: 8080
spring:
  rabbitmq:
    host: 118.31.127.248
    username: 你设置的账号
    password: 你设置的密码
    virtual-host: /govbuy
    port: 5672

去添加一个配置类

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 {
    /**
     * 设置三部分
     * 交换机 Exchange
     * 队列 Queue
     * 绑定关系 Binding
     */
    // 声明 交换机名称
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    // 声明 队列名称
    public static final String QUEUE_NAME = "boot_queue";

    //1 配置交换机
    @Bean("bootExchange") // 设置BeanName 为 bootExchanghe
    public Exchange bootExchange() {
        return ExchangeBuilder
                .topicExchange(EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    //2 Queue 队列
    @Bean("bootQueue") // 设置BeanName 为 bootQueue
    public Queue bootQueue() {
        return QueueBuilder
                .durable(QUEUE_NAME)
                .build();
    }

    //3 队列 与交换机的绑定
    /**
     * 我们首先要知道我们绑定的路由key
     * @param queue 要知道那个队列
     * @param exchange 要知道那个交换机
     * @Qualifier注解是 自动装配
     * @return
     */
    @Bean("bootBind") //设置BeanName 为 bootBind
    public Binding bootBindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("boot.#")
                .noargs();
    }
}

配置类完成,去测试一下吧。

发送端

import com.zanglikun.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 ProducerTest {
    // 注入 rabbitMQtemplate模板类 ,不注入 就是空指针异常等你
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){
        // 发送消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.hello","这是整合:testSend方法发送的消息");
    }

}
测试完成!

这里 发送端 生产者的整合已经完毕了。

接收端

注意:其他配置 都一样,我们只需要 加上一个 @RabbitListener 注解 完成监听。

因为 @Test 不能写参数

所以 我们 不能再创建一个 单元测试去使用了。

创建一个 TestResive

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

/**
 * @author : zanglikun
 * @date : 2021/2/2 14:28
 * @Version: 1.0
 * @Desc : 费劲,没啥好说的
 */
@Component
public class TestResive {

    @RabbitListener(queues = "boot_queue")
    public void getMessage(Message message) {
        System.out.println(message); // 这里 不只是输出 单个 发送的信息,而是 全部输出 消息里面的内容数据
    }
}

输出 的 结果:

(Body:'这是整合:testSend方法发送的消息' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hello, deliveryTag=1, consumerTag=amq.ctag-uiKwYKNNZ-oM9xXITZtxoA, consumerQueue=boot_queue])

简单的 整合就算成功了!!!

完整代码下载:

特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取全部资料 ❤