springcloud配置整理

一、web服务器配置

选用undertow服务器,添加undertow依赖

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-undertow</artifactId>

</dependency>

a、相关配置

server:

  undertow:

    io-threads: 16

    worker-threads: 256

    buffer-size: 1024

    buffers-per-region: 1024

    direct-buffers: true

b、参数解释

server.undertow.io-threads: 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,不要设置过大,如果过大,启动项目会报错:打开文件数过多

server.undertow.worker-threads: 阻塞任务线程池, 当执行类似servlet请求阻塞IO操作, undertow会从这个线程池中取得线程,它的值设置取决于系统线程执行任务的阻塞系数,默认值是IO线程数*8

server.undertow.buffer-size: 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理,每块buffer的空间大小,越小的空间被利用越充分,不要设置太大,以免影响其他应用,合适即可

server.undertow.buffers-per-region: 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region

server.undertow.direct-buffers: 是否分配的直接内存(NIO直接分配的堆外内存)

二、Fegin配置

服务之间的调用,目前比较常用的都是通过openfeign来进行调用,而openfeign是集成有负载均衡ribbon、熔断器hystrix的

1、hystrix熔断

feign默认不启用hystrix,需要手动指定开启熔断

feign.hytrix.enable=true

2、压缩

#启用feign请求压缩

feign.compression.request.enable=true

#压缩的mimetype

feign.compression.request.mime-types=text/xml,application/xml,application/json

#启用feign响应压缩

feign.compression.request.enable=true

3、feign http请求方式选择

feign默认使用的是基于JDK提供的URLConnection调用HTTP接口,不具备连接池,所以资源开销上有点影响,经测试JDK的URLConnection比Apache HttpClient快很多倍。Apache HttpClient和okhttp都支持配置连接池功能,也可以使用okhttp请求方式。

a、当使用HttpClient时,可如下设置:

feign:

  httpclient:

    enabled: true

    max-connections:1000

    max-connections-per-route: 200

b、当使用OKHttp时,可如下设置

feign:

  okhttp:

    enabled: true

  httpclient:

    max-connections: 1000

    max-connections-per-route: 200

参数解释

max-connections 设置整个连接池最大连接数(该值默认为200), 根据自己的场景决定

max-connections-per-route 设置路由的默认最大连接(该值默认为50),限制数量实际使用

三、hystrix配置

a、hystrix隔离配置

##hystrix隔离机制,线程

hystrix.command.default.execution.isolation.strategy=THREAD

#hystrixsamephore隔离极致并发数设置

hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=1000

#开启hystrix超时

hystrix.command.default.execution.timeout.enabled=true

#hystix超时设置  (1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

b、hystrix线程池配置

#hystrix线程隔离线程池核心线程数

hystrix.threadpool.default.coreSize=1000

#hystrix线程隔离线程池最大线程数

hystrix.threadpool.default.maximumSize=1000

#hystrix线程隔离线程池队列大小(-1:SynchronousQueue)

hystrix.threadpool.default.maxQueueSize=10

#允许达到最大线程数

hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true

# ##即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝

hystrix.threadpool.default.queueSizeRejectionThreshold=10

c、Hystrix针对单个接口设置超时时间语法

类名#方法名(参数类型,参数类型……)

类名:要设置的某个FeignClient的类名

方法名:设置FeignClient里面的方法

入参类型:方法里面需要传入的参数。如果是基础类型,就直接填基础类型:String/int等;如果是某个对象,就直接填对象的类名

举例如下:

hystrix.command.UserInfoOpenClient#getUserById(Long).execution.isolation.strategy=THREAD

hystrix.command.UserInfoOpenClient#getUserById(Long).isolation.semaphore.maxConcurrentRequests=1000

hystrix.command.UserInfoOpenClient#getUserById(Long).execution.timeout.enabled=true

hystrix.command.UserInfoOpenClient#getUserById(Long).execution.isolation.thread.timeoutInMilliseconds=1000

d、Hystrix针对单个服务设置超时时间语法

hystrix.command.服务名.execution,举例如下:

hystrix.command.gf-oss-service.execution.isolation.strategy=THREAD

hystrix.command.gf-oss-ser#Zvice.execution.isolation.semaphore.maxConcurrentRequests=1000

hystrix.command.gf-oss-service.execution.timeout.enabled=true

hystrix.command.gf-oss-service.execution.isolation.thread.timeoutInMilliseconds=300000

四、ribbon配置

a、超时时间

# 请求连接的超时时间

ribbon.ConnectTimeout=2000

# 请求处理的超时时间

ribbon.ReadTimeout=5000

#每个Ribbon客户端设置不同的超时时间, 通过服务名称进行指定:

serviceId.ribbon.ConnectTimeout=2000

serviceId.ribbon.ReadTimeout=5000

b、并发参数

# 最大连接数

ribbon.MaxTotalConnections=500

# 每个host最大连接数

ribbon.MaxConnectionsPerHost=500

c、重试

# 对当前实例的重试次数

ribbon.maxAutoRetries=1

# 切换实例的重试次数

ribbon.maxAutoRetriesNextServer=3

# 对所有操作请求都进行重试

ribbon.okToRetryOnAllOperations=true

# 对Http响应码进行重试

ribbon.retryableStatusCodes=500,404,502

d、ribbon与hystrix之间的超时时间关系:

如果hystrix.command.default.execution.timeout.enabled为true,则会有两个执行方法超时的配置,一个就是ribbon的ReadTimeout,一个就是熔断器hystrix的timeoutInMilliseconds, 此时谁的值小谁生效

如果hystrix.command.default.execution.timeout.enabled为false,则熔断器不进行超时熔断,而是根据ribbon的ReadTimeout抛出的异常而熔断,也就是取决于ribbon

ribbon的ConnectTimeout,配置的是请求服务的超时时间,除非服务找不到,或者网络原因,这个时间才会生效

由于ribbon的重试机制,通常熔断的超时时间需要配置的比ReadTimeout长,ReadTimeout比ConnectTimeout长,否则还未重试,就熔断了

为了确保重试机制的正常运作,理论上(以实际情况为准)建议hystrix的超时时间为:(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout

五、gateway配置

System.setProperty("reactor.netty.pool.leasingStrategy", "lifo");

spring.cloud.gateway.httpclient.pool.max-idle-time=1S

#TCP连接超时时间 单位:ms

spring.cloud.gateway.httpclient.connect-timeout=2000

#响应超时时间

spring.cloud.gateway.httpclient.response-timeout=PT30S

#接口级别

spring.cloud.gateway.default-filters[0].args.timeouts[0].apiPattern=/gf-oss-service//oss/part/upload(接口apiPattern)

#毫秒

spring.cloud.gateway.default-filters[0].args.timeouts[0].timeout=100000

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容