SpringCloud-Hystrix组件使用

时间:2020-12-15 00:03:05   收藏:0   阅读:30

1.服务雪崩

1.服务雪崩

2.图解雪崩效应

技术分享图片

技术分享图片

2.服务熔断

1.服务熔断

2.服务熔断图示

技术分享图片

3.服务降级

1.服务降级说明

淘宝 删除地址 确认收货 删除订单 取消支付 节省cpu 内存

技术分享图片

4.降级和熔断总结

1.共同点

2.异同点

3.总结

5.服务熔断的实现

服务熔断的实现思路

1.项目中引入hystrix依赖

在商品服务下面
<!--引入hystrix-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.开启断路器

@SpringBootApplication
@EnableCircuitBreaker  //用来开启断路器
public class Products9998Application {
    public static void main(String[] args) {
        SpringApplication.run(Products9998Application.class, args);
    }
}

3.使用HystrixCommand注解实现断路

//服务熔断
@GetMapping("/product/break")
@HystrixCommand(fallbackMethod = "testBreakFall" )
public String testBreak(int id){
  log.info("接收的商品id为: "+ id);
  if(id<=0){
    throw new RuntimeException("数据不合法!!!");
  }
  return "当前接收商品id: "+id;
}

// 触发熔断的方法
public String testBreakFall(int id){
  return "当前数据不合法: "+id;
}

技术分享图片

4.访问测试

技术分享图片

技术分享图片

一直使用错误参数访问,那么再使用正常参数访问也会显示不合法,因为触发了断路器,但过一点时间又会自动的关闭,访问又合法了

5.总结

6.断路器打开条件

A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service exceed circuitBreaker.requestVolumeThreshold (default: 20 requests) and the failure percentage is greater than circuitBreaker.errorThresholdPercentage (default: >50%) in a rolling window defined by metrics.rollingStats.timeInMilliseconds (default: 10 seconds), the circuit opens and the call is not made. In cases of error and an open circuit, a fallback can be provided by the developer. --摘自官方

原文翻译之后,总结打开关闭的条件:

技术分享图片

7.默认的服务FallBack处理方法

@GetMapping("/product/hystrix")
@HystrixCommand(defaultFallback = "testHystrixFallBack") //通过HystrixCommand降级处理 指定出错的方法
public String testHystrix(String name) {
  log.info("接收名称为: " + name);
  int n = 1/0;
  return "服务[" + port + "]响应成功,当前接收名称为:" + name;
}
//服务降级处理
public String testHystrixFallBack(String name) {
  return port + "当前服务已经被降级处理!!!,接收名称为: "+name;
}

技术分享图片

6.服务降级的实现

还是再之前项目的基础之上

1.客户端openfeign + hystrix实现服务降级实现

2.开启openfeign支持服务降级

feign.hystrix.enabled=true #开启openfeign支持降级

3.在openfeign客户端中加如Hystrix

// 创建一个ProductClientFallBack类实现这个接口,并实现这个接口的所有方法,为了对每个方法做不同的响应错略
// 指定当前的接口是openfeign组件,value是调用的服务名
@FeignClient(value = "products",fallback = ProductClientFallBack.class)
public interface ProductClient {

    @GetMapping("/product/findOne")
    Map<String, Object> findOne(@RequestParam("productId") String productId);

}

4.开发fallback处理类

package com.md.fallback;

@Component
public class ProductClientFallBack implements ProductClient {

    @Override
    public Map<String, Object> findOne(String productId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","false");
        map.put("msg","当前查询不可以使用,服务已经被降级");
        return map;
    }
}

正常访问

技术分享图片

然后直接将产品服务关闭,再进行访问

技术分享图片

注意:如果服务端降级和客户端降级同时开启,要求服务端降级方法的返回值必须与客户端方法降级的返回值一致!!!

7.Hystrix Dashboard

0.说明

技术分享图片

只是一个有UI页面的组件,创建一个新的项目,还是根据之前的springcloud的环境搭建

1.项目中引入依赖

<!--引入hystrix dashboard 依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2.入口类中开启hystrix dashboard

@SpringBootApplication
@EnableHystrixDashboard //开启监控面板
public class Hystrixdashboard9990Application {
	public static void main(String[] args) {
		SpringApplication.run(Hystrixdashboard9990Application.class, args);
  }
}

在配置文件中指定端口号9990

3.启动hystrix dashboard应用

技术分享图片

4.监控的项目中入口类中加入监控路径配置[新版本坑],并启动监控项目

@Bean
public ServletRegistrationBean getServlet() {
  HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
  ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
  registrationBean.setLoadOnStartup(1);
  registrationBean.addUrlMappings("/hystrix.stream");
  registrationBean.setName("HystrixMetricsStreamServlet");
  return registrationBean;
}

技术分享图片

5.通过监控界面监控

技术分享图片

后面的hystrix.stream是固定的

6.点击监控,一致loading,打开控制台发现报错[特别坑]

技术分享图片

# 解决方案
- 新版本中springcloud将jquery版本升级为3.4.1,定位到monitor.ftlh文件中,js的写法如下:
	$(window).load(function() 
- jquery 3.4.1已经废弃上面写法

- 修改方案 修改monitor.ftlh为如下调用方式:
	$(window).on("load",function()
	
- 编译jar源文件,重新打包引入后,界面正常响应。

技术分享图片

8.Hystrix停止维护

官方地址:https://github.com/Netflix/Hystrix

原文:https://www.cnblogs.com/mengd/p/14136067.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!