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

默认情况下调用接口能够触发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配置文件配置的,但是我试验了不可以的,如果哪位知道原因请多多交流。

Spring Cloud Hystrix是一个开源的容错框架,提供了断路器模式的实现,可以防止分布式系统中的级联故障。除此之外,它还包含服务降级、服务熔断、服务限流和监控报警等功能。下面分别用代码展示这些功能的实现。 1. 断路器模式 ```java @HystrixCommand(fallbackMethod = "fallback") public String hello() { // 调用其他服务或者一些耗时操作 } public String fallback() { return "Sorry, the service is unavailable!"; } ``` 在上面的代码中,`@HystrixCommand`注解标记的方法会被Hystrix包装成一个独立的线程池中的可执行命令。当执行命令的线程池达到一定阈值时,Hystrix会自动断开服务调用,避免级联故障。 2. 服务降级 ```java @HystrixCommand(fallbackMethod = "fallback") public String hello() { // 调用其他服务或者一些耗时操作 } public String fallback() { return "Sorry, the service is unavailable!"; } ``` 在上面的代码中,当调用`hello()`方法失败时,Hystrix会自动调用`fallback()`方法,返回一个默认的响应结果。 3. 服务熔断 ```java @HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000") }) public String hello() { // 调用其他服务或者一些耗时操作 } public String fallback() { return "Sorry, the service is unavailable!"; } ``` 在上面的代码中,我们使用`@HystrixCommand`注解的`commandProperties`属性来配置熔断器的阈值和休眠窗口。当调用`hello()`方法的失败率达到一定阈值时,Hystrix会自动触发熔断器,避免级联故障。 4. 服务限流 ```java @HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50") }) public String hello() { // 调用其他服务或者一些耗时操作 } public String fallback() { return "Sorry, the service is unavailable!"; } ``` 在上面的代码中,我们使用`@HystrixCommand`注解的`commandProperties`属性来配置服务限流的参数。当调用`hello()`方法超时或者失败率达到一定阈值时,Hystrix会自动限制服务的请求速率,保护服务资源。 5. 监控报警 ```java @SpringBootApplication @EnableHystrix @EnableHystrixDashboard public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在上面的代码中,我们使用`@EnableHystrix`和`@EnableHystrixDashboard`注解来启用Hystrix的监控和报警功能。通过访问Hystrix Dashboard,可以实时监控服务的健康状态,及时发现问题并进行处理
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值