Spring Cloud Hystrix降级处理超时时间设置execution.isolation.thread.timeoutInMilliseconds

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yaomingyang/article/details/86529826

默认情况下调用接口能够触发Hystrix服务降级处理的超时时间是1000ms,我们可以通过HystrixCommandProperties类的源码查找到,如下图:
在这里插入图片描述
点击属性default_executionTimeoutInMilliseconds进去就是我们需要的配置execution.isolation.thread.timeoutInMilliseconds
在这里插入图片描述
属性execution.isolation.thread.timeoutInMilliseconds可以通过代码的方式配置生效:

package com.eureka.client.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;


@Service
public class TestService {

	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "helloFallback", commandProperties= {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1200")})
	public String helloService() {
		String result = restTemplate.getForObject("http://EUREKA-SERVER/add", String.class);
		System.out.println(result);
		return result;
	}
	
	public String helloFallback() {
		return "timeout";
	}
}

当然网上我看也有很多说是可以通过application.properties配置文件配置的,但是我试验了不可以的,如果哪位知道原因请多多交流。

猜你喜欢

转载自blog.csdn.net/yaomingyang/article/details/86529826