这段时间,由于公司项目重构,原有的SpringMVC+ejb 切换成了springboot + springcloud,在编译调试时遇到了一些问题,导致项目在并发情况下CPU使用较高,通过网上搜寻资料,优化部分配置。
以下是springboot yml配置文件信息:
server:
port: 8080
context-path: /test
spring:
application:
#只有通过bootstrap配置,才能在zipkin中显示在打印的日志中
name: test
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
#spring mvc
mvc:
view.prefix: /WEB-INF/jsp/
view.suffix: .jsp
static-path-pattern: /**
cloud:
consul:
discovery:
#前端只需通过consul调用服务,不需要被发现,所以无需注册
register: false
host: localhost
port: 8500
loadbalancer:
retry:
enabled: false
#文件上传设置
http:
multipart:
max-file-size:4Mb
max-request-size:10Mb
#feign
feign:
httpclient:
enabled: true
max-connections: 400
max-connections-per-route: 100
okhttp:
enabled: false
hystrix:
# 在feign中开启hystrix功能,默认情况下feign不开启hystrix功能
enabled: true
compression:
#请求和响应GZIP压缩支持
request.enabled: true
response.enabled: true
#支持压缩的mime types
request.mime-types: text/xml,application/xml,application/json
request.min-request-size: 2048
#客户端负载均衡
ribbon:
ReadTimeout: 15000 #请求处理的超时时间
ConnectTimeout: 15000 #请求连接的超时时间
MaxAutoRetries: 0 #对当前实例的重试次数
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
OkToRetryOnAllOperations: false #对所有操作请求都进行重试 false
#熔断器
hystrix:
threadpool:
default:
coreSize: 100 #并发执行的最大线程数,默认10
maxQueueSize: 1000 #BlockingQueue的最大队列数
queueSizeRejectionThreshold: 500 #排队线程数量阈值,即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
endpoints:
shutdown:
enabled: true
sensitive: true
restart:
enabled: true
health:
sensitive: false
启动类配置(application):
@Bean
Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
@Bean
Request.Options feignOptions() {
return new Request.Options(/**connectTimeoutMillis**/1 * 10000, /** readTimeoutMillis **/1 * 15000);
}
在没有配置熔断器与连接池的时候,在本地调试和测试的时候,功能没问题,但是并发情况下,会导致cpu过高,造成的原因:
1.重试机制,调用失败就重试,导致请求堆积
2.没有熔断机制,请求过多导致线程不足,tps直接归零
3.未手动配置连接池信息,采用默认数据,没法合理处理请求
4.请求压缩
5.请求数量处理与拦截
配置完成之后,客服端(springboot)调用服务端(springcloud)客户端负载与CPU消耗变的较为正常,tps波动也比较小了。