💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# :-: Spring Cloud性能调优 * [一、zuul并发调优](https://www.kancloud.cn/zlt2000/microservices-platform/989550#zuul_1) * [1\. 修改隔离策略](https://www.kancloud.cn/zlt2000/microservices-platform/989550#1__3) * [2\. 熔断器并发调优](https://www.kancloud.cn/zlt2000/microservices-platform/989550#2__19) * [3\. 使用Undertow代替Tomcat](https://www.kancloud.cn/zlt2000/microservices-platform/989550#3_UndertowTomcat_41) * [3.1. 移除Tomcat 依赖](https://www.kancloud.cn/zlt2000/microservices-platform/989550#31_Tomcat__48) * [3.2. 增加Untertow 依赖](https://www.kancloud.cn/zlt2000/microservices-platform/989550#32_Untertow__62) * [3.3. 配置文件加上Untertow的配置](https://www.kancloud.cn/zlt2000/microservices-platform/989550#33_Untertow_70) * [二、Feign参数调优](https://www.kancloud.cn/zlt2000/microservices-platform/989550#Feign_85) * [1\. 替换OKHttp](https://www.kancloud.cn/zlt2000/microservices-platform/989550#1_OKHttp_86) * [1.1. 添加依赖](https://www.kancloud.cn/zlt2000/microservices-platform/989550#11__90) * [1.2. 修改配置文件](https://www.kancloud.cn/zlt2000/microservices-platform/989550#12__97) * [2\. 开启Feign请求响应压缩](https://www.kancloud.cn/zlt2000/microservices-platform/989550#2_Feign_110) * [三、Ribbon参数调优](https://www.kancloud.cn/zlt2000/microservices-platform/989550#Ribbon_122) ## 一、zuul并发调优 zuul默认是使用`semaphore`隔离,并且最大的并发默认是10 ### 1\. 修改隔离策略 默认情况下推荐使用 thread 隔离策略 > 线程池提供了比信号量更好的隔离机制,并且从实际测试发现高吞吐场景下可以完成更多的请求。但是信号量隔离的开销更小,对于本身就是10ms以内的系统,显然信号量更合适 ~~~ zuul: ribbon-isolation-strategy: thread ribbon: threadPool: useSeparateThreadPools: true threadPoolKeyPrefix: api-gateway ~~~ > **ribbon-isolation-strategy**:修改线程隔离策略**useSeparateThreadPools**:让每个路由使用独立的线程池**threadPoolKeyPrefix**:线程池前缀 ### 2\. 熔断器并发调优 修改熔断器的线程数量,注意线程数不是越多越好 ~~~ hystrix: threadpool: default: coreSize: 100 maximumSize: 2000 allowMaximumSizeToDivergeFromCoreSize: true maxQueueSize: -1 command: default: execution: isolation: thread: timeoutInMilliseconds: 60000 ~~~ > **allowMaximumSizeToDivergeFromCoreSize**:允许maximumSize起作用**maxQueueSize**:如该值为-1,那么使用的是SynchronousQueue,否则使用的是LinkedBlockingQueue**timeoutInMilliseconds**:断路器的超时时间;如果ribbon配置了重试那么该值必需大于ribbonTimeout,重试才能生效 ### 3\. 使用Undertow代替Tomcat 默认情况下,Spring Boot 使用 Tomcat 来作为内嵌的 Servlet 容器,可以将 Web 服务器切换到 Undertow 来提高应用性能,Undertow 是红帽公司开发的一款**基于 NIO 的高性能 Web 嵌入式服务器**Untertow 的特点: * **轻量级**:它是一个 Web 服务器,但不像传统的 Web 服务器有容器概念,它由两个核心 Jar 包组成,加载一个 Web 应用可以小于 10MB 内存 * **Servlet3.1 支持**:它提供了对 Servlet3.1 的支持 * **WebSocket 支持**:对 Web Socket 完全支持,用以满足 Web 应用巨大数量的客户端 * **嵌套性**:它不需要容器,只需通过 API 即可快速搭建 Web 服务 #### 3.1. 移除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> ~~~ #### 3.2. 增加Untertow 依赖 ~~~ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> ~~~ #### 3.3. 配置文件加上Untertow的配置 ~~~ server: undertow: io-threads: 16 worker-threads: 256 buffer-size: 1024 direct-buffers: true ~~~ > **io-threads**:设置IO线程数,它主要执行非阻塞的任务,默认会取值cpu核心**worker-threads**:阻塞任务线程池,当执行类似servlet请求阻塞IO操作会从这个线程池中取得线程,默认值是IO线程数\*8**buffer-size**:设置buffer大小,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理**direct-buffers**:是否分配的直接内存(NIO直接分配的堆外内存) ## 二、Feign参数调优 ### 1\. 替换OKHttp 在默认情况下 spring cloud feign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection,没有使用线程池。有2种可选的线程池:HttpClient和OKHttp比较推荐OKHttp,请求封装的非常简单易用,性能也很ok。 #### 1.1. 添加依赖 ~~~ <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> ~~~ #### 1.2. 修改配置文件 ~~~ feign: okhttp: enabled: true httpclient: enabled: false max-connections: 1000 max-connections-per-route: 100 ~~~ > **max-connections**:最大连接数**max-connections-per-route**:每个url的连接数 ### 2\. 开启Feign请求响应压缩 开启压缩可以有效节约网络资源,但是会增加CPU压力,建议把最小压缩的文档大小适度调大一点 ~~~ ## 开启Feign请求响应压缩 feign.compression.request.enabled=true feign.compression.response.enabled=true ## 配置压缩文档类型及最小压缩的文档大小 feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048 ~~~ ## 三、Ribbon参数调优 主要调整请求的超时时间,是否重试 > 如果业务没有做幂等性的话建议把重试关掉ribbon.MaxAutoRetriesNextServer=0 ~~~ ## 从注册中心刷新servelist的时间 默认30秒,单位ms ribbon.ServerListRefreshInterval=15000 ## 请求连接的超时时间 默认1秒,单位ms ribbon.ConnectTimeout=30000 ## 请求处理的超时时间 默认1秒,单位ms ribbon.ReadTimeout=30000 ## 对所有操作请求都进行重试,不配置这个MaxAutoRetries不起作用 默认false #ribbon.OkToRetryOnAllOperations=true ## 对当前实例的重试次数 默认0 #ribbon.MaxAutoRetries=1 ## 切换实例的重试次数 默认1 ribbon.MaxAutoRetriesNextServer=0 ~~~ > 如果`MaxAutoRetries=1`和`MaxAutoRetriesNextServer=1`请求在1s内响应,超过1秒先同一个服务器上重试1次,如果还是超时或失败,向其他服务上请求重试1次。那么整个ribbon请求过程的超时时间为:**ribbonTimeout = (ribbonReadTimeout + ribbonConnectTimeout) \* (maxAutoRetries + 1) \* (maxAutoRetriesNextServer + 1)**