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'"