一. Hystrix是什么?
分布式系统面临的一种重要问题:服务雪崩.复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某个时刻将不可避免的失败。
Hystrix断路器是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时,异常等,Hystrix能保证在一个依赖服务出现问题时,不会导致整体服务失败,避免级联故障,提高分布式系统的弹性.
"断路器"本身是一种开关装置,当某个服务单元发生故障后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的,可处理的备选响应(FallBack),而不是长时间的等待或者抛出一个调用房无法处理的异常,这样就保证了服务调用方的线程不会长时间不必要的占用,从而避免了故障在分布式系统中的蔓延,甚至雪崩.
二. Hystrix能做什么?
简单来说:隔离,熔断,降级
(1) 服务熔断
使用示例:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
@EnableCircuitBreaker//对hystrixR熔断机制的支持
public class AppApplication_hystrix_8001 {
public static void main(String[] args)
{
SpringApplication.run(AppApplication_hystrix_8001.class, args);
}
}
//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
@RequestMapping(value = "/product/get/{id}", method = RequestMethod.GET)
public Product get(@PathVariable("id") Long id) {
Product product = productService.get(id);
if (null == product) {
throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
}
return product;
}
public Product processHystrix_Get(@PathVariable("id") Long id) {
return new Product().setId(id).setDescription("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand");
}
(2)服务降级
所谓降级,一般是从整体负荷考虑,就是当某个服务熔断之后, 服务器将不再被调用.此时客户端可以自己准备一个本地的fallback回调,返回一个缺省值.这样做,虽然服务水平下降,但好歹可用,比直接挂掉要强.
整体资源快不够了,忍痛将某些服务先关掉,待度过难关,再开启回来.服务降级处理是在客户端实现完成的,与服务端没有关系.当服务端已经down了, 通过服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器.
服务降级处理是在客户端实现完成的,与服务端没有关系。
服务消费方需要开启支持Hystrix服务降级:
feign:
hystrix:
enabled: true
服务降级处理类
@Component // 不要忘记添加
public class ProductClientServiceFallbackFactory implements FallbackFactory<ProductClientService> {
@Override
public ProductClientService create(Throwable throwable) {
return new ProductClientService() {
@Override
public Product get(Long id) {
return new Product().setId(id).setDescription("该ID:" + id + "没有没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭");
}
@Override
public Map<String, Object> list() {
return null;
}
@Override
public int add(Product product) {
return 0;
}
};
}
}
Feign声明式接口添加降级处理类
@FeignClient(value = "microservice-product",fallbackFactory=ProductClientServiceFallbackFactory.class)
public interface ProductClientService {
/**
* 使用Feign声明式服务调用,看的是接口方法头上标注的@RequestMapping,
* 调用声明式接口中的方法实际调用的是@FeignClient(value = "microservice-product")
* 标识的服务中的映射路径为@RequestMapping所声明的路径,
* 注意声明式接口中的方法签名需要与映射路径对应的方法签名一致
* @param id
* @return
*/
@RequestMapping(value = "/product/get/{id}", method = RequestMethod.GET)
Product get(Long id);
@RequestMapping(value = "/product/list", method = RequestMethod.GET)
Map<String,Object> list();
@RequestMapping(value = "/product/add", method = RequestMethod.POST)
int add(Product product);
}
三. Hystrix Dashboard服务监控
<!-- hystrix和 hystrix-dashboard相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
@SpringBootApplication
@EnableHystrixDashboard
public class Application_Dashboard
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
所有微服务提供方都需要监控依赖配置:
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
访问Hystrix Dashboard服务:
启动服务提供方后在上面的监控界面中填入如下信息:
http://localhost:8001/hystrix.stream可以看到一直在监控输出。
如何看监控界面信息?
7色1圈1线
整图说明:
搞懂一个才能看懂复杂的: