Spring Cloud的一些理解(四)---熔断器Hystix

Hystix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败。 就是一个降级处理,在调用其他服务失败时,添加内部处理逻辑。

使用时在Spring Boot启动类上添加注解@EnableHystrix,需要添加依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix

在需要处理熔断时,声明一个失败处理函数:

@HystrixCommand(fallbackMethod = "methodFallback")
public MyObj method(){
    MyObj myObj;
    /**
     * 业务处理
     */
    return myObj;
}

public MyObj methodFallback(){
    MyObj myObj;
    /**
     * 异常处理
     */
    return myObj;
}

配置说明(.yml方式)

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            # 设置hystrix的超时时间为6000ms,默认1000ms
            timeoutInMilliseconds: 6000

 

你可能感兴趣的:(Spring,Cloud)