微服务必不可少的熔断器组件Hystrix

hystrix是用来做熔断降级的,直白说就是你出了问题(超时、失败、宕机等)不能影
防止服务雪崩的解决方案,一般有降级、熔断、缓存、合并和隔离,这里主要介绍降级熔断

响我。

一个简单案例(客户端玩法)—基于openfeign

添加依赖
<!--openfeign中已经依赖了hystrix-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
配置生效
feign:
	hystrix:
		enabled: true
配置包扫描

在启动类上添加@EnableFeignClients启动Feign扫描
编写接口代理类:

@FeignClient(value = "demo-user",fallback = UserServiceApiFallback.class)
public interface UserServiceApiProxy {
    @GetMapping("user/info")
    String info(String uid);

    @GetMapping("user/list")
    List<String> list();
}

编写fallback类:

@Component
public class UserServiceApiFallback implements UserServiceApiProxy {
    @Override
    public String info(String uid) {
        return "超时了老弟";
    }

    @Override
    public List<String> list() {
        return Arrays.asList("超时了老兄!");
    }
}

demo-user.list响应时间超过了客户端feign允许的时长后,直接返回fallback中的值,不再等待demo-user.list的实际响应。

另一个简单的案例(客户端玩法)—基于hystrix命令模式

添加依赖
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
配置包扫描

在启动类上添加@EnableFeignClients启动Feign
在启动类上添加@EnableHystrix,启动Hystrix
编写接口代理类:

//这里不需要fallback属性,下面在调用该接口的地方采用HystrixCommand方式
@FeignClient(value = "demo-user")
public interface UserServiceApiProxy {
    @GetMapping("user/info")
    String info(String uid);

    @GetMapping("user/list")
    List<String> list();
}

编写业务逻辑类:


@RestController
@RequestMapping("order")
public class OrderServiceApi {

	//这是要通过openfeign调用的其他服务代理接口(就是上面那个接口)
    @Autowired
    UserServiceApiProxy userServiceApiProxy;

    @GetMapping("info")
    public String info(String oid){
        return  "order info by oid";
    }

    @GetMapping("list")
    @HystrixCommand(fallbackMethod = "err") //当list中调用服务超时时,会立即执行err函数(代码中的下一个函数)
    public List list(){

        List list= this.userServiceApiProxy.list();
        return Arrays.asList("手机","电脑","书籍","汽车",list);
    }

	//当降级后被调用的err函数
    public List err(){
        return Arrays.asList("超时了大哥");
    }
}

上述两种写法其实效果一样,第二种把降级代码和业务代码混在一起是不好的实践,但是第二种可以更精细化配置,在HystrixCommand中可以使用HystrixProperties配置什么情况下允许执行降级。

服务熔断配置案例

 //这个服务熔断的意思是10s内请求10次失败率达到60%就熔断
 @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            // 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            // 请求次数
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), 
            // 失败率达到多少后跳闸
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值