当前位置:   article > 正文

Spring Cloud Feign 性能优化_feigh调用会影响性能吗

feigh调用会影响性能吗

网上看到的,到底性能如何暂时不知道,暂时先存一下,之后会测试一下

关键字:com.netflix.hystrix.exception.HystrixRuntimeException; timeout; could not be queued for execution

1、替换 tomcat

首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍

第一步,pom 修改

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-undertow</artifactId>
  14. </dependency>

第二步,配置

  1. server:
  2. undertow:
  3. max-http-post-size: 0
  4. # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,数量和CPU 内核数目一样即可
  5. io-threads: 4
  6. # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载 io-threads*8
  7. worker-threads: 32
  8. # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
  9. # 每块buffer的空间大小,越小的空间被利用越充分
  10. buffer-size: 1024
  11. # 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region
  12. # buffers-per-region: 1024 # 这个参数不需要写了
  13. # 是否分配的直接内存
  14. direct-buffers: true

2、替换 HTTPClient

其次把普通的 HTTPURLConnection 替换为使用 HTTPClient
第一步 pom 增加对应的包

  1. <dependency>
  2. <groupId>io.github.openfeign</groupId>
  3. <artifactId>feign-httpclient</artifactId>
  4. </dependency>

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

  1. # feign配置
  2. feign:
  3. hystrix:
  4. # 在feign中开启hystrix功能,默认情况下feign不开启hystrix功能
  5. enabled: true
  6. ## 配置httpclient线程池
  7. httpclient:
  8. enabled: true
  9. okhttp:
  10. enabled: false

第三步,配置 HTTPClient Bean

  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3. import java.util.concurrent.TimeUnit;
  4. import org.apache.http.client.HttpClient;
  5. import org.apache.http.client.config.RequestConfig;
  6. import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
  7. import org.apache.http.impl.client.HttpClientBuilder;
  8. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. @Configuration
  12. public class HttpPool {
  13. @Bean
  14. public HttpClient httpClient(){
  15. System.out.println("===== Apache httpclient 初始化连接池开始===" );
  16. // 生成默认请求配置
  17. RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
  18. // 超时时间
  19. requestConfigBuilder.setSocketTimeout(5 * 1000);
  20. // 连接时间
  21. requestConfigBuilder.setConnectTimeout(5 * 1000);
  22. RequestConfig defaultRequestConfig = requestConfigBuilder.build();
  23. // 连接池配置
  24. // 长连接保持30
  25. final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS);
  26. // 总连接数
  27. pollingConnectionManager.setMaxTotal(1000);
  28. // 同路由的并发数
  29. pollingConnectionManager.setDefaultMaxPerRoute(100);
  30. // httpclient 配置
  31. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
  32. // 保持长连接配置,需要在头添加Keep-Alive
  33. httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
  34. httpClientBuilder.setConnectionManager(pollingConnectionManager);
  35. httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig);
  36. HttpClient client = httpClientBuilder.build();
  37. // 启动定时器,定时回收过期的连接
  38. Timer timer = new Timer();
  39. timer.schedule(new TimerTask() {
  40. @Override
  41. public void run() {
  42. System.out.println("=====closeIdleConnections===");
  43. pollingConnectionManager.closeExpiredConnections();
  44. pollingConnectionManager.closeIdleConnections(5, TimeUnit.SECONDS);
  45. }
  46. }, 10 * 1000, 5 * 1000);
  47. System.out.println("===== Apache httpclient 初始化连接池完毕===");
  48. return client;
  49. }
  50. }

3、配置 Hystrix

第一步,还是导入包

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-hystrix</artifactId>
  4. </dependency>

第二步,配置

  1. # 配置hystrix的参数
  2. hystrix:
  3. threadpool:
  4. # default: 默认参数,作用的所有的hystrix的客户端,如果需要对某个具体的接口,可以写接口+方法名称
  5. default:
  6. coreSize: 500
  7. command:
  8. default:
  9. fallback:
  10. # 是否开启回退方法
  11. enabled: true
  12. execution:
  13. isolation:
  14. thread:
  15. timeoutInMilliseconds: 30000 #缺省为1000

4、压测吧

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/383235
推荐阅读
相关标签
  

闽ICP备14008679号