Hystrix 超时配置重写

本文详细介绍了如何在Spring Cloud中配置Feign客户端与Hystrix断路器的集成,包括设置超时时间、重试策略及断路器参数。通过两个不同配置类,分别展示了在Sleuth开启和关闭情况下的配置差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Configuration
@ConditionalOnProperty(value = "spring.sleuth.feign.enabled", havingValue = "false")
@Slf4j
public class CommonHystrixConfiguration {

	/**
	 * hystrix 超时时间
	 */
	static int hystrixTimeOut = 10000;
	/**
	 * 请求超时时间
	 */
	static int requestTimeOut = 3000;

	@Bean
	public Request.Options options() {
		return new Request.Options(requestTimeOut, requestTimeOut);
	}

	@Bean
	Retryer feignRetryer() {
		return new Retryer.Default(100, SECONDS.toMillis(1), 1);
	}

	@Bean
	@Primary
	public Feign.Builder feignHystrixBuilderExt(BeanFactory beanFactory) {
	
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			public HystrixCommand.Setter create(Target<?> target, Method method) {
				String groupKey = target.name();
				String commandKey = method.getName();
				return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
						.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(
								HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(hystrixTimeOut)
										.withCircuitBreakerSleepWindowInMilliseconds(hystrixTimeOut));
			}
		});
	}
@Configuration
@ConditionalOnProperty(value = "spring.sleuth.feign.enabled", havingValue = "true")
@Slf4j
public class CommonTraceHystrixConfiguration {

	/**
	 * hystrix 超时时间
	 */
	static int hystrixTimeOut = 10000;
	/**
	 * 请求超时时间
	 */
	static int requestTimeOut = 3000;

	@Bean
	public Request.Options options() {
		return new Request.Options(requestTimeOut, requestTimeOut);
	}

	@Bean
	Retryer feignRetryer() {
		return new Retryer.Default(100, SECONDS.toMillis(1), 1);
	}

	@Bean
	@Primary
	//@Scope("prototype")
	public Feign.Builder feignTraceHystrixBuilderExt(BeanFactory beanFactory) {
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			public HystrixCommand.Setter create(Target<?> target, Method method) {
				String groupKey = target.name();
				String commandKey = method.getName();
				return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
						.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(
								HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(hystrixTimeOut)
										.withCircuitBreakerSleepWindowInMilliseconds(hystrixTimeOut));
			}
		}).client(new TraceFeignClient(beanFactory));
	}

}
@Configurable /*单个类得*/
@Slf4j
public class ImServiceHystrixConfiguration {

	@Bean
	@ConditionalOnProperty(value = "spring.sleuth.feign.enabled", havingValue = "false")
	public Feign.Builder feignHystrixBuilder() {
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			public HystrixCommand.Setter create(Target<?> target, Method method) {
				String groupKey = target.name();
				String commandKey = method.getName();
				int time = 5000;
				if (commandKey.startsWith("sys")) {
					time = 1000 * 60 * 10;
				}
				return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))// 控制
																											// RemoteProductService
																											// 下,所有方法的Hystrix
																											// Configuration
						.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(
								HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(time) // 超时配置
				);
			}
		});
	}

	@Bean
	@ConditionalOnProperty(value = "spring.sleuth.feign.enabled", havingValue = "true")
	public Feign.Builder feignTraceHystrixBuilder(BeanFactory beanFactory) {
		log.info("load com.dominos.cloud.order.config.ImServiceHystrixConfiguration.feignTraceHystrixBuilder()");
		
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			public HystrixCommand.Setter create(Target<?> target, Method method) {
				String groupKey = target.name();
				String commandKey = method.getName();
				int time = 5000;
				if (commandKey.startsWith("sys")) {
					time = 1000 * 60 * 10;
				}
				return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))// 控制
																											// RemoteProductService
																											// 下,所有方法的Hystrix
																											// Configuration
						.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults(
								HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(time) // 超时配置
				);
			}
		}).client(new TraceFeignClient(beanFactory));
	}

	@Bean
	public Request.Options options() {
		return new Request.Options(6000 * 100, 6000 * 100);
	}

}

 

转载于:https://my.oschina.net/xiaominmin/blog/3048888

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值