RabbitMQ系列5 SpringBoot整合RabbitMQ
SpringBoot整合RabbitMQ
生产者
整合步骤概述
1.创建生产者SpringBoot工程
2.导入依赖坐标
3.编写yml配置,基本信息配置
4.定义交换机,队列以及绑定关系的配置类
5.注入RabbitTemplate,调用方法,完成消息发送
1.创建生产者SpringBoot工程
2.导入依赖坐标
<!--继承父类工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.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>
3.编写yml配置,基本信息配置
spring:
rabbitmq:
host: 121.196.111.120 #ip
port: 5672 #端口
username: guest #用户名
password: guest #密码
virtual-host: /demo #虚拟机名称
4.定义交换机,队列以及绑定关系的配置类
package com.pjh.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 RabbitConfig {
/*定义交换机名称*/
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();
}
/* 2.Queue 队列*/
@Bean("bootQueue")
public Queue bootQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
/*队列和交换机绑定关系*/
/*
* 知道哪个队列
* 知道哪个交换机
* routing key
* */
@Bean
public Binding getBinding(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
5.注入RabbitTemplate,调用方法,完成消息发送
package com.pjh.test;
import com.pjh.Config.RabbitConfig;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.print.DocFlavor;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {
/*注入RabbitTemplate*/
@Autowired
private RabbitTemplate rabbitTemplate;
@org.junit.Test
public void testSend(){
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
}
}
消费者
整合步骤概述
1.创建生产者SpringBoot工程
2.导入依赖坐标
3.编写yml配置,基本信息配置
4.编写启动类
5.编写消息监听处理类
6.测试
1.创建生产者SpringBoot工程
2.导入依赖坐标
<!--继承父类工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.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>
3.编写yml配置,基本信息配置
spring:
rabbitmq:
virtual-host: 121.196.111.120
4.编写启动类
package com.pjh;
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);
}
}
5.编写消息监听处理类
package com.pjh.Listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListen {
/**
* 监听某个队列的消息
* @param message 接收到的消息
*/
@RabbitListener(queues = "boot_queue")
public void myListener1(String message){
System.out.println("消费者接收到的消息为:" + message);
}
}