消息
一、消息概述
- JMS与AMQP的差异
二、异步处理
三、RabbitMQ简介
四、RabbitMQ运行机制
五、安装与使用RabbitMQ
- Linux 安装
-- 下载:
docker pull rabbitmq:3-management
-- 运行:
docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq xxx
xxx 代表 IMAGE ID
注意:下载过程中涉及的镜像问题请自行百度
- p 5672端口:主机与客户端进行通信的接口
- p 15672端口:访问web管理页面的接口
- 测试
打开web浏览器,输入:
127.0.0.1:15672
也可以将127.0.0.1替换成你的服务器地址
会看到如下页面:输入默认的账号与密码:guest进入rabbit服务
- 添加第一个direct类型的Exchanges
- 添加消息队列
- 为Exchanges绑定Queues
- exchange.topic需要模糊匹配
- exchange发送消息
exchange.direct 只会想与key匹配成功的Queue发送消息
exchange.fanout会向所有的Queue发送消息
exchange.topic会向与匹配规则相匹配的Queue发送消息
下面以exchange.direct发送消息为例
- Queues之helloworld获取消息
六、SpringBoot整合RabbitMQ
- 引入spring-boot-starter-amqp
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- properties文件配置
spring.rabbitmq.host=xxx //xxx: rabbitmq运行的ip地址
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.virtual-host
- 主类
/*
* 自动配置
* 1、RabbitAutoConfiguration
* 2.有自动配置的连接工厂ConnectionFactory;
* 3.RabbitProperties封装了RabbitMQ的配置
* 4、RabbitTemplate,给RabbitMQ发送和接收消息
* 5、AmqpAdmin:RabbitMQ系统管理功能组件
* 6.@EnableRabbit + @RabbitListener 监听消息队列的内容
* */
@EnableRabbit //开启基于注解的Rabbit模式
@SpringBootApplication
public class Springboot1300AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot1300AmqpApplication.class, args);
}
}
- 首先定制序列化规则
@Configuration
public class MyAMPQConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
- Book对象
public class Book {
private String bookName;
private String author;
public Book(){
super();
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot1300AmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/*
* 1、单播(点对点)
* */
@Test
public void contextLoads() {
// Message需要自己构造一个;定义消息体内容和消息头
// rabbitTemplate.send(exchange,routeKey,message);
// object默认当成消息体,只需要传入要发送的对象,自动序列化发给rabbitmq;
// rabbitTemplate.convertAndSend(exchange,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,true));
//对象被默认序列化以后发送出去
// rabbitTemplate.convertAndSend("exchange.direct","helloworld.news",new Book("XXX","王圣嵩"));
rabbitTemplate.convertAndSend("exchange.direct","helloworld",map);
}
//接收数据,如何将数据自动的转为json数据
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("helloworld");
System.out.println(o.getClass());
System.out.println(o);
}
/*
* 2.广播
* */
@Test
public void sendMsg(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("XXX","王圣嵩"));
}
}
- 使用监听模式
@Service
public class BookService {
//想要RabbitListener起作用,需要开启基于注解的Rabbit模式(主程序中标注@EnableRabbit)
@RabbitListener(queues = "helloworld.news")
public void receive(Book book){
System.out.println("收到消息"+book);
}
@RabbitListener(queues = "helloworld")
public void receive02(Message message) {
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
- 使用SpringBoot管理Exchange与Queue
@Test
public void creatExchange() {
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
System.out.println("创建完成");
amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
// 创建绑定规则
amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
}