消息中間件對(duì)于我們系統(tǒng)之間的解耦合,消峰等都有極大的幫助。spring boot 也集成了此部分的內(nèi)容,集成最為容易的是rabbitmq。今天我們就以rabbitmq為例說明。
老規(guī)矩,先看下pom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ 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-amqp</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > </ dependency > |
AMQP,即Advanced Message Queuing Protocol,一個(gè)提供統(tǒng)一消息服務(wù)的應(yīng)用層標(biāo)準(zhǔn)高級(jí)消息隊(duì)列協(xié)議,是應(yīng)用層協(xié)議的一個(gè)開放標(biāo)準(zhǔn),為面向消息的中間件設(shè)計(jì)。基于此協(xié)議的客戶端與消息中間件可傳遞消息,并不受客戶端/中間件不同產(chǎn)品,不同的開發(fā)語言等條件的限制,spring-boot-starter-amqp引入的就是rabbitmq。有個(gè)前提,你的機(jī)子上要首先先安裝rabbitmq的server,然后執(zhí)行 rabbitmq-server server就啟動(dòng)了。啟動(dòng)后,我們就可以配置我們的客戶端程序了。首先看下我們的配置文件
1
2
3
4
5
6
|
spring.application.name: spirng-boot-rabbitmq spring.rabbitmq.host: 127.0.0.1 spring.rabbitmq.port: 5672 spring.rabbitmq.username: guest spring.rabbitmq.password: guest |
配置了服務(wù)器的IP,端口,用戶名,密碼等基礎(chǔ)信息,保證我們能連上服務(wù)器。
增加一個(gè)Rabbitmq的配置類
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.shuqi; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public Queue Queue() { return new Queue( "hello" ); } } |
創(chuàng)建了一個(gè)名稱叫做hello的隊(duì)列,然后producer可以往hello的隊(duì)列里放數(shù)據(jù),consumer可以從hello的隊(duì)列里消費(fèi)數(shù)據(jù)。看下producer的處理程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.shuqi.controller; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private AmqpTemplate rabbitTemplate; @RequestMapping ( "/hello" ) public String hello( @RequestParam String name){ rabbitTemplate.convertAndSend( "hello" , "hello " +name); return "消息發(fā)送成功" ; } } |
通過controller生產(chǎn)消息,通過AmqpTemplate發(fā)送消息。有了生產(chǎn)者我們看下消費(fèi)者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.shuqi.consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener (queues = "hello" ) @Slf4j public class HelloConsumer { @RabbitHandler public void process(String hello) { log.info( "接收到的消息:message:{}" ,hello); } } |
@RabbitListener(queues = "hello") 表示是一個(gè)Rabbitmq的監(jiān)聽器,監(jiān)聽的隊(duì)列名稱是hello,說明數(shù)據(jù)可定會(huì)過來,數(shù)據(jù)過來了,通過 @RabbitHandler 修飾的方法來處理過來的數(shù)據(jù)。打印一下。下面我們啟動(dòng)項(xiàng)目看看效果。
在瀏覽器中輸入 http://localhost:8080/hello?name=shuqi 看到下面的結(jié)果
看下控制臺(tái)輸出的日志
2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer : 接收到的消息:message:hello shuqi
說明消息已經(jīng)被consumer接收并處理掉了。大家可以把玩下。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/f9d1466681d4