RabbitMQ
【黑马程序员RabbitMQ全套教程,rabbitmq消息中间件到实战】
文章目录
- RabbitMQ
- 第一天 基础
- 6 SpringBoot 整合RabbitMQ
- 6.1 SpringBoot 整合 RabbitMQ【生产者】
- 6.1.1 生产者
- 6.2 SpringBoot 整合 RabbitMQ【消费者】
- 6.2.1 消费者
- 6.3 小结
第一天 基础
6 SpringBoot 整合RabbitMQ
6.1 SpringBoot 整合 RabbitMQ【生产者】
6.1.1 生产者
- 创建生产者SpringBoot工程
好家伙,老师居然用手工 创建…
那我也…
直接创建
OK,自己导坐标吧
<?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><groupId>com.dingjiaxiong</groupId><artifactId>producer-springboot</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies></project>
OK, 这就是一个 SpringBoot 工程模块了
- 引入start,依赖坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
OK,上一步 一块儿搞了
- 编写yml配置,基本信息配置
spring:rabbitmq:host: xxxxxxxxxxxx # 记得改成自己的服务器IPusername: guestpassword: guestport: 5672virtual-host: /
这里就没用自己的用户了,感觉这节课 是很久之后,黑马老师补录的
来一个启动类
package com.dingjiaxiong;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** ClassName: ProducerApplication* date: 2022/11/16 17:00** @author DingJiaxiong*/@SpringBootApplication
public class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class);}}
OK
- 定义交换机,队列以及绑定关系的配置类
package com.dingjiaxiong.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;/*** ClassName: RabbitMQConfig* date: 2022/11/16 17:02** @author DingJiaxiong*/@Configuration
public class RabbitMQConfig {public static final String EXCHANGE_NAME = "boot_topic_exchange";public static final String QUEUE_NAME = "boot_queue";//1. 交换机@Bean("bootExchange")public Exchange bootExchange(){return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}//2. Queue 队列@Bean("bootQueue")public Queue bootQueue(){return QueueBuilder.durable(QUEUE_NAME).build();}//3. 队列和交换机的绑定关系@Beanpublic Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();}}
- 注入RabbitTemplate,调用方法,完成消息发送
直接测试类
package com.dingjiaxiong.test;import com.dingjiaxiong.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;/*** ClassName: ProducerTest* date: 2022/11/16 17:11** @author DingJiaxiong*/@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {//1. 注入RabbitTemplate@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testSend(){rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello");}}
OK,一切准备就绪,直接运行
OK,绿了
查看管控台【记得 换成 guest 用户】
交换机创建 成功
队列 也创建完成了,而且还有一条消息在里面
拿一下
没问题
【这就是生产者 了】
6.2 SpringBoot 整合 RabbitMQ【消费者】
6.2.1 消费者
【消费者】
- 创建消费者SpringBoot工程
终于想到Spring Initializr 了
下一步
这个版本就太高了… 一会儿 换一个【笔者这里 2022年11月16日,Boot 版本是2.7.5,老师是2.1.7 …】
依赖都不要,直接创建
OK,一个全新的SpringBoot 工程 模块
换一下版本 吧,和生产者 一致些
记得刷一下
- 引入start,依赖坐标
OK,就这样吧
- 编写yml配置,基本信息配置
直接复制 生产者 的
spring:rabbitmq:host: xxxxxxxxxxxxx # 记得改成自己的服务器IPusername: guestpassword: guestport: 5672virtual-host: /
- 定义监听类,使用@RabbitListener注解完成队列监听。
package com.dingjiaxiong.listener;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** ClassName: RabbitMQListener* date: 2022/11/16 17:28** @author DingJiaxiong*/@Component
public class RabbitMQListener {@RabbitListener(queues = "boot_queue")public void ListenerQueue(Message message){System.out.println(message);}}
OK,现在 的队列 里面还有一条 消息,直接启动消费者
OK,这样就拿到了
查看管控台
消息已经被消费了 ,欧克欧克【这就是 SpringBoot 整合RabbitMQ 了】
6.3 小结
- SpringBoot提供了快速整合RabbitMQ的方式
- 基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
- 生产端直接注入RabbitTemplate完成消息发送
- 消费端直接使用@RabbitListener完成消息接收