feign ribbon 配置

feign 配置

官方文档配置

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

feign:
client:
config:
feignName:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: full
errorDecoder: com.example.SimpleErrorDecoder
retryer: com.example.SimpleRetryer
requestInterceptors:
- com.example.FooRequestInterceptor
- com.example.BarRequestInterceptor
decode404: false
encoder: com.example.SimpleEncoder
decoder: com.example.SimpleDecoder
contract: com.example.SimpleContract
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
# To disable Hystrix in Feign
hystrix:
enabled: false


# To set thread isolation to SEMAPHORE
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
1
2
3
4
5
6
7
8
9
10
11
12

# 配置请求GZIP压缩
feign.compression.request.enabled=true
# 配置响应GZIP压缩
feign.compression.response.enabled=true
# 配置压缩支持的MIME TYPE
feign.compression.request.mime-types=text/xml,application/xml,application/json
# 配置压缩数据大小的下限
feign.compression.request.min-request-size=2048

#熔断
feign.hystrix.enabled=false

ribbon 配置

官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

users:
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
stores:
ribbon:
listOfServers: example.com,google.com

ribbon:
eureka:
enabled: false
eager-load:
enabled: true
clients: client1, client2, client3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


# 设置连接超时时间
ribbon.ConnectTimeout=3000
# 设置读取超时时间
ribbon.ReadTimeout=2000
# 对所有操作请求都进行重试
ribbon.OkToRetryOnAllOperations=true
# 切换实例的重试次数
ribbon.MaxAutoRetriesNextServer=2
# 对当前实例的重试次数
ribbon.MaxAutoRetries=1
#切换实例的重试次数
ribbon.MaxAutoRetriesNextServer=1


#暂不开启重试,以防请求重复
spring.cloud.loadbalancer.retry.enabled=false
  • 这种配置是一种全局配置,就是是对所有的请求生效的,如果我想针对不同的服务配置不同的连接超时和读取超时,那么我们可以在属性的前面加上服务的名字,如下:
1
2
3
4
5
6
7
8
9
10
11
12


# 设置针对hello-service服务的连接超时时间
hello-service.ribbon.ConnectTimeout=600
# 设置针对hello-service服务的读取超时时间
hello-service.ribbon.ReadTimeout=6000
# 设置针对hello-service服务所有操作请求都进行重试
hello-service.ribbon.OkToRetryOnAllOperations=true
# 设置针对hello-service服务切换实例的重试次数
hello-service.ribbon.MaxAutoRetriesNextServer=2
# 设置针对hello-service服务的当前实例的重试次数
hello-service.ribbon.MaxAutoRetries=1