Spring Cloud Feign 性能優化

一、替換 tomcat

首先,把 tomcat 換成 undertow,這個性能在 Jmeter 的壓測下,undertow 比 tomcat 高一倍第一步,pom 修改去除tomcatjava

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>複製代碼

第二步,配置git

server:
  undertow:
    max-http-post-size: 0
# 設置IO線程數, 它主要執行非阻塞的任務,它們會負責多個鏈接, 默認設置每一個CPU核心一個線程,數量和CPU 內核數目同樣便可
    io-threads: 4
# 阻塞任務線程池, 當執行相似servlet請求阻塞操做, undertow會從這個線程池中取得線程,它的值設置取決於系統的負載  io-threads*8
    worker-threads: 32
# 如下的配置會影響buffer,這些buffer會用於服務器鏈接的IO操做,有點相似netty的池化內存管理
# 每塊buffer的空間大小,越小的空間被利用越充分
    buffer-size: 1024
# 每一個區分配的buffer數量 , 因此pool的大小是buffer-size * buffers-per-region
#   buffers-per-region: 1024 # 這個參數不須要寫了
# 是否分配的直接內存
    direct-buffers: true複製代碼

二、替換 HTTPClient

第一步,加依賴github

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>複製代碼

第二部,在 application.yml或者 bootstrap.yml 裏面配置web

# feign配置
feign:
  hystrix:
    # 在feign中開啓hystrix功能,默認狀況下feign不開啓hystrix功能
    enabled: true
  ## 配置httpclient線程池
  httpclient:
    enabled: true
  okhttp:
    enabled: false複製代碼

第三步,配置 HTTPClient Beanspring

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpPool {

    @Bean
    public HttpClient httpClient(){
        System.out.println("===== Apache httpclient 初始化鏈接池開始===" );
        // 生成默認請求配置
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        // 超時時間
        requestConfigBuilder.setSocketTimeout(5 * 1000);
        // 鏈接時間
        requestConfigBuilder.setConnectTimeout(5 * 1000);
        RequestConfig defaultRequestConfig = requestConfigBuilder.build();
        // 鏈接池配置
        // 長鏈接保持30秒
        final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS);
        // 總鏈接數
        pollingConnectionManager.setMaxTotal(1000);
        // 同路由的併發數
        pollingConnectionManager.setDefaultMaxPerRoute(100);

        // httpclient 配置
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 保持長鏈接配置,須要在頭添加Keep-Alive
        httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
        httpClientBuilder.setConnectionManager(pollingConnectionManager);
        httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig);
        HttpClient client = httpClientBuilder.build();

        // 啓動定時器,定時回收過時的鏈接
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("=====closeIdleConnections===");
                pollingConnectionManager.closeExpiredConnections();
                pollingConnectionManager.closeIdleConnections(5, TimeUnit.SECONDS);
            }
        }, 10 * 1000, 5 * 1000);
        System.out.println("===== Apache httpclient 初始化鏈接池完畢===");

        return client;
    }
}複製代碼

三、配置 Hystrix

第一步,依賴apache

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>複製代碼

第二步,配置bootstrap

# 配置hystrix的參數
hystrix:
  threadpool:
    # default: 默認參數,做用的全部的hystrix的客戶端,若是須要對某個具體的接口,能夠寫接口+方法名稱
    default:
      coreSize: 500
  command:
    default:
      fallback:
        # 是否開啓回退方法
        enabled: true
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000 #缺省爲1000複製代碼

原文連接:www.jianshu.com/p/fe1c4412d…趙小胖我的博客tomcat

相關文章
相關標籤/搜索