您好,登錄后才能下訂單哦!
場(chǎng)景:用戶注冊(cè),信息寫(xiě)入數(shù)據(jù)庫(kù)后,需要給用戶發(fā)送注冊(cè)成功的郵件,再發(fā)送注冊(cè)成功的郵件。
1.同步調(diào)用:注冊(cè)成功后,順序執(zhí)行發(fā)送郵件方法,發(fā)送短信方法,最后響應(yīng)用戶
2.并行調(diào)用:注冊(cè)成功后,用多線程的方式并發(fā)執(zhí)行發(fā)郵件和發(fā)短信方法,最后響應(yīng)用戶
3.消息隊(duì)列:注冊(cè)成功后,將要發(fā)送的消息用很短的時(shí)間寫(xiě)入消息隊(duì)列中,之后響應(yīng)用戶;發(fā)送郵件的服務(wù)和發(fā)送短息的服務(wù)就可以從消息隊(duì)列中異步讀去,然后發(fā)送任務(wù)。
場(chǎng)景:購(gòu)物下單后,調(diào)用庫(kù)存系統(tǒng),更新庫(kù)存。
1.耦合的方式:訂單系統(tǒng),寫(xiě)調(diào)用庫(kù)存系統(tǒng)的邏輯。
2.解耦的方式:訂單系統(tǒng),將下達(dá)的消息寫(xiě)入消息隊(duì)列,庫(kù)存系統(tǒng)從消息隊(duì)列中讀取消息,更新庫(kù)存。
秒殺場(chǎng)景中,我們可以設(shè)置一個(gè)定長(zhǎng)的消息隊(duì)列,秒殺開(kāi)始,誰(shuí)快誰(shuí)先進(jìn)入隊(duì)列,然后快速返回用戶是否秒到 ,之后在平穩(wěn)的處理秒殺后的業(yè)務(wù)。
RabbitMQ是一個(gè)由erlang開(kāi)發(fā)的AMQP(Advanved Message Queue Protocol)的開(kāi)源實(shí)現(xiàn)。
AMQP 中消息的路由過(guò)程和 Java 開(kāi)發(fā)者熟悉的 JMS 存在一些差別,AMQP 中增加了 Exchange 和 Binding 的角色。生產(chǎn)者把消息發(fā)布到 Exchange 上,消息最終到達(dá)隊(duì)列并被 消費(fèi)者接收,而 Binding 決定交換器的消息應(yīng)該發(fā)送到那個(gè)隊(duì)列。
Exchange分發(fā)消息時(shí)根據(jù)類型的不同分發(fā)策略有區(qū)別,目前共四種類型:direct、fanout、topic、headers 。headers 匹配 AMQP 消息的 header 而不是路由鍵, headers 交換器和 direct 交換器完全一致,但性能差很多,目前幾乎用不到了,所以直接看另外三種類型:
我們使用 docker 來(lái)安裝 RabbitMQ。
我們?cè)?docker hub上選擇官方的帶management管理界面的最新版本。
#獲取rabbitmq鏡像
docker pull rabbitmq:3-management
#啟動(dòng) rabbitmq鏡像,5672是mq通信端口,15672是mq的web管理界面端口
run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 鏡像ID
訪問(wèn)127.0.0.1:15672 ,用賬號(hào):guest 密碼:guest 登錄,界面如下:
對(duì)rabbitmq的詳細(xì)使用在這里,就不講解了,我們這節(jié)的重點(diǎn)是整合rabbitmq。
創(chuàng)建項(xiàng)目引入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;
/**
* 自定義消息轉(zhuǎn)換器,默認(rèn)是jdk的序列化轉(zhuǎn)換器,我們自定義為json的
*/
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
我們測(cè)試創(chuàng)建管理配置、發(fā)送消息、接收消息
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(){
//創(chuàng)建Exchange
amqpAdmin.declareExchange( new DirectExchange( "exchange.direct") );
amqpAdmin.declareExchange( new FanoutExchange( "exchange.fanout") );
amqpAdmin.declareExchange( new TopicExchange( "exchange.topic") );
//創(chuàng)建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" , "這是一條點(diǎn)對(duì)點(diǎn)消息" );
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);
}
}
監(jiān)聽(tīng)消息
###4. 啟動(dòng)類
package com.gf;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 自動(dòng)配置
* 1. RabbitAutoConfiguration
* 2. 自動(dòng)配置了連接工廠ConnectionFactory
* 3. RabbitProperties 封裝了RabbitMQ的配置
* 4. RabbitTemplate : 給RabbitMQ發(fā)送和接受消息
* 5. AmqpAdmin : RabbitMQ系統(tǒng)管理功能組件
* 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
歡迎關(guān)注我的公眾號(hào)《程序員果果》,關(guān)注有驚喜~~
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。