如題,我們要使用Spring Boot和RabbitMQ實現RPC遠程調用,那么首先要了解RPC。RPC,即Remote Procedure Call Protocol 遠程過程調用協議,在大型的公司,系統一般都是由大大小小的服務構成,不同的團隊維護不同的代碼,部署在不同的機器。但是在做開發時候往往要用到其它團隊的方法,因為已經有了實現。但是這些服務部署不同的機器上,想要調用就需要網絡通信,這些代碼繁瑣且復雜,一不小心就會寫的很低效。RPC協議定義了規划,其它的公司都給出了不同的實現。而現在我們不使用其他公司的實現,直接使用消息中間件RabbitMQ進行實現。 現在有這樣的一個業務場景,在一個商城系統中,用戶在訂單微服務中購買商品成功之后會給用戶相應的積分,而積分系統是另外一個微服務,兩個微服務之間的調用之前是使用ribbon+eureka+feign進行調用的,現在我們使用這樣的邏輯,將它改造成使用RabbitMQ進行RPC調用:
1.創建工程
創建兩個微服務工程order和integral,引入相同的依賴:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<
parent
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-parent</
artifactId
>
<
version
>2.0.1.RELEASE</
version
>
</
parent
>
<!-- 管理依賴 -->
<
dependencyManagement
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-dependencies</
artifactId
>
<
version
>Finchley.M7</
version
>
<
type
>pom</
type
>
<
scope
>import</
scope
>
</
dependency
>
</
dependencies
>
</
dependencyManagement
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
</
dependency
>
<!-- SpringBoot整合RabbitMQ客戶端 -->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-amqp</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-test</
artifactId
>
</
dependency
>
</
dependencies
>
<
repositories
>
<
repository
>
<
id
>spring-milestones</
id
>
<
name
>Spring Milestones</
name
>
<
url
>https://repo.spring.io/libs-milestone</
url
>
<
snapshots
>
<
enabled
>false</
enabled
>
</
snapshots
>
</
repository
>
</
repositories
>
|
2.工程配置文件
在order微服務中創建配置文件application.yml:
|
01
02
03
04
05
06
07
08
09
10
11
|
server:
port: 8010
spring:
application:
name: app-member
#### RabbitMq配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
|
在 積分微服務integral中添加配置文件:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
server:
port: 8090
serviceId
spring:
application:
name: app-member
#### RabbitMq配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
listener:
simple:
retry:
max-attempts: 3
enabled: true
|
3.創建消息生產者和消費消息的監聽者
在訂單微服務order中創建消息生產者:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
@RestController
public
class
QueenSender {
@Autowired
private
AmqpTemplate rabbiTemplate;
/**
*
* @Title: sendMesage
* @Description: TODO發送消息
* @param: @param message
* @return: void
* @throws
*/
@RequestMapping
(
"/testMq"
)
public
void
sendMesage(String message) {
System.out.println(
"購物成功,向積分系統發送消息:"
+message+
",添加積分"
);
rabbiTemplate.convertAndSend(
"testExchange"
,
"testRabbitKey"
,message);
}
}
|
創建啟動類:
|
1
2
3
4
5
6
7
8
|
@SpringBootApplication
public
class
RabbitProviderApp {
public
static
void
main(String[] args) {
SpringApplication.run(RabbitProviderApp.
class
, args);
}
}
|
在積分微服務中創建消息監聽者:
|
01
02
03
04
05
06
07
08
09
10
11
12
|
@Component
public
class
RabbitMqListener {
@RabbitListener
(bindings =
@QueueBinding
(
value =
@Queue
(value =
"hello"
, durable =
"true"
),
exchange =
@Exchange
(value =
"testExchange"
, type =
"topic"
, durable =
"true"
),
key =
"testRabbitKey"
))
public
void
receiveMessage(String message) {
System.out.println(
"這里是積分微服務,接收到消息:"
+message);
}
}
|
注意:使用@RabbitListener監聽指定隊列、指定exchange、指定routingKey的消息,同時@RabbitListener有建立隊列、exchange、routingKey的功能
創建啟動類:
|
1
2
3
4
5
6
7
|
@SpringBootApplication
public
class
RabbitMQApp {
public
static
void
main(String[] args) {
SpringApplication.run(RabbitMQApp.
class
, args);
}
}
|
4.測試
先啟動積分微服務,再啟動訂單微服務,在瀏覽器中輸入http://localhost:8010/testMq?message=123,發現訂單微服務控制台輸出:
購物成功,向積分系統發送消息:123,添加積分
而在積分微服務的控制台輸出:
這里是積分微服務,接收到消息:123
測試成功。
更多Java學習資料可關注:gzitcast
