1、替换 tomcat
首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍
第一步,pom 修改
<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
配置连接池
在默认情况下 spring cloud feign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection,没有使用线程池。
有2种可选的线程池:HttpClient和OKHttp,比较推荐OKHttp,请求封装的非常简单易用,性能也很ok。
配置连接池之后,性能大约能提升15%左右
使用Apache的HTTPClient为例,来为Feign配置连接池
第一步:加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>10.1.0</version>
</dependency>
第二步:添加配置
feign:
httpclient:
# 让feign使用Apache HTTPClient做请求,而不是默认的urlConnection
enabled: true
# feign最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
feign:
okhttp:
enabled: true
httpclient:
enabled: false
max-connections: 1000
max-connections-per-route: 100
开启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