场景:用户注册,信息写入数据库后,须要给用户发送注册成功的邮件,再发送注册成功的邮件。php
1.同步调用:注册成功后,顺序执行发送邮件方法,发送短信方法,最后响应用户 java
2.并行调用:注册成功后,用多线程的方式并发执行发邮件和发短信方法,最后响应用户 git
3.消息队列:注册成功后,将要发送的消息用很短的时间写入消息队列中,以后响应用户;发送邮件的服务和发送短息的服务就能够从消息队列中异步读去,而后发送任务。 github
场景:购物下单后,调用库存系统,更新库存。web
1.耦合的方式:订单系统,写调用库存系统的逻辑。 spring
2.解耦的方式:订单系统,将下达的消息写入消息队列,库存系统从消息队列中读取消息,更新库存。 docker
秒杀场景中,咱们能够设置一个定长的消息队列,秒杀开始,谁快谁先进入队列,而后快速返回用户是否秒到 ,以后在平稳的处理秒杀后的业务。 shell
RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue Protocol)的开源实现。数据库
AMQP 中消息的路由过程和 Java 开发者熟悉的 JMS 存在一些差异,AMQP 中增长了 Exchange 和 Binding 的角色。生产者把消息发布到 Exchange 上,消息最终到达队列并被 消费者接收,而 Binding 决定交换器的消息应该发送到那个队列。 apache
Exchange分发消息时根据类型的不一样分发策略有区别,目前共四种类型:direct、fanout、topic、headers 。headers 匹配 AMQP 消息的 header 而不是路由键, headers 交换器和 direct 交换器彻底一致,但性能差不少,目前几乎用不到了,因此直接看另外三种类型:
咱们使用 docker 来安装 RabbitMQ。
咱们在 docker hub上选择官方的带management管理界面的最新版本。
#获取rabbitmq镜像
docker pull rabbitmq:3-management
#启动 rabbitmq镜像,5672是mq通讯端口,15672是mq的web管理界面端口
run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 镜像ID
复制代码
访问127.0.0.1:15672 ,用帐号:guest 密码:guest 登陆,界面以下:
对rabbitmq的详细使用在这里,就不讲解了,咱们这节的重点是整合rabbitmq。
建立项目引入rabbitmq依赖。
<?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.gf</groupId>
<artifactId>springboot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-rabbitmq</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
复制代码
package com.gf.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.MessageConverter;
/** * 自定义消息转换器,默认是jdk的序列化转换器,咱们自定义为json的 */
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
复制代码
咱们测试建立管理配置、发送消息、接收消息
package com.gf;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
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;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void contextLoads() {
}
@Test
public void create(){
//建立Exchange
amqpAdmin.declareExchange( new DirectExchange( "exchange.direct") );
amqpAdmin.declareExchange( new FanoutExchange( "exchange.fanout") );
amqpAdmin.declareExchange( new TopicExchange( "exchange.topic") );
//建立Queue
amqpAdmin.declareQueue( new Queue( "direct.queue" , true ) );
amqpAdmin.declareQueue( new Queue( "fanout.queue" , true ) );
//绑定Queue
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.direct" , "direct.queue" , null ) );
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.direct" , "fanout.queue" , null ) );
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.fanout" , "" , null ) );
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.fanout" , "" , null ) );
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.topic" , "direct.#" , null ) );
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.topic" , "direct.*" , null ) );
}
@Test
public void send2Direct() {
Map<String , Object> map = new HashMap<>();
map.put( "msg" , "这是一条点对点消息" );
map.put( "data" , Arrays.asList("helloworld" , 123 , true) );
rabbitTemplate.convertAndSend( "exchange.direct" , "direct.queue" , map );
}
@Test
public void send2Topic() {
Map<String , Object> map = new HashMap<>();
map.put( "msg" , "这是一条广播消息" );
map.put( "data" , Arrays.asList("topic消息" , 123 , true) );
rabbitTemplate.convertAndSend( "exchange.fanout" , "", map );
}
@Test
public void receive() {
Object o = rabbitTemplate.receiveAndConvert( "direct.queue" );
o.getClass();
System.out.println(o.getClass());
System.out.println(o);
}
}
复制代码
监听消息
package com.gf;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/** * 自动配置 * 1. RabbitAutoConfiguration * 2. 自动配置了链接工厂ConnectionFactory * 3. RabbitProperties 封装了RabbitMQ的配置 * 4. RabbitTemplate : 给RabbitMQ发送和接受消息 * 5. AmqpAdmin : RabbitMQ系统管理功能组件 * 6. @EnableRabbit + @RabbitListener */
@EnableRabbit
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}
复制代码
package com.gf.service;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class MQService {
@RabbitListener(queues = "fanout.queue")
public void receive(Message message) {
System.out.println("收到消息 : " + new String(message.getBody()));
}
}
复制代码