Spring Cloud Feign性能优化代码实例

所属分类: 软件编程 / java 阅读数: 32
收藏 0 赞 0 分享

1、替换 tomcat

首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍第一步,pom 修改去除tomcat

<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>

第二步,配置

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

2、替换 HTTPClient

第一步,加依赖

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>

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

# feign配置
feign:
 hystrix:
  # 在feign中开启hystrix功能,默认情况下feign不开启hystrix功能
  enabled: true
 ## 配置httpclient线程池
 httpclient:
  enabled: true
 okhttp:
  enabled: false

第三步,配置 HTTPClient Bean

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;
  }
}

3、配置 Hystrix

第一步,依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

第二步,配置

# 配置hystrix的参数
hystrix:
 threadpool:
  # default: 默认参数,作用的所有的hystrix的客户端,如果需要对某个具体的接口,可以写接口 方法名称
  default:
   coreSize: 500
 command:
  default:
   fallback:
    # 是否开启回退方法
    enabled: true
   execution:
    isolation:
     thread:
      timeoutInMilliseconds: 30000 #缺省为1000

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多