5. 远程调用(客户端)
常用的几种RPC调用方式包括http client、RestTemplate、openFigen
此处演示HttpComponents和普通的RestTemplate,下一章演示RestTemplate集合Eureka做负载均衡、以及openFigen方式远程调用
- 创建modules(cloud-rpc-clients)
- 修改pom
<dependencies>
<!-- 提供web 内嵌的Tomcat-->
<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>
</dependency>
<!--热部署的工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
测试httpclient访问 payment接口
- httpclient快速开始请参考:http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html
RestTemplate访问接口
- Spring提供的接口
修改yml
业务类
启动类
测试
封装统一的返回结果
