no

How to Create a Bean of Type FeignClient

This class creates a bean that returns FeignClient constructed using FeignHttpClientProperties properties.

package com.czetsuyatech.traceability.feign.config;

import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder;
import feign.Client;
import feign.httpclient.ApacheHttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.cloud.openfeign.FeignFormatterRegistrar;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;

@Configuration(proxyBeanMethods = false)
@ConditionalFeignEnabled
public class FeignClientConfiguration {

  @Bean
  public Client client(HttpClientBuilder httpClientBuilder, FeignHttpClientProperties httpClientProperties) {

    httpClientBuilder = httpClientBuilder != null ? httpClientBuilder : HttpClientBuilder.create();

    final PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
        new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
    poolingHttpClientConnectionManager.setMaxTotal(100);

    final RequestConfig defaultRequestConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(100)
        .setConnectTimeout(httpClientProperties.getConnectionTimeout())
        .setSocketTimeout(100)
        .setRedirectsEnabled(httpClientProperties.isFollowRedirects())
        .build();

    return new ApacheHttpClient(
        httpClientBuilder
            .setConnectionManager(poolingHttpClientConnectionManager)
            .setDefaultRequestConfig(defaultRequestConfig)
            .build()
    );
  }

  @Bean
  public FeignFormatterRegistrar localDateFeignFormatterRegister() {

    return registry -> {
      DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
      registrar.setUseIsoFormat(true);
      registrar.registerFormatters(registry);
    };
  }
}

Related

spring-cloud 2820704734195375290

Post a Comment Default Comments

item