FeignClient与RestTemplate的区别比较简单研究

Love The Way You Lie 2022-11-06 05:49 198阅读 0赞

个人觉得可能还没达到那种境界,还体会不到真正的实质性区别,就好比用HttpClient可以实现的用FeignClient同样可以实现,反之也是。

就是后端调用后端。
JAVA 项目中接口调用怎么做 ?

Httpclient
Okhttp
Httpurlconnection
RestTemplate
上面是最常见的几种用法,我们今天要介绍的用法比上面的更简单,方便,它就是Feign

Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单。

Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。

而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。

SpringCloud对Feign进行了封装,使其支持SpringMVC标准注解和HttpMessageConverters。

Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

区别:

在预订微服务中,有一个同步呼叫票价(Fare)。RestTemplate是用来制作的同步呼叫。使用RestTemplate时,URL参数是以编程方式构造的,数据被发送到其他服务。在更复杂的情况下,我们将不得不RestTemplate深入到更低级别的API提供的甚至是API 的细节。

Feign是Spring Cloud Netflix库,用于在基于REST的服务调用上提供更高级别的抽象。Spring Cloud Feign在声明性原则上工作。使用Feign时,我们在客户端编写声明式REST服务接口,并使用这些接口来编写客户端程序。开发人员不用担心这个接口的实现。这将在运行时由Spring动态配置。通过这种声明性的方法,开发人员不需要深入了解由HTTP提供的HTTP级别API的细节的RestTemplate。

总结:

也就是说FeignClient简化了请求的编写,且通过动态负载进行选择要使用哪个服务进行消费,而这一切都由Spring动态配置实现,我们不用关心这些,只管使用方法即可。再说,就是简化了编写,RestTemplate还需要写上服务器IP这些信息等等,而FeignClient则不用。

不过话又再说回来,其实RestTemplate同样可以简化到使用FeignClient那样简单,无非就是自己封装多一层去实现而已,所以,我个人觉得没有太多绝对,只是看你的业务需求怎么定位这个选择而已。

使用Spring Cloud搭建各种微服务之后,服务可以通过@FeignClient使用和发现服务场中的其他服务。
还是以Config Server和Config Client为例,这是服务场中的注册的两个微服务。
Config Server中定义了两个服务接口(一个Post、一个Get方法)

  1. package demo.controller;
  2. import org.springframework.cloud.context.config.annotation.RefreshScope;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import demo.model.User;
  8. @RestController
  9. @RefreshScope
  10. public class Controller {
  11. @GetMapping(value="s")
  12. String helloServer(String username) {
  13. return "Hello " + username + "!";
  14. }
  15. @PostMapping(value="u")
  16. String helloUser(@RequestBody User user) {
  17. return "Hello " + user + "!";
  18. }
  19. }

在Config Client(spring.application.name=test-dev)我们使用Config Server中定义的这两个微服务。

  1. import org.springframework.cloud.netflix.feign.FeignClient;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.PostMapping;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import cn.test.bean.User;
  7. @FeignClient(value = "config-service")
  8. public interface FindService {
  9. @GetMapping(value="s")
  10. String helloServer(@RequestParam("username")String username);
  11. @PostMapping(value="u")
  12. String helloUser(@RequestBody User user);
  13. }

注意,Get方法中通过@RequestParam指定需要传递的参数,应用启动时需添加@EnableFeignClients注解。

  1. @Autowired
  2. private FindService service;
  3. @GetMapping("/find")
  4. String find(String param) {
  5. return "find " + service.helloServer(param);
  6. }
  7. @PostMapping("/f")
  8. String findU(@RequestBody User user) {
  9. return "find " + service.helloUser(user);
  10. }

————————————

https://blog.csdn.net/weixin_43343423/article/details/103075241

发表评论

表情:
评论列表 (有 0 条评论,198人围观)

还没有评论,来说两句吧...

相关阅读