springCloud微服务组件:熔断器(Hystrix)

本文介绍了SpringCloud微服务组件Hystrix的使用,包括服务降级、熔断机制以及如何通过Turbine进行监控。在降级策略中,讲述了提供方和消费方如何实现降级方法。熔断机制则描述了当请求连续失败达到阈值时,如何打开熔断器以保护系统。此外,还详细解释了如何配置和使用Turbine进行微服务的聚合监控。
摘要由CSDN通过智能技术生成

springCloud微服务组件:熔断器(Hystrix)

降级

消费方调用提供方接口,提供方如果出异常了,需要向消费方返回一个降级方法

1.提供方降级

  • 导入Hystrix对应的坐标,才能向消费方提供降级的方法

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    
  • 编写服务接口对应的降级方案,要用@HystrixCommand注解声明这是一个降级方法

    package com.gwx.controller;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/hello")
    public class ProviderController {
         
        @RequestMapping("/hello")
    //    指定这个接口出现异常调用的降级方法
        @HystrixCommand(fallbackMethod = "hello_fallback",commandProperties = {
         
    //            表示这个接口超时多少秒出现异常才会去调用降级的方法
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
        })
        public String hello(){
         
            return "hello nacos";
        }
    //    定义降级的方法
        public String hello_fallback(){
         
            return "降级了";
        }
    }
    
  • 在启动类开启Hystrix的服务降级的功能

@SpringBootApplication
@EnableCircuitBreaker
public class ProviderApp {
   
    public 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值