1、RPC
基于socker
,速度快,效率高;代表:webService、dubbo
2、HTTP
基于TCP协议
;封装比较臃肿;对服务和调用方没有任何技术、语言的限定;自由灵活,更加符合微服务的理念
;代表:Rest API、SpringCloud
通过以下三种 http 客户端的工具类包,可以进行 http 服务调用
httpClient
okHttp
JDK原生的URLConnection
Spring
提供了 RestTemplate 工具类
对上述三种工具类包进行了封装
在SpringBoot的启动类中,注入RestTemplate 的Bean
/**
* @Author: Chengqb
* @Date: Created in 11:36 2019/12/4
* @MethodName: restTemplate
* @param: []
* @return: org.springframework.web.client.RestTemplate
* @Description: 注册一个RestTemplate组件, 已达到在各个模块间互相调用的效果
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
在需要的地方进行调用
String url = "http://user-service/user/" + id;
String userVO = restTemplate.getForObject(url, String.class);