SpringCloud之微服务容错的实现

作者:卿亦离 时间:2023-11-29 02:02:22 

1.雪崩效应

SpringCloud之微服务容错的实现

雪崩效应

如上图所示,假设我们有3个微服务A,B,C,A调用B,B调用C,如果C挂掉了,由于B是同步调用,不断等待,导致资源耗尽,B也挂掉,接下来A也挂掉了,造成了雪崩效应!为了防止雪崩效应,所以我们要在本篇文章中介绍Hystrix。

2.Hystrix

SpringCloud之微服务容错的实现

Hystrix

hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,反正这 * 我是没吃过的,不敢吃。那么Hystrix又具备什么样的特点呢:

2.1服务降级

双11的时候我们经常看到哎哟被挤爆了,这其实就是一种服务降级。那么Hystrix是怎么做到服务降级的呢?

2.1.1优先核心服务,非核心服务不可用或者弱可用

比如在一个系统中我们可能有限保证订单和支付服务,但是对于广告之类的服务就若花掉

2.1.2通过HystrixCommand注解

2.1.3fallbackMethod中具体实现降级逻辑

2.1.4具体使用

2.1.4.1pom.xml


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

2.1.4.2启动类


//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker
@SpringCloudApplication
public class OrderApplication {
public static void main(String[] args) {
   SpringApplication.run(OrderApplication.class, args);
 }
}

在这里我们引入了@EnableCircuitBreaker这个注解,但是可以看到我们的main函数所在的类上面有好多注解了,我们这里用一个SpringCloudApplication注解包含上面的3个注解

2.1.4.3HystrixCommand与fallback的使用


@HystrixCommand(fallbackMethod="fallback")
 @GetMapping("/getProductInfoList")
 public String getProductInfoList(@RequestParam("number") Integer number) {
   RestTemplate restTemplate = new RestTemplate();
   return restTemplate.postForObject("http://127.0.0.1:8005/product/listForOrder",
       Arrays.asList("157875196366160022"),
       String.class);
 }
private String fallback() {
   return "太拥挤了, 请稍后再试~~";
 }

假设你的方法有几万个,上面的这种写法用@HystrixCommand这个注解肯定要用几万次,太麻烦了,我们来优化和更新一下:


@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
@HystrixCommand(commandProperties = {
     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
 })
 @GetMapping("/getProductInfoList")
 public String getProductInfoList(@RequestParam("number") Integer number) {
   RestTemplate restTemplate = new RestTemplate();
   return restTemplate.postForObject("http://127.0.0.1:8005/product/listForOrder",
       Arrays.asList("157875196366160022"),
       String.class);
 }
private String defaultFallback() {
   return "默认提示:太拥挤了, 请稍后再试~~";
 }
}

直接在Controller上面写了一个DefaultProperties这样的注解,然后定义了一个defaultFallback这样的方法,我们在getProductInfoList这个方法上的@HystrixCommand注解上可以不在注明fallback方法了,因为我们都用defaultFallback了。但是在这里我们用了一个commandProperties = {

@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),这个表示当调用方法超过3秒钟时,就服务降级

2.2依赖隔离

Hystrix使用依赖隔离完成的是线程池的隔离,它为每个HystrixCommand创建一个单独的线程池。这样某个在HystrixCommand包装下的依赖服务出现延迟过高的情况,也只是对该依赖的服务调用产生影响,并不会拖慢其他服务。使用了HystrixCommand将某个函数包装成Hystrix命令时,Hystrix框架就会自动的为这个函数实现了依赖隔离,所以依赖隔离和服务降级是一体化实现的。

2.3服务熔断

当服务调用发生错误到一定比例的时候,断路器就会打开,服务的调用会转向另外一个指定的调用方法,这就是服务熔断,这样说比较抽象,我们看看下面这个图:

SpringCloud之微服务容错的实现

断路器

断路器一共有三个状态,初始的时候是closed状态,如果用户不断调用且失败次数达到了一定的阈值,这个时候断路器就会变成open状态,接下来用户不断调用这个接口都是进入到另一个缺省方法中。但是断路器不会一直处于open状态,当open状态达到一定时间后,断路器会变成half open状态,进入half open状态后,将会允许请求再次访问真正的服务,如果访问成功了,断路器会变成closed状态,服务恢复正常,如果还失败又再次进入到open状态。

2.3.1代码演示


 @HystrixCommand(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"),  //错误率
 })
 @GetMapping("/getProductInfoList")
 public String getProductInfoList(@RequestParam("number") Integer number) {
   if (number % 2 == 0) {
     return "success";
   }
RestTemplate restTemplate = new RestTemplate();
   return restTemplate.postForObject("http://127.0.0.1:8005/product/listForOrder",
       Arrays.asList("157875196366160022"),
       String.class);
 }

