SpringCloud生产环境组件参考配置:具体配置还得具体场景和业务需求
1:Eureka推荐配置
1)Eureka服务端推荐配置
启动类上加上如下注解
/**
* eureka server
*/
@SpringBootApplication
@EnableEurekaServer
bootstrap.yml配置文件如下
server:
port: 8761
spring:
application:
name: sc-eurekaserver
eureka:
client:
registerWithEureka: false
fetchRegistry: false
属性 | 描述 |
eureka.client.serviceUrl.defaultZone | 注册中心位置,高可用时需要配置多个地址,可根据启动参数传入 |
eureka.instance.prefer-ip-address | 推荐大家都可以用IP来注册 |
eureka.server.enable-self-perservation | 默认开启,关闭自我保护,如果规模较大,可以考虑不关闭 |
eureka.server.eviction-interval-timer-in-ms | 自动失效检测时间,默认60时,可以设置短一点 |
2)Eureka客户端配置
启动类上加上如下注解
@SpringBootApplication
@EnableDiscoveryClient
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
instance:
prefer-ip-address: true
其他如心跳间隔时间的设置与服务端差别不大,推荐使用官网的方式,保持30s不变
2.Ribbon推荐配置
ribbon:
ConnectTimeout: 6000 #全局请求连接的超时时间,默认5s
ReadTimeout: 6000 #全局请求的超时时间,默认5S
MaxAutoRetries: 0 #对当前实例的重试次数
MaxAutoRetriesNextServer: 0 #切换下一个实例的重试次数
okToRetryOnAllOperations:false #对所有的操作都进行重试
3.Hystrix断路器推荐配置
hystrix:
command:
default:
execution:
timeout:
isolation:
thread:
timeoutInMilliseconds: 15000
全局请求连接的超时时间为1S,通常会调整这个值的大小,推荐设为10S
4.Zuul推荐配置
zuul:
ribbonIsolationStrategy: THREAD
threadPool:
useSeparateThreadPools: true
threadPoolKeyPrefix: zuulgateway
max:
host:
max-per-route-connections: 200
max-total-connections: 500
host:
socket-timeout-millis: 5000
connect-timeout-millis: 10000
hystrix:
threadpool:
default:
coreSize: 20
maximumSize: 50
maxQueueSize: -1
allowMaximumSizeToDivergeFromCoreSize: true
command:
default:
execution:
timeout:
enabled: false
isolation:
thread:
interruptOnTimeout: false
timeoutInMilliseconds: 15000
zuul全部配置一般如下配置
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
endpoints:
web:
exposure:
include: hystrix.stream
feign:
hystrix:
enabled: true
ribbon:
ConnectTimeout: 6000
ReadTimeout: 6000
MaxAutoRetries: 0 #对第一次请求的服务的重试次数
MaxAutoRetriesNextServer: 0 #要重试的下一个服务的最大数量(不包括第一个服务)
OkToRetryOnAllOperations: false
zuul:
ribbonIsolationStrategy: THREAD
threadPool:
useSeparateThreadPools: true
threadPoolKeyPrefix: zuulgateway
max:
host:
max-per-route-connections: 200
max-total-connections: 500
host:
socket-timeout-millis: 5000
connect-timeout-millis: 10000
hystrix:
threadpool:
default:
coreSize: 20
maximumSize: 50
maxQueueSize: -1
allowMaximumSizeToDivergeFromCoreSize: true
command:
default:
execution:
timeout:
enabled: false
isolation:
thread:
interruptOnTimeout: false
timeoutInMilliseconds: 15000
Zuul配置描述
属性 | 描述 |
hystrix.command.default.execution.ioslation .thread.timeoutInMillseconds | 全局请求连接的超时时间,默认为1S,通常会调整这个值的大小,推荐改为10S |
hystrix.threadpool.default.coreSize | 全局默认的核心线程大小,默认为10,一般会调大一点 |
hystrix.threadpool.default.maximumSize | 全局默认最大线程池大小,一般也会调大一点 |
hystrix.threadpool.default. allowMaximumSizeToDivergeFromCoreSize | 该属性配置coreSize和maximumSize生效,默认为false |
zuul.ribbonIsolationStrategy: THREAD | 使用线程池隔离策略,默认Zuul使用信号量 |
zuul.threadPool.threadPoolKeyPrefix:THREAD | 使用线程池隔离时每条线程的前缀 |
zuul.host.maxTotalConnections | 目标主机可用的最大连接数,默认为20 |
zuul.host.maxPreRouteConnections | 每个路由可用的最大连接数,默认为20 |
zuul.host.connect-timeout-mills zuul.host.socket-timeout-mills | 如果你不是以服务名来路由而不是使用URL来配置zuul路由,则使用的是这两个超时配置 |
5.Hystrix推荐配置
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
endpoints:
web:
exposure:
include: hystrix.stream
turbine:
appConfig: sc-user-service,sc-zuul-service,sc-data-service
clusterNameExpression: "'default'"