生产者
pom.xml配置文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<!--rabbitmq 的导入-->
<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>
application.yaml配置文件
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
virtual-host: /
#相当与创建了一个连接工厂
RabbitmqConfig类
package springboot;
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 {
// 注:所有的类都在import org.springframework.amqp.core...包下导入
public static final String EXCHANGE_NAME = "boot_topic_exchange";
public static final String QUEUE_NAME = "boot_queue";
//创建一个模糊匹配路由原则的交换机 方法名相当于id 返回的交换机Exchange就是class引入的类
@Bean //存到ioc容器中
public Exchange bootExchange(){
//参数 第一个是交换机名字 第二个是 是否永久化
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
//创建一个队列
@Bean
public Queue bootQueue(){
//参数是队列的名字
return QueueBuilder.durable(QUEUE_NAME).build();
}
//交换机与队列绑定
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue")Queue bootQueue,
@Qualifier("bootExchange")Exchange bootExchange){
/*
1. 知道哪个队列
2. 知道哪个交换机
3. routing key
4.noargs():表示不指定参数
*/
return BindingBuilder.bind(bootQueue).to(bootExchange).with("topic.#").noargs();
}
}
Application类
package springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//springboot的入口
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
测试包
package springboot;
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 {
//导入rabbit模板
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
//发送信息
//第一个参数 交换机名字 第二 路由原则 第三是发送信息
rabbitTemplate.convertAndSend("boot_topic_exchange","topic.app","测试信息");
}
}
消费者配置
pom.xml配置文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
application.yaml配置
spring:
rabbitmq:
port: 5672
username: guest
password: guest
virtual-host: /
host: localhost
application类
package springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
RabbimtMQListener类
package springboot;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbimtMQListener {
//创建一个监听器监听接收信息:注解方式引入监听器
@RabbitListener(queues = "boot_queue")
public void myRabbitListeners(Message message){
System.out.println(new String(message.getBody()));
}
}
小结:
SpringBoot提供了快速整合RabbitMQ的方式
基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
生产端直接注入RabbitTemplate完成消息发送
消费端直接使用@RabbitListener完成消息接收