通过代码,我们可以看到,有四个参数,但是最重要的还是这三个参数:

1.circuitBreaker.requestVolumeThreshold 请求数达到后才计算

2.circuitBreaker.sleepWindowInMilliseconds open状态达到这个时间后,就会进入到半熔断状态

3.circuitBreaker.errorThresholdPercentage 失败请求比例

2.3.2使用配置项

从上面的代码中,我们可以看到,很多配置项都是写在了代码里面,接下来,我们把这些配置放到配置文件里面去:


@HystrixCommand
 @GetMapping("/getProductInfoList")
 public String getProductInfoList(@RequestParam("number") Integer number) {
   if (number % 2 == 0) {
     return "success";
   }
RestTemplate restTemplate = new RestTemplate();
   return restTemplate.postForObject("http://127.0.0.1:8005/product/listForOrder",
       Arrays.asList("157875196366160022"),
       String.class);
 }

在yaml文件中添加这样的配置:


hystrix:
command:
 default:
  execution:
   isolation:
    thread:
     timeoutInMilliseconds: 1000
 getProductInfoList:
  execution:
   isolation:
    thread:
     timeoutInMilliseconds: 3000

在yaml文件中有一个getProductInfoList配置项,和方法名一模一样,还有一个default选项,default选项表示所有被HystrixCommand注解的方法都可以用这个配置,getProductInfoList表示只有这个方法可以用这个配置项。

2.3.3feign-hystrix的使用

上面的小章节我们使用的是RestTemplate模板,接下来我们使用feign配合hystrix来进行使用,有意思的是大家可以看看spring-cloud-starter-feign的pom.xml其实已经在内部依赖了hystrix。

2.3.3.1yaml配置文件


feign:
hystrix:
 enabled: true

2.3.3.2FeignClient


@FeignClient(name = "product", fallback = ProductClient.ProductClientFallback.class)
public interface ProductClient {
@PostMapping("/product/listForOrder")
 List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList);
@PostMapping("/product/decreaseStock")
 void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList);
@Component
 static class ProductClientFallback implements ProductClient {
@Override
   public List<ProductInfoOutput> listForOrder(List<String> productIdList) {
     return null;
   }
@Override
   public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
}
 }
}

两个注意点,一个是fallback的配置,还有一个是内部类需要加上@Component注解

2.3.3.3调用方记得加上ComponentScan

 2.4监控HystrixDashBoard

2.4.1pom.xml

spring-cloud-starter-hystrix-dashboard

2.4.2yaml文件


management:
context-path: /

2.4.3访问

IP:PORT/hystrix

来源:https://www.jianshu.com/p/2dda8ac85a27

标签:Spring,Cloud,微服务容错
0
投稿

猜你喜欢

  • springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

    2023-08-16 10:26:45
  • SpringBoot快速搭建实现三步骤解析

    2021-06-12 22:33:24
  • unity实现场景跳转

    2023-08-30 22:58:15
  • Android 校验email是否合法实现代码

    2021-06-02 05:18:24
  • Java基于Tcp的基础聊天功能实例

    2023-11-25 05:26:56
  • 利用Android画圆弧canvas.drawArc()实例详解

    2022-05-21 08:45:54
  • 详解WPF中用户控件和自定义控件的使用

    2023-07-25 12:20:26
  • Android应用退出登录的实现方法

    2023-11-15 19:52:44
  • opencv3/C++ FLANN特征匹配方式

    2021-08-19 10:17:47
  • Spring security如何重写Filter实现json登录

    2023-09-15 13:33:31
  • 一篇文章弄懂Java8中的时间处理

    2022-07-22 07:56:32
  • 浅析Java多线程同步synchronized

    2023-05-20 15:52:29
  • Java Document生成和解析XML操作

    2021-11-10 13:17:46
  • Spring Cloud基于zuul实现网关过程解析

    2021-09-11 06:52:19
  • c#编写的番茄钟倒计时器代码

    2023-02-23 12:21:50
  • C++实现俄罗斯方块源码

    2023-06-27 23:49:14
  • Android简易音乐播放器实现代码

    2021-12-22 23:26:53
  • c# 基于GMap.NET实现电子围栏功能(WPF版)

    2023-01-28 05:13:01
  • Flutter自定义圆盘取色器

    2023-07-05 23:55:43
  • springboot+swagger2.10.5+mybatis-plus 入门详解

    2023-03-15 01:58:38
  • asp之家 软件编程 m.aspxhome.com