当前位置: 首页 > news >正文

SpringCloud-断路器Hystrix

  一、降级使用

        1、添加依赖

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

         2、启动类添加注解@EnableCircuitBreaker

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {public static void main(String[] args) {SpringApplication.run(PaymentHystrixMain8001.class,args);}
}

        3、超时或代码出错,降级代码fallbackMethod表示降级方法(服务提供方)

@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public String paymentInfo_TimeOut(Integer id){int timeNumber = 5;int age = 10/0;//try { TimeUnit.SECONDS.sleep(timeNumber); } catch (InterruptedException e) { e.printStackTrace(); }return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id:"+id+"\t"+"O(∩_∩)O哈哈~"+"  耗时"+timeNumber+"秒";}public String paymentInfo_TimeOutHandler(Integer id){return "线程池:"+Thread.currentThread().getName()+" 系统繁忙,请稍后再试,id:"+id+"\t"+"呜呜~";}

        4、消费方

         主启动类添加注解@EnableHystrix

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

       降级代码

@GetMapping("/consumer/payment/hystrix/timeout/{id}")@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")})public String paymentInfo_TimeOut(@PathVariable("id") Integer id){// int age = 10/0;String result = paymentHystrixService.paymentInfo_TimeOut(id);return result;}public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";}
  二、全局降级

      1、在controller类上添加@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")注解,并定义全局降级方法payment_Global_FallbackMethod。

        2、在需要降级的方法上面添加注解@HystrixCommand

  三、基于服务提供降级

        添加配置

feign:hystrix:enabled: true

        在openFeign接口上添加注解@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)

        创建降级类并实现openFeign接口

@Component
public class PaymentFallbackService implements PaymentHystrixService{@Overridepublic String paymentInfo_OK(Integer id) {return "----------------PaymentFallbackService fall back paymentInfo_OK";}@Overridepublic String paymentInfo_TimeOut(Integer id) {return "----------------PaymentFallbackService fall back paymentInfo_TimeOut";}
}
  四、服务熔断
@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"),// 失败率达到多少后跳闸})public String paymentCircuitBreaker(@PathVariable("id") Integer id){if(id < 0){throw new RuntimeException("******id 不能负数");}String serialNumber = IdUtil.simpleUUID();return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;}public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~   id: " +id;}
  五、DashBoard搭建

        添加配置,其中actuator依赖需要在每个被监控服务中添加

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

     启动类上添加注解@EnableHystrixDashboard

     访问http://localhost:9001/hystrix

     在被监控服务启动类里面添加如下代码

 /***此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑*ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",*只要在自己的项目里配置上下面的servlet就可以了*/@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}

     在上面打开的页面访问http://localhost:8001/hystrix.stream

http://www.lryc.cn/news/8562.html

相关文章:

  • JavaScript精简笔记
  • MySQL常用函数汇总
  • 100M网口客户电脑插上网线就断线,自己工厂正常,是什么问题导致?
  • 从零开始学习无人机 00 硬件配置
  • 免翻在Chrome上使用新必应(New Bing)聊天机器人
  • LA@特征值和特征向量
  • transpose代码学习
  • 【Redis】Redis 常用数据类型操作 ② ( 数据库操作 | 切换数据库 | 查询当前数据库键个数 | 清空当前数据库 | 清空所有数据库 )
  • 最简单的物体识别例子
  • 指针——“C”
  • 学习 Linux 内核书籍推荐
  • 深圳硬件黑客松活动,开放报名!
  • 力扣sql简单篇练习(十七)
  • Linux网络技术学习(六)—— 网络设备初始化(II)
  • 一手教你如何搭建Hadoop基于Zookeeper的集群(5台主机)
  • Spring Cloud是什么?怎么理解Spring Cloud?
  • robotframework + selenium自动化测试常见的问题
  • 2023春招java面试题及答案
  • QT+OpenGL光照
  • OpenCV-PyQT项目实战(7)项目案例03:鼠标框选
  • vue2版本《后台管理模式》(上)
  • C++与C基础重叠部分
  • 神经网络基础部件-卷积层详解
  • 【计算机网络】HTTPS协议原理
  • 21岁,华科博士在读,我的赛事Top经验
  • 基于ThinkPHP6.0+Vue+uni-app的多商户商城系统好用吗?
  • Linux中断
  • Excel+SQL实战项目 - 餐饮业日销售情况分析仪
  • 电商导购CPS,京东联盟如何跟单实现用户和订单绑定
  • Redis学习【6】之BitMap、HyperLogLog、Geospatial操作命令 (1)