服务调用方试

服务调用方式

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);
发布了54 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41970025/article/details/105114036