Ribbon配置
Spring Cloud Feign
的客户端负载均衡是通过Spring Cloud Ribbon
实现的, 所以可以直接通过Ribbon
客户端的方式来自定义各个服务客户端调用的参数.
- 全局配置
全局配置使用ribbon.<key>=<value>
的方式设置ribbon
的各项默认参数.
# 默认客户端调用超时时间
ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000
- 指定服务配置
指定服务配置使用<client>.ribbon.key=value
格式设置.
HELLO-SERVICE.ribbon.ConnectTimeout=500
HELLO-SERVICE.ribbon.ReadTimeout=5000
HELLO-SERVICE.ribbon.OkToRetryOnAllOperations=true
...
- 重试机制
HELLO-SERVICE.ribbon.MaxAutoRetriesNextServer=2
HELLO-SERVICE.ribbon.MaxAutoRetries=1
Hystrix配置
- 全局配置
对于Hystrix
的全局配置桶Spring Cloud Ribbon
的全局配置一样, 直接使用默认配置前缀, 比如设置全局超时时间:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
注: 需要确认feign.hystrix.enabled
参数没有被设置为false
- 禁用
Hystrix
- 全局禁用
feign.hystrix.enabled=false
- 针对服务客户端禁用需要通过
@Scope("prototype")
注解指定客户端配置Feign.Builder
实例
@Configuration
public class DisableHystrixConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
然后在HelloService
的注解@FeignClient
中通过configuration
参数指定
@FeignClient(value = "HELLO-SERVICE", configuration = DisableHystrixConfiguration.class)
public interface HelloService {
...
}
- 服务降级
无法使用Spring Cloud Hystrix
提供的方式, 而是使用Spring Cloud Feign
另外一种简单的定义方式
需要为HelloService
接口实现一个服务降级类HelloServiceFallback
, 其中每个重写方法的实现逻辑都可以用来定义相应的服务降级逻辑
然后在@FeignClient
注解中指定fallback
属性指定对于的服务降级实现类
本文由 anybbo 创作,采用 知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Dec 17,2020