• SpringCloud优化记录


    如何设置Hystrix线程池大小

    设置服务中每个 hystrix 线程池的大小,以及 如何设置超时时间呢?

    假设你的服务A,每秒钟会接收30个请求,同时会向服务B发起30个请求,然后每个请求的响应时长经验值大概在200ms

    那么你的hystrix线程池需要多少个线程呢?

    计算公式: 每秒 高峰访问次数 * 访问延时 + bufferThread

    timeout 设为 200ms, 每秒100个请求---->计算结果:100*0.2+10=30 个线程,两台虚拟机 可以解决

    如果每秒 访问次数30000怎么办?

    30000 * 0.2 + 100 =6100 个线程
    6100 / 20 =305 台虚拟机 也可以搞定
    虚拟机: 4 个cpu core ,4G内存
    物理机 : 十几个cpu core ,几十个G的内存,5~8 个虚拟机,这里 1台物理机=6台虚拟机
    305 台虚拟机 = 51 台物理机~搞定

    对于线程池大小,一般控制在 10个 左右,20个 以内,最少5个

    如何设置请求超时时间

    请求的超时时间设置为多少?答案是 300ms

    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 200  # 设置熔断超时时间  default 1000
            timeout:
              enabled: true # 打开超时熔断功能 default true
    
    ribbon:
      ConnectTimeout: 1000 # 设置连接超时时间 default 2000
      ReadTimeout: 1000    # 设置读取超时时间  default 5000
      OkToRetryOnAllOperations: true # 对所有操作请求都进行重试  default false
      MaxAutoRetriesNextServer: 1    # 切换实例的重试次数  default 1
      MaxAutoRetries: 1     # 对当前实例的重试次数 default 0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    因为一个接口,理论的最佳响应速度应该在 200ms 以内,或者慢点的接口就几百毫秒

    yaml配置

    
    <dependency>
        <groupId>io.github.openfeigngroupId>
        <artifactId>feign-httpclientartifactId>
    dependency>
    
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
        <version>2.2.7.RELEASEversion>
    dependency>
    
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        <version>2.2.7.RELEASEversion>
    dependency>
    
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-openfeignartifactId>
        <version>2.2.7.RELEASEversion>
    dependency>
    
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.25version>
    dependency>
    
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.5.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    
    server:
      servlet:
        context-path: /content #前缀
      port: 8080
    spring:
      application:
        name: spring-test # 应用名
      cloud:
        nacos:
          # nacos 地址
          server-addr: 127.0.0.1:8848
        #      discovery:
        #        cluster-name: HZ #集群名称
      #    gateway:
      #      routes: # 网关路由配置
      #        - id: service-hosp             # 路由id,自定义,只要 唯一即可
      #          # uri: http://127.0.0.1:8081  # 路由的目标地址 http就是固定地址
      #          uri: lb://service-hosp
      #          # /*代表一级,/** 代表多级
      #          predicates:
      #            # /a/hosp/c/d 满足 /a/hosp/ 也满足
      #            - Path=/*/hosp/** # 将含有hosp的uri 转发到service-hosp这个服务,lb 代表负载均衡
      #          filters:
      #            # 服务的熔断和降级配置(内置的过滤器)
      #            - name: Hystrix
      #              args:
      #                name: default
      #                fallbackUri: 'forward:/defaultFallback' # 服务降级转发到 /defaultFallback 这个接口
      #            # 网关的限流过滤器配置(内置的过滤器)
      #            - name: RequestRateLimiter
      #              args:
      #                key-resolver: '#{@pathKeyResolver}'   # 使用 SpEL 表达式按名称引用bean,一共有三种,名称请查看配置对象
      #                redis-rate-limiter.replenishRate: 1   # 令牌桶每秒填充速率
      #                redis-rate-limiter.burstCapacity: 2   # 令牌桶总的容量大小,写2方便测试
      # 配置数据源信息
      datasource:
        druid:
          # 高并发场景下,万一遇到网络问题,可能会导致你跟数据库的Socket连接异常无法通信
          connect-timeout: 1200 # 建立TCP连接的超时时间
          socket-timeout: 3000  # 发送请求后 等待响应的超时时间
          max-wait: 800 # 0.8s 以上, 大量线程获取不到连接,0.8s 左右快速就失败
          # 连接数 <=  (核心数 * 2) + 有效磁盘数
          maxActive: 20 # 确实有高并发场景,可以适当增加到3~5倍,不超过 100
          initial-size: 5 # 初始化大小
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.111.101:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: 123456
        # jackson的全局时间格式
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: Asia/Shanghai
      redis:
        host: 192.168.111.101
        port: 6379
        password:
        database: 0
        timeout: 3000
        lettuce:
          pool:
            max-active: 10 # 最大连接
            max-idle: 5 # 最大空闲连接
            min-idle: 3 # 最小空闲连接
            max-wait: 100 # 连接等待时间
    #    cluster:
    #      max-redirects: 3
    #      nodes:
    
    
    mybatis-plus:
      configuration:
        # 数据库字段:user_id===>实体属性:userId
        map-underscore-to-camel-case: true
        # 打印sql日志
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      global-config:
        db-config:
          id-type: ASSIGN_ID # 采用 分布式ID策略
          # 逻辑删除字段
          logic-delete-field: isDeleted
          # 正常值
          logic-not-delete-value: 0
          # 删除值
          logic-delete-value: 1
      # mapper的地址,类路径
      mapper-locations: classpath*:com/xconline/mapper/xml/*.xml
    
    feign:
      client:
        config:
          default: # default全局的配置
          	connectTimeout: 10000 # 连接超时设置
          	readTimeout: 60000 # 执行超时配置
            loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
      httpclient:
        enabled: true # 开启 feign对HttpClient的支持
        max-connections: 200 # 最大的连接数
        max-connections-per-route: 50 # 每个路径的最大连接数
      hystrix:
        enabled: true  # 开启熔断机制
      compression:
        request:
          enabled: true # 配置请求GZIP压缩
          mime-types: [ "text/xml","application/xml","application/json" ] # 配置压缩支持的MIME TYPE
          min-request-size: 2048 # 配置压缩数据大小的下限
        response:
          enabled: true # 配置 响应 GZIP压缩
    
    hystrix:
      command:
        default:
          execution:
            isolation:
              strategy: THREAD # THREAD|SEMAPHORE
              thread:
                timeoutInMilliseconds: 300  # 设置熔断超时时间  default 1000
            timeout:
              enabled: true # 打开超时熔断功能 default true
    ribbon:
      ConnectTimeout: 10000 # 设置连接超时时间 default 2000
      ReadTimeout: 2000    # 设置读取超时时间  default 5000
      OkToRetryOnAllOperations: true # 对所有操作请求都进行重试  default false
      MaxAutoRetriesNextServer: 2    # 切换实例的重试次数  default 1
      MaxAutoRetries: 2     # 对当前实例的重试次数 default 0
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
  • 相关阅读:
    MySql隔离级别知识点整理
    unordered_map,unordered_set模拟实现
    强大的多数据库客户端工具:DataGrip【送源码】
    第二部分—C语言提高篇_11. 预处理
    Python类型转换
    HTML+CSS大作业 (水果之家10个网页)
    杜教筛练习题
    【Linux】管道命令split、awk、sed【二】
    vue2安装vue-router,less ,vuex的版本
    拉电流、灌电流和漏电流
  • 原文地址:https://blog.csdn.net/qq_30659573/article/details/127806049