【SpringCloud学习笔记】Hystrix服务降级,熔断

搭建项目

在这里插入图片描述
hystrix既可以在服务端使用,也可以在客户端使用

服务端,服务降级

  • 在 pom.xml 文件中引入hystrix依赖
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 在应用的启动类上使用@EnableCircuitBreaker开启hystrix的功能
@SpringBootApplication
@EnableCircuitBreaker
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}
  • application.yaml配置文件
server:
  port: 9001
spring:
  application:
    name: hystrix-service-provider
eureka:
  instance:
    hostname: localhost
    #开启ip注册
    prefer-ip-address: true
  client:
    #默认值是true,是否将自己注册到注册中心
    register-with-eureka: true
    #默认值是true,是否从注册中心拉取服务
    fetch-registry: true
    #注册中信对外暴露的注册中心地址
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • 使用注解 @HystrixCommand 标记调用失败时需要熔断的方法,fallbackMethod 属性指定降级方法,注意:指定的降级方法需要和降级方法相同入参
@RestController
public class HystrixServiceProviderController {

    @HystrixCommand(fallbackMethod = "fallbackHelloHystrix")
    public String helloHystrix() {
        //int i = 1/0;
        return "hello hystrix";
    }

	//兜底方法
    public String fallbackHelloHystrix() {
        return "---服务端---,当前服务不可用!!!";
    }
}

上面的代码,后面随着业务方法增多,每个业务方法都要有一个兜底方法,这样兜底的方法是跟业务代码柔和在一起,导致代码的膨胀。Hystrix还提供了一个全局配置

  • 第一步,类上面添加 @DefaultProperties(defaultFallback = “fallbackHelloHystrix2”)
  • 第二步,方法上面添加@HystrixCommand
  • 第三步,定义通用异常处理方法
@RestController
@DefaultProperties(defaultFallback = "fallbackHelloHystrix2")
public class HystrixServiceProviderController {
    
    @HystrixCommand
    public String helloHystrix() {
        //int i = 1/0;
        return "hello hystrix";
    }

    public String fallbackHelloHystrix() {
        return "服务端,当前服务不可用";
    }

    public String fallbackHelloHystrix2() {
        return "服务端-当前服务不可用-全局异常处理兜底方法";
    }
}

服务端,服务熔断

  • 服务熔断就是,当在单位时间内触发了超过N次服务降级,并且服务降级占比超过阈值之后,就会触发服务熔断。当服务过来的时候,直接拒绝尝试服务调用,直接让服务降级处理。之后再慢慢尝试恢复。就相当于家里面的电路熔断器,当电流超负荷时,熔断器就会起作用烧断保险丝,将电停掉,导致全家断电
@RestController
public class HystrixServiceProviderController {
    @HystrixCommand(fallbackMethod = "fallbackHelloHystrix", commandProperties = {
            //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            //请求次数,默认20次
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
            //时间窗口期,默认5s
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            //失败率达到多少后跳闸,默认50%
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),})
    public String helloHystrix() {
        return "hello hystrix";
    }

    public String fallbackHelloHystrix() {
        return "---服务端---,当前服务不可用!!!";
    }
}

如何和Feign一起使用

  • 上面的例子中,都是在服务端做的,消费者是通过feign远程调用,那如何实现,首先需要在配置文件中开启,hystrix-service-consumer模块的配置文件
feign:
  hystrix:
  	#在Feign中开启Hystrix
    enabled: true 
  • Feign的远程调用接口,就是调用hystrix-service-provider服务
@FeignClient(name = "hystrix-service-provider", fallback = FallBackService.class)
public interface HystrixFeignClient {

    @GetMapping(value = "/provider/helloHystrix")
    String helloHystrix();
}
  • 针对Feign接口,需要写一个实现类,上面feignClient注解中的fallback对应的就是该实现类
@Component
public class FallBackService implements HystrixFeignClient {
    @Override
    public String helloHystrix() {
        return "当前服务不可用,请稍后重试";
    }
}
  • 业务代码
@RestController
public class HystrixServiceConsumerController {

    @Autowired
    private HystrixFeignClient hystrixFeignClient;

    @GetMapping(value = "/consumer/helloHystrix")
    public String helloHystrix(){
        return hystrixFeignClient.helloHystrix();
    }
}

当业务代码通过openFeign调用服务时,服务出现超时,宕机等情况,就会执行fallback 配置的类中对应的方法返回

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值