在以编程方式创建的 feign 客户端上获取 Spring 默认 Feign 选项

发布于 2025-01-10 05:26:00 字数 1033 浏览 0 评论 0原文

我正在尝试获取默认的 feign 选项/配置,以在我使用 Feign.Builder 以编程方式创建的 feign 客户端中使用它:

这是我想要获取的配置(application.yaml):

feign:
  client:
    config:
      default:
        connect-timeout: 5000
        read-timeout: 5000

我试图做的是添加 @EnableFeignClients 并尝试获取 FactoryBean或者它是OptionsFactoryBean的实现,但我没有看到它被Spring注入到任何地方。

我还尝试在 StackOverflow 和其他网站中搜索,看看是否有其他人尝试过我想做的事情,但我无法找到任何信息,因此我创建了这个问题。

public MyClientFactory(Client client,
                       ObjectMapper objectMapper,
                       FactoryBean<Request.Options> optionsFactory,
                       ErrorDecoder errorDecoder) throws Exception {
    this.builder = Feign.builder()
            .client(client)
            .decoder(new JacksonDecoder(objectMapper))
            .encoder(new JacksonEncoder(objectMapper))
            .options(optionsFactory.getObject())
            .errorDecoder(errorDecoder);
}

有人可以让我知道如何获得默认的 spring feign 配置吗?也许我的方法不正确?

谢谢!

I am trying to get the default feign options/config to use it in feign clients that I create programatically using Feign.Builder:

This is the config (application.yaml) that I want to get:

feign:
  client:
    config:
      default:
        connect-timeout: 5000
        read-timeout: 5000

What I tried to do is to do is to add @EnableFeignClients and try to get the FactoryBean<Request.Options> or it's implementation OptionsFactoryBean but I don't see it being injected by Spring anywhere.

I've also tried searching in StackOverflow and other websites to see if there are other people that have tried what I am trying to do, but I wasn't able to find any information hence why I am creating this question.

public MyClientFactory(Client client,
                       ObjectMapper objectMapper,
                       FactoryBean<Request.Options> optionsFactory,
                       ErrorDecoder errorDecoder) throws Exception {
    this.builder = Feign.builder()
            .client(client)
            .decoder(new JacksonDecoder(objectMapper))
            .encoder(new JacksonEncoder(objectMapper))
            .options(optionsFactory.getObject())
            .errorDecoder(errorDecoder);
}

Can someone please let me know how I can get the default spring feign configs? Maybe my approach is incorrect?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2025-01-17 05:26:00

还没有尝试过,但我假设你需要来自 FeignClientProperties 类。

Haven't tried it but I assume you'll need a bean from the FeignClientProperties class.

丑丑阿 2025-01-17 05:26:00

我最终根据 @Arnold Galovics 建议创建了一个基于 FeignClientProperties 的 Request.Options @Bean 。

@Bean
Request.Options options(FeignClientProperties feignClientProperties) {
    FeignClientProperties.FeignClientConfiguration feignClientConfiguration = feignClientProperties.getConfig().get(feignClientProperties.getDefaultConfig());

    Request.Options defaultOptions = new Request.Options();
    return new Request.Options(
            Optional.ofNullable(feignClientConfiguration.getConnectTimeout()).orElse(defaultOptions.connectTimeoutMillis()), TimeUnit.MILLISECONDS,
            Optional.ofNullable(feignClientConfiguration.getReadTimeout()).orElse(defaultOptions.readTimeoutMillis()), TimeUnit.MILLISECONDS,
            Optional.ofNullable(feignClientConfiguration.isFollowRedirects()).orElse(defaultOptions.isFollowRedirects())
    );
}

然后我将这个 bean 注入到我的 Feign Client Factory 中:

public MyClientFactory(Client client,
                       ObjectMapper objectMapper,
                       Request.Options options,
                       ErrorDecoder errorDecoder) {
    this.builder = Feign.builder()
            .client(client)
            .decoder(new JacksonDecoder(objectMapper))
            .encoder(new JacksonEncoder(objectMapper))
            .options(options)
            .errorDecoder(errorDecoder);
}

I ended up creating a Request.Options @Bean based on FeignClientProperties as @Arnold Galovics suggested.

@Bean
Request.Options options(FeignClientProperties feignClientProperties) {
    FeignClientProperties.FeignClientConfiguration feignClientConfiguration = feignClientProperties.getConfig().get(feignClientProperties.getDefaultConfig());

    Request.Options defaultOptions = new Request.Options();
    return new Request.Options(
            Optional.ofNullable(feignClientConfiguration.getConnectTimeout()).orElse(defaultOptions.connectTimeoutMillis()), TimeUnit.MILLISECONDS,
            Optional.ofNullable(feignClientConfiguration.getReadTimeout()).orElse(defaultOptions.readTimeoutMillis()), TimeUnit.MILLISECONDS,
            Optional.ofNullable(feignClientConfiguration.isFollowRedirects()).orElse(defaultOptions.isFollowRedirects())
    );
}

And then I injected this bean in my Feign Client Factory:

public MyClientFactory(Client client,
                       ObjectMapper objectMapper,
                       Request.Options options,
                       ErrorDecoder errorDecoder) {
    this.builder = Feign.builder()
            .client(client)
            .decoder(new JacksonDecoder(objectMapper))
            .encoder(new JacksonEncoder(objectMapper))
            .options(options)
            .errorDecoder(errorDecoder);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文