记一次springcloud并发优化

使用组件 springcloud套件 + mysql

一个普通的rest请求,做个用户基础信息查询,sql已进行优化 const级别,但是通过jemeter进行压测,发现实际并发量只有20-30,经检查后发现是配置未优化问题,解决方式如下:

1. 增加springboot mysql连接池配置,默认使用的为HikariDataSource。配置如下

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: cjs-base
    password: xxx
    url: jdbc:mysql://xxx:3306/xxx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
    hikari:
      minimum-idle: 100
      maximum-pool-size: 500
      idle-timeout: 30000
      max-lifetime: 180000
      # connection-timeout: 5000
      connection-test-query: select 1
      connection-init-sql: select 2
      #多少毫秒检测一次连接泄露.
      leak-detection-threshold: 1000
      test-on-borrow: true
      test-on-connect: true
      test-on-return: true
      test-while-idle: true
      #指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
      time-between-eviction-runs-millis: 10000
      #是否对连接进行包装,防止连接关闭之后被使用.
      use-disposable-connection-facade: true
      validation-interval: 10000
      alidation-query: select 3
      validation-query-timeout: 5000
      #设定连接校验的超时时间,当使用Hikari connection pool时指定
      alidation-timeout: 5000    

其中连接池大小,根据自己项目情况配置。

2. 修改fegin配置,一般默认就好。主要是防止资源占用无法释放,需要将超时时间设置小一些。

feign:
  hystrix:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      feignName:
        connectTimeout: 1000
        readTimeout: 1000
  compression:
    request:
      enabled: true
    response:
      enabled: true

 3.修改hystrix配置,将连接池大小设置为大于需要的并发量

hystrix:
  threadpool:
    default:
      coreSize: 500
      maxQueueSize: 1000
  command:
    default:
      circuitBreaker: 
        requestVolumeThreshold: 50
      execution:
        isolation:
          semaphore:
            maxConcurrentRequests: 1000
          strategy: SEMAPHORE
          thread:
            timeoutInMilliseconds: 1000
  shareSecurityContext: true

  4.修改ribbon配置

#请求处理的超时时间
ribbon:
  ReadTimeout: 1000
  ConnectTimeout: 1000

5. 增加微服务的实例数量,在k8s中将各微服务的数量按需扩容,我这边每个微服务扩容为3个实例,加上以上的配置优化,并发量大约可控制在250左右,已满足当前业务需求。 如还需要进一步的优化,可采用加大服务实例、增加NOSQL的实现方式来实现

猜你喜欢

转载自www.cnblogs.com/mengyue/p/11617784.html