場景:用戶註冊,信息寫入數據庫後,須要給用戶發送註冊成功的郵件,再發送註冊成功的郵件。java
1.同步調用:註冊成功後,順序執行發送郵件方法,發送短信方法,最後響應用戶
git
2.並行調用:註冊成功後,用多線程的方式併發執行發郵件和發短信方法,最後響應用戶
程序員
3.消息隊列:註冊成功後,將要發送的消息用很短的時間寫入消息隊列中,以後響應用戶;發送郵件的服務和發送短息的服務就能夠從消息隊列中異步讀去,而後發送任務。
github
場景:購物下單後,調用庫存系統,更新庫存。web
1.耦合的方式:訂單系統,寫調用庫存系統的邏輯。
spring
2.解耦的方式:訂單系統,將下達的消息寫入消息隊列,庫存系統從消息隊列中讀取消息,更新庫存。
docker
秒殺場景中,咱們能夠設置一個定長的消息隊列,秒殺開始,誰快誰先進入隊列,而後快速返回用戶是否秒到 ,以後在平穩的處理秒殺後的業務。
數據庫
RabbitMQ是一個由erlang開發的AMQP(Advanved Message Queue Protocol)的開源實現。apache
AMQP 中消息的路由過程和 Java 開發者熟悉的 JMS 存在一些差異,AMQP 中增長了 Exchange 和 Binding 的角色。生產者把消息發佈到 Exchange 上,消息最終到達隊列並被 消費者接收,而 Binding 決定交換器的消息應該發送到那個隊列。
json
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); } }
監聽消息
###4. 啓動類
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())); } }
源碼:https://github.com/gf-huanchupk/SpringBootLearning
歡迎關注個人公衆號《程序員果果》,關注有驚喜~~