`

spring cloud Zuul网关参数调优

 
阅读更多

最近遇到zuul上面的调优,记录下

 

参数调优

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

容器(tomcat)

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

  1.  
    server:
  2.  
    tomcat:
  3.  
    max-threads: 2000
  4.  
    min-spare-threads: 400
  5.  
    max-connections: 30000
  6.  
    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参数。

  1.  
    zuul:
  2.  
    host:
  3.  
    max-per-route-connections: 500
  4.  
    max-total-connections: 6000
  5.  
    connect-timeout-millis: 70000
  6.  
    socket-timeout-millis: 60000
  7.  
    # connection-request-timeout-millis: -1
  8.  
    # time-to-live: -1
  9.  
    # time-unit: milliseconds

 

调整hystrix相关参数

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

  1.  
    zuul:
  2.  
    # hystrix 隔离策略
  3.  
    ribbon-isolation-strategy: thread
  4.  
    # zuul hystrix semaphore配置
  5.  
    # semaphore:
  6.  
    # max-semaphores: 5000
  7.  
  8.  
    # zuul hystrix thread pool配置
  9.  
  10.  
    # thread-pool:
  11.  
    # use-separate-thread-pools: false
  12.  
    # thread-pool-key-prefix: test-

 

hystrix

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

  1.  
    hystrix:
  2.  
    command:
  3.  
    default:
  4.  
    execution:
  5.  
    isolation:
  6.  
    strategy: THREAD

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

  1.  
    # hystrix 默认线程池配置
  2.  
    threadpool:
  3.  
    default:
  4.  
    coreSize: 600
  5.  
    maximumSize: 1000
  6.  
    maxQueueSize: -1

另外,需要优化timeoutInMilliseconds值。

  1.  
    hystrix:
  2.  
    command:
  3.  
    default:
  4.  
    execution:
  5.  
    isolation:
  6.  
    thread:
  7.  
    timeoutInMilliseconds: 300000

 

ribbon

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

  1.  
    ribbon:
  2.  
    MaxConnectionsPerHost: 500
  3.  
    MaxTotalConnections: 2000
  4.  
    ConnectTimeout: 60000
  5.  
    ReadTimeout: 60000
  6.  
    # MaxAutoRetries: 0
  7.  
    # MaxAutoRetriesNextServer: 1

 

feign

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

  1.  
    feign:
  2.  
    hystrix:
  3.  
    enabled: true
  4.  
    client:
  5.  
    config:
  6.  
    default:
  7.  
    connectTimeout: 60000
  8.  
    readTimeout: 40000
  9.  
    loggerLevel: full

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

  1.  
    feign:
  2.  
    hystrix:
  3.  
    enabled: true
  4.  
    client:
  5.  
    config:
  6.  
    xmall-auth:
  7.  
    connectTimeout: 60000
  8.  
    readTimeout: 40000
  9.  
    loggerLevel: full

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

  1.  
    feign:
  2.  
    httpclient:
  3.  
    enabled: false
  4.  
    connection-timeout: 60000
  5.  
    max-connections: 2000
  6.  
    max-connections-per-route: 500
  7.  
    # 启用okhttp
  8.  
    okhttp:
  9.  
    enabled: true

 

最终配置

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

  1.  
    server:
  2.  
    port: 5566
  3.  
    tomcat:
  4.  
    max-threads: 2000
  5.  
    min-spare-threads: 400
  6.  
    max-connections: 30000
  7.  
    max-http-header-size: 10MB
  8.  
  9.  
    zuul:
  10.  
    prefix: /gateway
  11.  
    sensitive-headers:
  12.  
    routes:
  13.  
    auth:
  14.  
    path: /auth/**
  15.  
    service-id: xmall-auth
  16.  
    strip-prefix: true
  17.  
    product:
  18.  
    path: /product/**
  19.  
    service-id: xmall-product
  20.  
    strip-prefix: true
  21.  
    host:
  22.  
    max-per-route-connections: 500
  23.  
    max-total-connections: 6000
  24.  
    connect-timeout-millis: 70000
  25.  
    socket-timeout-millis: 60000
  26.  
    # connection-request-timeout-millis: -1
  27.  
    # time-to-live: -1
  28.  
    # time-unit: milliseconds
  29.  
    # hystrix 隔离策略
  30.  
    ribbon-isolation-strategy: thread
  31.  
    # zuul hystrix semaphore配置
  32.  
    # semaphore:
  33.  
    # max-semaphores: 5000
  34.  
  35.  
    # zuul hystrix thread pool配置
  36.  
  37.  
    # thread-pool:
  38.  
    # use-separate-thread-pools: false
  39.  
    # thread-pool-key-prefix: test-
  40.  
  41.  
    hystrix:
  42.  
    command:
  43.  
    default:
  44.  
    execution:
  45.  
    isolation:
  46.  
    strategy: THREAD
  47.  
    thread:
  48.  
    timeoutInMilliseconds: 300000
  49.  
    # hystrix 默认线程池配置
  50.  
    threadpool:
  51.  
    default:
  52.  
    coreSize: 600
  53.  
    maximumSize: 1000
  54.  
    maxQueueSize: -1
  55.  
  56.  
    # hystrix 服务自定义线程池配置
  57.  
    # xmall-auth:
  58.  
    # coreSize: 600
  59.  
    # maximumSize: 1000
  60.  
    # maxQueueSize: -1
  61.  
  62.  
    # ribbon配置
  63.  
    ribbon:
  64.  
    MaxConnectionsPerHost: 500
  65.  
    MaxTotalConnections: 2000
  66.  
    ConnectTimeout: 60000
  67.  
    ReadTimeout: 60000
  68.  
  69.  
    security:
  70.  
    oauth2:
  71.  
    resource:
  72.  
    user-info-uri: http://localhost:7777/oauth/user
  73.  
    prefer-token-info: false
  74.  
  75.  
    feign:
  76.  
    hystrix:
  77.  
    enabled: true
  78.  
    client:
  79.  
    config:
  80.  
    default:
  81.  
    connectTimeout: 60000
  82.  
    readTimeout: 40000
  83.  
    loggerLevel: full
  84.  
    httpclient:
  85.  
    enabled: false
  86.  
    connection-timeout: 60000
  87.  
    max-connections: 2000
  88.  
    max-connections-per-route: 500
  89.  
    # 启用okhttp
  90.  
    okhttp:
  91.  
    enabled: true
  92.  
  93.  
    management:
  94.  
    endpoints:
  95.  
    web:
  96.  
    exposure:
  97.  
    include: '*'
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics