Spring cloud Zuul 参数调优

zuul 参数调优

适用版本:
spring-boot: 1.4.x.RELEASE
spring-cloud:Camden.SR3
Hystrix: 1.5.6

spring-boot-tomcat 优化参数:

主要只有2个,最大和最小worker线程:

server.tomcat.max-threads=128 # Maximum amount of worker threads.

server.tomcat.min-spare-threads=64 # Minimum amount of worker threads.

spring-boot-undertow 优化参数:

ioThreads

设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接,默认取CPU核心数量,最小值为2。
Math.max(Runtime.getRuntime().availableProcessors(), 2);
spring-boot 参数:server.undertow.io-threads=

worker-threads

阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载,默认值为io-threads*8。

spring-boot 参数:server.undertow.worker-threads=

buffer

buffer-size:

每块buffer的空间大小,越小的空间被利用越充分。

buffers-per-region:

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

directBuffers

是否分配的直接内存。

获取JVM最大可用内存maxMemory=Runtime.getRuntime().maxMemory();

maxMemory<64M,不开启directBuffers, bufferSize = 512,buffersPerRegion = 10;

64<=maxMemory<128M,开启directBuffers, bufferSize = 1024 bytes,buffersPerRegion = 10;

maxMemory>128M,开启directBuffers, bufferSize = 16*1024 bytes,buffersPerRegion = 20;

spring-boot 参数:

# 最大可用内存<64M,不开启

server.undertow.buffer-size= # Size of each buffer in bytes.

server.undertow.buffers-per-region= # Number of buffer per region.

server.undertow.direct-buffers= # Allocate buffers outside the Java heap.

//默认值:cpu数量,最小为2

server.undertow.io-threads= # Number of I/O threads to create for the worker.

//默认值:io-threads*8

server.undertow.worker-threads= # Number of worker threads.

zuul 内置参数

zuul.host.maxTotalConnections

适用于ApacheHttpClient,如果是okhttp无效。每个服务的http客户端连接池最大连接,默认是200.

zuul.host.maxPerRouteConnections

适用于ApacheHttpClient,如果是okhttp无效。每个route可用的最大连接数,默认值是20。

zuul.semaphore.max-semaphores

Hystrix最大的并发请求execution.isolation.semaphore.maxConcurrentRequests,这个值并非TPSQPSRPS等都是相对值,指的是1秒时间窗口内的事务/查询/请求,semaphore.maxConcurrentRequests是一个绝对值,无时间窗口,相当于亚毫秒级的。当请求达到或超过该设置值后,其其余就会被拒绝。默认值是100。参考: Hystrix semaphore和thread隔离策略的区别及配置参考

这个参数本来直接可以通过Hystrix的命名规则来设置,但被zuul重新设计了,使得在zuul中semaphores的最大并发请求有4个方法的参数可以设置,如果4个参数都存在优先级(1~4)由高到低:

  • [优先级1]zuul.eureka.api.semaphore.maxSemaphores
  • [优先级2]zuul.semaphore.max-semaphores
  • [优先级3]hystrix.command.api.execution.isolation.semaphore.maxConcurrentRequests
  • [优先级4]hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests

需要注意的是:在Camden.SR3版本的zuul中hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests设置不会起作用,这是因为在org.springframework.cloud.netflix.zuul.filters.ZuulProperties.HystrixSemaphore.maxSemaphores=100设置了默认值100,因此zuul.semaphore.max-semaphores的优先级高于hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests

zuul.eureka.[commandKey].semaphore.maxSemaphores:
其中commandKey为

参考设置参数:

#

zuul.host.maxTotalConnections: 200

zuul.host.maxPerRouteConnections: 10

#zuul.semaphore.max-semaphores: 128

# 建议使用这种方式来设置,可以给每个不同的后端微服务设置不同的信号量

zuul.eureka.[service id].semaphore.maxSemaphores: 128

其他Hystrix参数:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds用来设置thread和semaphore两种隔离策略的超时时间,默认值是1000。

  • 建议设置这个参数,在Hystrix 1.4.0之前,semaphore-isolated隔离策略是不能超时的,从1.4.0开始semaphore-isolated也支持超时时间了。
  • 建议通过CommandKey设置不同微服务的超时时间,对于zuul而言,CommandKey就是service id:hystrix.command.[CommandKey].execution.isolation.thread.timeoutInMilliseconds

ribbon参数

ribbon:

# # Max number of next servers to retry (excluding the first server)

# MaxAutoRetries: 1

# # Whether all operations can be retried for this client

# MaxAutoRetriesNextServer: 1

# # Interval to refresh the server list from the source

# OkToRetryOnAllOperations: true

# # Interval to refresh the server list from the source

# ServerListRefreshInterval: 2000

# # Connect timeout used by Apache HttpClient

ConnectTimeout: 3000

# # Read timeout used by Apache HttpClient

ReadTimeout: 3000

主要是ConnectTimeoutReadTimeout2个参数,最终会设置到http Client中。

注意这个参数很重要了,会配合execution.isolation.thread.timeoutInMilliseconds一起来用,多少合适要根据微服务实际情况来定:

  • 太小:会导致很多正常业务失败
  • 太大:会导致实际的熔断效果很差,严重会导致雪崩。

一般实际大小为:

  • 要保证该服务的可用性为几个9?99.5 99.9 99.99
  • 要保证的N个9的最大响应时间。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为一个 AI,我没有实际的项目经历,但我可以给你一些关于 Spring Cloud 项目的常见经历和使用情况。 Spring Cloud 是一个用于构建分布式系统的开发工具集合,它提供了一系列的组件和库,用于简化分布式系统开发、部署和管理。Spring Cloud 提供了诸如服务注册与发现、负载均衡、断路器、配置管理等功能,使得开发者能够快速构建和管理分布式系统。 在实际项目中,使用 Spring Cloud 可以实现微服务架构,将大型的单体应用拆分成多个小的、独立的服务。每个服务都有自己的代码库、数据库和部署环境,它们之间通过 RESTful API 或消息队列进行通信。这种架构使得系统更加模块化、可扩展和可维护。 在使用 Spring Cloud 进行项目开发时,通常会选择一些核心组件来实现不同的功能。例如,使用 Eureka 实现服务注册与发现,使用 Ribbon 实现负载均衡,使用 Hystrix 实现断路器模式等。此外,还可以使用 Config Server 实现配置中心、使用 Feign 实现声明式服务调用、使用 Zuul 实现 API 网关等。 Spring Cloud 还与其他技术栈和工具集成,如 Spring Boot、Netflix OSS、Docker 等,从而提供了更多的功能和选择。使用这些技术和工具,开发者可以更加高效地构建、测试和部署分布式系统。 总的来说,Spring Cloud 在实际项目中的应用非常广泛,可以帮助开发者快速搭建分布式系统,并提供了丰富的功能和工具来简化开发和管理。但是需要注意的是,使用 Spring Cloud 也需要考虑一些挑战和复杂性,如服务注册与发现的稳定性、负载均衡的配置和调优等。因此,在使用 Spring Cloud 时,需要根据实际情况进行选择和权衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值