springboot+rabbitMQ实现RPC远程调用
如题,我们要使用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: 8010spring: 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: 8090serviceIdspring: 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
|
@RestControllerpublic 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
|
@SpringBootApplicationpublic 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
|
@Componentpublic 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
|
@SpringBootApplicationpublic class RabbitMQApp { public static void main(String[] args) { SpringApplication.run(RabbitMQApp.class, args); }} |
4.测试
先启动积分微服务,再启动订单微服务,在浏览器中输入http://localhost:8010/testMq?message=123,发现订单微服务控制台输出:
购物成功,向积分系统发送消息:123,添加积分
而在积分微服务的控制台输出:
这里是积分微服务,接收到消息:123
测试成功。
更多Java学习资料可关注:gzitcast

浙公网安备 33010602011771号