1. 程式人生 > >Feign http 請求跟蹤—亂碼及連線池

Feign http 請求跟蹤—亂碼及連線池

開發十年,就只剩下這套架構體系了! >>>   

 

feign使用http client配置見上文:Feign http 請求跟蹤

 

至此遇到問題

1,feign 使用Apache Http Client 亂碼

2,預設執行緒池管理不符合業務要求

 

解決方法:

1,feign 使用Apache Http Client 亂碼

記得某issue有對Content-Type做相容,配置了下確實可以

配置feign 攔截器,設定Content-type

@Configuration
public class FeignHeaderInterceptor implements RequestInterceptor {
    RequestContext context = RequestContext.getContext();
    requestTemplate.header("Content-Type", "application/json; charset=UTF-8");
}

2,預設執行緒池管理不符合業務要求

預設feign http client配置類HttpClientFeignLoadBalancedConfiguration,對於一些主機連線配置預設值,效能較差的系統不太適合,需要重寫下

@Bean
@ConditionalOnMissingBean(HttpClientConnectionManager.class)
public HttpClientConnectionManager connectionManager(
      ApacheHttpClientConnectionManagerFactory connectionManagerFactory,
      FeignHttpClientProperties httpClientProperties) {
   final HttpClientConnectionManager connectionManager = connectionManagerFactory
         .newConnectionManager(httpClientProperties.isDisableSslValidation(), httpClientProperties.getMaxConnections(),
               httpClientProperties.getMaxConnectionsPerRoute(),
               httpClientProperties.getTimeToLive(),
               httpClientProperties.getTimeToLiveUnit(), registryBuilder);
   this.connectionManagerTimer.schedule(new TimerTask() {
      @Override
      public void run() {
         connectionManager.closeExpiredConnections();
      }
   }, 30000, httpClientProperties.getConnectionTimerRepeat());
   return connectionManager;
}

 

自定配置及客戶端,系統採用ribbon對基本http 請求引數配置,這邊直接複用

/**
 * feign http client 配置
 * @author yugj
 * @date 2019/3/14 上午11:09.
 */
@Configuration
public class FeignApacheHttpClientConfiguration {
​
    @Value("${ribbon.MaxConnectionsPerHost}")
    private Integer maxConnectionPerHost;
    @Value("${ribbon.MaxTotalConnections}")
    private Integer maxTotalConnections;
​
    @Value("${ribbon.ReadTimeout}")
    private Integer readTimeout;
    @Value("${ribbon.ConnectTimeout}")
    private Integer connectTimeout;
​
​
    private final ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2,
            new BasicThreadFactory.Builder().namingPattern("apache-connection-manager-timer-%d").daemon(true).build());
​
    /**
     * 連線池管理配置
     * 替換HttpClientFeignLoadBalancedConfiguration HttpClientConnectionManager 預設配置
     * @return manager
     */
    @Bean
    public HttpClientConnectionManager httpClientConnectionManager() {
​
        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS);
​
        connectionManager.setMaxTotal(maxTotalConnections);
        connectionManager.setDefaultMaxPerRoute(maxConnectionPerHost);
​
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                connectionManager.closeExpiredConnections();
            }
        }, 20000, FeignHttpClientProperties.DEFAULT_CONNECTION_TIMER_REPEAT,TimeUnit.MILLISECONDS);
​
​
​
        return connectionManager;
​
    }
​
    /**
     * http client bean
     * @param httpClientConnectionManager connectionManager
     * @return client
     */
    @Bean
    public CloseableHttpClient httpClient(HttpClientConnectionManager httpClientConnectionManager) {
​
        HttpClientBuilder builder = HttpClientBuilder.create().disableCookieManagement().useSystemProperties();
​
        //預設請求配置 一般不需配置,對應client在包裝使用時會重寫對應值
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setConnectTimeout(connectTimeout)
                .setSocketTimeout(readTimeout)
                .build();
​
        CloseableHttpClient httpClient = builder.
                setDefaultRequestConfig(defaultRequestConfig).
                setConnectionManager(httpClientConnectionManager).
                setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).build();
​
        return httpClient;