Spring Cloud进阶之路 | 十七:服务网关性能调优(zuul)

前言

前面网关的相关文章已经说明了网关的地位,既然网关如此重要,便更要保障网关的稳定、可靠运行。所谓的稳定、可靠无非几个方面,如集群容灾、性能调优、负载均衡等。本文便针对性能调优,调优网关工程的部分参数,优化网关性能表现。

准备工作

复用上一篇文章Spring Cloud进阶之路 | 十六:服务网关集成断路器监控(zuul + Hystrix Dashboard)中的全部工程:xmall-product、xmall-auth、xmall-gateway。

参数调优

zuul网关的参数调优,有以下几个方面:容器、zuul、hystrix、ribbon、feign等,下面一一说明。

容器(tomcat)

以tomcat为例,调整max-threads、min-spare-threads、max-connections、max-http-header-size。具体值可自行根据压力测试结果,逐步调整。

server:
  tomcat:
    max-threads: 2000
    min-spare-threads: 400
    max-connections: 30000
  max-http-header-size: 10MB

zuul

调整host相关参数,max-per-route-connections、max-total-connections、connect-timeout-millis、socket-timeout-millis、connection-request-timeout-millis、time-to-live。如果需要改变时间单位,可调整time-unit参数

zuul:
  host:
    max-per-route-connections: 500
    max-total-connections: 6000
    connect-timeout-millis: 70000
    socket-timeout-millis: 60000
#    connection-request-timeout-millis: -1
#    time-to-live: -1
#    time-unit: milliseconds

调整hystrix相关参数

zuul中hystrix默认隔离策略为SEMAPHORE。如hystrix隔离策略使用线程池,需要调整ribbon-isolation-strategy参数为THREAD。如果为信号量,则为SEMAPHORE如隔离策略为THREAD,则需要配置hystrix相关参数(见下面章节)。如隔离策略为SEMAPHORE,可通过调整zuul.semaphore.max-semaphores实现。

zuul:
# hystrix 隔离策略
  ribbon-isolation-strategy: thread
#  zuul hystrix semaphore配置
#  semaphore:
#    max-semaphores: 5000
​
#  zuul hystrix thread pool配置
​
#  thread-pool:
#    use-separate-thread-pools: false
#    thread-pool-key-prefix: test-

hystrix

默认隔离策略为THREAD。通过参数hystrix.command.default.execution.isolation.strategy调整。其它详细配置,可以参见HystrixCommandProperties。

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD

THREAD隔离策略下,需优化coreSize、maximumSize、maxQueueSize,具体值可通过压力测试结果,逐步调整。

# hystrix 默认线程池配置
  threadpool:
    default:
      coreSize: 600
      maximumSize: 1000
      maxQueueSize: -1

另外,需要优化timeoutInMilliseconds值。

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 300000

注:中的该值计算方式见文章Hystrix超时时间小于ribbon超时时间报错介绍。

ribbon

需优化MaxConnectionsPerHost、MaxTotalConnections、ConnectTimeout、ReadTimeout、MaxAutoRetries、MaxAutoRetriesNextServer。注意,其中的ConnectTimeout、ReadTimeout、MaxAutoRetries、MaxAutoRetriesNextServer事关hystrix.timeoutInMilliseconds计算,具体参见文章Hystrix超时时间小于ribbon超时时间报错中的介绍。详细配置,可参见DefaultClientConfigImpl、IClientConfig、IClientConfigKey。

ribbon:
  MaxConnectionsPerHost: 500
  MaxTotalConnections: 2000
  ConnectTimeout: 60000
  ReadTimeout: 60000
#  MaxAutoRetries: 0
#  MaxAutoRetriesNextServer: 1

feign

首先需要优化feign客户端配置中的connectTimeout、readTimeout。详细配置参见FeignClientConfiguration。

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 60000
        readTimeout: 40000
        loggerLevel: full

如果需要单独为某服务设置,可将服务名替换掉default即可。

feign:
  hystrix:
    enabled: true
  client:
    config:
      xmall-auth:
        connectTimeout: 60000
        readTimeout: 40000
        loggerLevel: full

关于feign.client实现方式,可选择okhttp。但是,无论选择哪种实现方式,均需优化connection-timeout、max-connections、max-connections-per-route。以okhttp为例:

feign:
  httpclient:
    enabled: false
    connection-timeout: 60000
    max-connections: 2000
    max-connections-per-route: 500
  # 启用okhttp
  okhttp:
    enabled: true

最终配置

以本系列工程为例,根据压力SOAPUI 60s 并发压力测试结果,调整后的参数如下,笔者尝试的结果是:60s 10000线程,8G内存,单台示例,可无压力。

server:
  port: 5566
  tomcat:
    max-threads: 2000
    min-spare-threads: 400
    max-connections: 30000
  max-http-header-size: 10MB
​
zuul:
  prefix: /gateway
  sensitive-headers:
  routes:
    auth:
      path: /auth/**
      service-id: xmall-auth
      strip-prefix: true
    product:
      path: /product/**
      service-id: xmall-product
      strip-prefix: true
  host:
    max-per-route-connections: 500
    max-total-connections: 6000
    connect-timeout-millis: 70000
    socket-timeout-millis: 60000
#    connection-request-timeout-millis: -1
#    time-to-live: -1
#    time-unit: milliseconds
# hystrix 隔离策略
  ribbon-isolation-strategy: thread
#  zuul hystrix semaphore配置
#  semaphore:
#    max-semaphores: 5000
​
#  zuul hystrix thread pool配置
​
#  thread-pool:
#    use-separate-thread-pools: false
#    thread-pool-key-prefix: test-
​
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 300000
# hystrix 默认线程池配置
  threadpool:
    default:
      coreSize: 600
      maximumSize: 1000
      maxQueueSize: -1
​
#  hystrix 服务自定义线程池配置
#    xmall-auth:
#      coreSize: 600
#      maximumSize: 1000
#      maxQueueSize: -1
​
# ribbon配置
ribbon:
  MaxConnectionsPerHost: 500
  MaxTotalConnections: 2000
  ConnectTimeout: 60000
  ReadTimeout: 60000
​
security:
  oauth2:
    resource:
      user-info-uri: http://localhost:7777/oauth/user
      prefer-token-info: false
​
feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 60000
        readTimeout: 40000
        loggerLevel: full
  httpclient:
    enabled: false
    connection-timeout: 60000
    max-connections: 2000
    max-connections-per-route: 500
  # 启用okhttp
  okhttp:
    enabled: true
​
management:
  endpoints:
    web:
      exposure:
        include: '*'

源码

github

https://github.com/liuminglei/SpringCloudLearning/tree/master/17/

gitee

https://gitee.com/xbd521/SpringCloudLearning/tree/master/17/

本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。

微信搜索【银河架构师】,发现更多精彩内容。

发布了30 篇原创文章 · 获赞 1 · 访问量 2429

猜你喜欢

转载自blog.csdn.net/liuminglei1987/article/details/104282000