Spring-cloud之熔断器 Hystrix 防止服务雪崩

在分布式系统中,一种不可避免的情况就是某些服务会出现故障,导致依赖他们的其他服务出现远程调度的线程问题(雪崩效应)。而Hystrix提供的熔断器,通过隔离服务的访问点,能阻止这种分布式系统中出现的联动故障,并提供故障的解决方案,从而提高了整个分布式系统的弹性。

设计原则

1、防止单个服务的故障耗尽整个服务的servlet容器的线程资源

2、快速失败机制,如果某个服务出现故障则调用该服务的请求快速失败,而不是线程等待

3、提供回退方案

4、使用熔断机制,防止故障扩散到其他服务

5、提供熔断器的监控组件Hystrix Dashboard,实时监控熔断器状态。提供Turbine聚合多喝Dashboard

工作机制

1、当某个API接口失败的次数在一定时间内小鱼设定的阀值时,熔断器处于关闭状态,该API正常提供服务。

2、当失败次数大于设定的阀值的时候,Hystrix判定改API接口出现故障,打开熔断器,这时候该请求API接口会执行快速失败的逻辑(fallback回退的逻辑)而不执行业务逻辑,请求的线程不会处于阻塞状态。

3、处于打开状态的熔断器,一段时间后会处于半打开状态,并将一定数量的请求执行业务逻辑,剩余的请求会执行快速失败。若执行的业务逻辑请求失败,则熔断器继续打开,若成功则熔断器关闭。

 

熔断器可以在RestTemplate+Ribbon 或 feign 服务中使用

 

Ribbon中使用Hystrix(熔断器)

  1.在Ribbon的pom.xml中添加依赖

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

  2.在application中增加注解启动熔断器Hystrix
  

package com.funtl.hello.spring.cloud.web.admin.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

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

3.在 Ribbon 调用方法上增加 @HystrixCommand 注解并指定 fallbackMethod 熔断方法 

package com.funtl.hello.spring.cloud.web.admin.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class AdminService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String sayHi(String message) {
        return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
    }

    public String hiError(String message) {
        return "Hi,your message is :\"" + message + "\" but request error.";
    }
}

 如果发生请求超时就是所谓的爱的魔力转圈圈  直接调用熔断方法

 

Feign使用熔断Hystrix

  fegin自带Hystrix但是默认关闭 需要手动开启
在application.yml 中加入

 

feign:
  hystrix:
    enabled: true

Fegin的熔断是通过调用一个自定义熔断类来实现对应的service服务接口类
所以 
   创建一个熔断类 并且实现 相应的需要实现熔断的服务

 

  别忘记要在所被实现的服务接口类的@FeignClient的属性增加
  fallback=所被实现的服务接口类.class

package com.einblatt.hello.spring.cloud.web.admin.feign.service;

import com.einblatt.hello.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

    @FeignClient(value = "hello-spring-cloud-service-admin",fallback = AdminServiceHystrix.class)
public interface AdminService {

    @RequestMapping(value = "hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam(value = "message") String message);

}


  创建熔断类

package com.einblatt.hello.spring.cloud.web.admin.feign.service.hystrix;

import com.einblatt.hello.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;

@Component
public class AdminServiceHystrix implements AdminService {
    @Override
    public String sayHi(String message) {
        return String.format("this is message %s but error",message);
    }
}


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值