【问题标题】:How to determine which default http client was injected by Spring container for @FeignClient?如何确定Spring容器为@FeignClient注入了哪个默认http客户端?
【发布时间】:2018-10-01 16:02:34
【问题描述】:

我正在使用@FeignClient 注释。我想知道在运行我的应用程序时 Spring 注入了哪个 httpclient。

例如, OkHttpClient 和 ApacheHttpClient feign 客户端可以通过将 feign.okhttp.enabled 或 feign.httpclient.enabled 分别设置为 true 并将它们放在类路径中来使用。

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "service", path = "/api/v1", configuration = ServiceConfiguration.class)
public interface ServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/test/{param1}", consumes = MediaType.APPLICATION_JSON_VALUE)
    String test(@PathVariable("param1") String param);
}

我现在不确定这些客户端中的哪一个被注入,因为我的应用程序很复杂,在类路径中有多个 httpclient 库。

有什么方法可以监控吗?

我启用了 JMX 并尝试查看 jconsole Mbean,但没有关于 httpclients 的信息。

【问题讨论】:

  • 您是否尝试过在 DEBUG 级别登录?

标签: spring-boot spring-cloud-netflix netflix-feign spring-cloud-feign


【解决方案1】:

如果没有看到您的 Spring 设置就无法判断,特别是如果您的应用程序像您所说的那样复杂。

由于您使用 Spring 注释来声明您的 @FeignClient,因此您很可能依赖于 spring-cloud-netflix-core。如果您使用@EnableFeignClients 启用默认值,请查看FeignAutoConfiguration 类。此类注册实际的 HTTP 客户端 bean。如果您同时添加 feign.httpclient.enabledfeign.okhttp.enabled 属性(IMO 是一个奇怪的设置)尝试调试 FeignAutoConfiguration 以查看哪个 Client feignClient() bean 将在 Spring 上下文中注册。

或者在所有 HTTP 客户端库中启用有线日志记录,并根据日志查看哪个库实际执行了请求。

【讨论】:

    【解决方案2】:

    根据之前的回答,现在我在 FeignAutoConfiguration.java 中看到了这一行

    @ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
    

    如果你不添加任何属性,那么简单的答案就是默认的 Apache 客户端

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 2014-11-04
      • 1970-01-01
      • 2018-05-17
      • 2021-11-27
      • 1970-01-01
      • 2017-12-23
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多