繁体   English   中英

SpringBoot FeignClient 与 WebClient

[英]SpringBoot FeignClient vs WebClient

我想使用几个 rest 服务。 之前我用过RestTemplate,但现在我想知道SpringBoot FeignClient 和WebClient 的主要区别是什么? 什么时候应该使用它们?

主要区别在于 WebClient 支持响应式调用。 您可以使用https://github.com/Playtika/feign-reactive 之类的 3rd 方 feign 客户端来实现这一点,但基本上对于一种反应方式,您应该考虑将 WebClient 与一些整洁的异步连接器(如 Jetty)一起使用。 另一方面,如果您想要一种麻烦最少的阻塞方式,那么 Feign 可能是您的最佳选择。

为了能够回答“何时” ,需要了解每个人的能力。

Spring WebClient是一个非阻塞响应式客户端,用于发出 HTTP 请求。 Hence if you intend to use Spring Reactive Stream API to stream data asynchronously then this is the way to go. 想想事件驱动的架构。 WebClient 是Spring WebFlux库的一部分。

[Feign] 3是一个声明性的 REST 库,它使用基于注释的体系结构和每个请求的线程 model。 这意味着线程将阻塞,直到 feign 客户端收到响应。 阻塞代码的问题是它必须等到消费线程完成,因此认为 memory 和 CPU 周期。

因此,当需要非阻塞 HTTP 请求时,请使用 Spring WebClient ,否则由于使用简单model ,所以使用 Feign。

(注:没有理由不能使用 WebClient 进行阻塞操作,但 Feign 更成熟,基于注解的 model 更容易)

WebClient 是一个非阻塞响应式。

Feign 挡住了。

可以通过多种方式从 Spring 应用程序调用 rest 服务。 您是否检查过 spring 提供的 RestTemplate class? 它非常易于使用。

private static void getEmployees()
{
    RestTemplate restTemplate = new RestTemplate();
 
    String result = restTemplate.getForObject("http://api.example.com", String.class);
    System.out.println(result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM