SpringCloud之熔断器-Hystrix

Hystrix是一个延迟和容错库,旨在隔离对远程系统,服务和第三方库的访问点,停止级联故障,并在复杂的分布式系统中实现弹性,在这些系统中,故障是不可避免的。
在这里插入图片描述

依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

SpringBootApplication入口中需要添加@EnableCircuitBreaker注解,此时Spring工厂会启动AOP方式对所有的方法上有@HystrixCommand的业务方法添加熔断策略。

@SpringBootApplication
@EnableCircuitBreaker
public class HystrixSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixSpringBootApplication.class,args);
    }
}

在业务方法上添加@HystrixCommand注解实现熔断。

@HystrixCommand
@Override
public User queryUserById(Integer id) {
    System.out.println(Thread.currentThread().getId());
    return new User("未熔断", new Date(), true, 11.11);
}

线程隔离

默认该方法的执行会启动新的线程执行和主程序不在一个线程中,因此如果上下文中存在ThreadLocal变量,在该方法中就失效了。因此一般可以通过设置commandProperties注解属性,设置线程就可以了。

  • 默认情况Service和Controller线程ID
    在这里插入图片描述
@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")
    })
@Override
public User queryUserById(Integer id) {
	System.out.println("Service线程ID:" + Thread.currentThread().getId());
    return new User("未熔断", new Date(), true, 11.11);
}
  • 设置后Service和Controller线程ID
    在这里插入图片描述

execution.isolation.strategy该属性的可选值有两个THREADSEMAPHORE默认值是THREAD。①一般如果一个实例一秒钟有100个并发,此时因为频繁启动线程的开销过大此时一般考虑使用SEMAPHORE,②非网络调用。

Fallback

过在@HystrixCommand中声明fallbackMethod的名称可以实现优雅降级,如下所示:

@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
@Override
public User queryUserById(Integer id) {
    System.out.println(Thread.currentThread().getId());
    int i=10/0;
    return new User("未熔断", new Date(), true, 11.11);
}
public User fallbackMethodQueryUserById(Integer id, Throwable e) {
    System.out.println(e.getMessage());
    return new User("熔断降级后", new Date(), true, 11.11);
}

调用结果:
在这里插入图片描述

注意要求fallbackMethod方法和目标方法必须在同一个类中,具有相同的参数(异常参数可选)

Error Propagation

根据此描述,@ HystrixCommand能够指定应忽略的异常类型。如下所述ArithmeticException: / by zero将不会触发fallbackMethod方法。

//	@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
//	@HystrixCommand(commandProperties = {
//			@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")
//	})
	@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",ignoreExceptions = {ArithmeticException.class})
	@Override
	public User getUser(Integer id) {
		System.out.println("Service线程ID:" + Thread.currentThread().getId());
		int i = 10 / 0;
		return new User("未熔断", new Date(), true, 11.11);
	}

	public User fallbackMethodQueryUserById(Integer id, Throwable e) {
		System.out.println(e.getMessage());
		return new User("熔断降级后", new Date(), true, 11.11);
	}

请求超时熔断

用户可以通过设置execution.isolation.thread.timeoutInMilliseconds属性设置一个方法最大请求延迟,系统会抛出HystrixTimeoutException

@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",commandProperties = {
			@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="100")
	})
	@Override
	public User getUser(Integer id) {
		System.out.println("Service线程ID:" + Thread.currentThread().getId());
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//		int i = 10 / 0;
		return new User("未熔断", new Date(), true, 11.11);
	}

	public User fallbackMethodQueryUserById(Integer id, Throwable e) {
		System.out.println(e.getMessage());
		return new User("超时熔断", new Date(), true, 11.11);
	}

执行结果:
在这里插入图片描述

https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
https://github.com/Netflix/Hystrix/wiki/Configuration

Hystrix Dashboard

依赖

 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
 </dependency>

SpringBoot入口类添加@EnableHystrixDashboard注解
访问页面http://localhost:端口/hystrix

在这里插入图片描述

发布了19 篇原创文章 · 获赞 8 · 访问量 4541

猜你喜欢

转载自blog.csdn.net/M283592338/article/details/104297756