前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Hystrix简单实用

Spring Cloud Hystrix简单实用

作者头像
ha_lydms
发布2023-08-10 09:19:53
1240
发布2023-08-10 09:19:53
举报
文章被收录于专栏:学习内容学习内容

一、简介

请添加图片描述
请添加图片描述

Hystrix,英文意思是豪猪,全身是刺,刺是一种保护机制。Hystrix也是Netflflix公司的一款组件。

Hystrix是什么?

在分布式环境中,许多服务依赖项中的部分服务必然有概率出现失败。Hystrix是一个库,通过添加延迟和容错逻辑,来帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点阻止级联失败,通过提供回退选项来实现防止级联出错。提高了系统的整体弹性。与Ribbon并列,也几乎存在于每个Spring Cloud构建的微服务和基础设施中。

Hystrix被设计的目标是:

  • 对通过第三方客户端库访问的依赖项(通常是通过网络)的延迟和故障进行保护和控制。
  • 在复杂的分布式系统中阻止雪崩效应。
  • 快速失败,快速恢复。
  • 回退,尽可能优雅地降级。

二、快速开始

1、pom依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2、启动类注解

代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//注解简化写法:微服务中,注解往往引入多个,简化注解可以使用组合注解。
// @SpringCloudApplication =等同于
// @SpringBootApplication+@EnableDiscoveryClient+@EnableCircuitBreaker
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker   //开启熔断
public class DemoHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoHystrixApplication.class, args);
    }

    @Bean
    @LoadBalanced
    //开启负载均衡
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3、服务降级配置@HystrixCommand

使用@HystrixCommand定义fallback方法

代码语言:javascript
复制
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestHystrixController {

    @RequestMapping("/hystrix")
    @HystrixCommand(fallbackMethod = "queryByIdFallBack")
    public String queryById() {
        System.out.println("testBystrix");
        return "testBystrix";
    }

    public String queryByIdFallBack() {
        return "对不起,网络太过拥挤";
    }
}

4、配置熔断策略

  1. 常见熔断策略配置
  2. 熔断后休眠时间:sleepWindowInMilliseconds
  3. 熔断触发最小请求次数:requestVolumeThreshold
  4. 熔断触发错误比例阈值:errorThresholdPercentage
  5. 熔断超时时间:timeoutInMilliseconds

官方配置文件地址

代码语言:javascript
复制
https://github.com/Netflix/Hystrix/wiki/Configuration#circuitBreaker.forceOpen
代码语言:javascript
复制
# 配置熔断策略:
# 强制打开熔断器 默认false关闭的。测试配置是否生效
hystrix.command.default.circuitBreaker.forceOpen: false
# 触发熔断错误比例阈值,默认值50%
hystrix.command.default.circuitBreaker.errorThresholdPercentage: 20
# 熔断后休眠时长,默认值5秒
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds: 60000
# 熔断触发最小请求次数,默认值是20
hystrix.command.default.circuitBreaker.requestVolumeThreshold: 5
# 熔断超时设置,默认为1秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 2000

5、测试

请添加图片描述
请添加图片描述

三、原理分析

熔断器的原理如同电力过载保护器。

熔断器状态机有3个状态:

  • 关闭状态,所有请求正常访问
  • 打开状态,所有请求都会被降级。
    • Hystrix会对请求情况计数,当一定时间失败请求百分比达到阈值,则触发熔断,断路器完全关闭
    • 默认失败比例的阈值是50%,请求次数最低不少于20次
  • 半开状态
    • 打开状态不是永久的,打开一会后会进入休眠时间(默认5秒)。休眠时间过后会进入半开状态。
    • 半开状态:熔断器会判断下一次请求的返回状况,如果成功,熔断器切回关闭状态。如果失败,熔断器切回打开状态。
请添加图片描述
请添加图片描述

熔断器的核心解决方案:线程隔离和服务降级。

  • 线程隔离。
  • 服务降级(兜底方法)

线程隔离和服务降级之后,用户请求故障时,线程不会被阻塞,更不会无休止等待或者看到系统奔溃,至少可以看到执行结果(熔断机制)。

什么时候熔断:

  1. 访问超时
  2. 服务不可用(死了)
  3. 服务抛出异常(虽然有异常但还活着)
  4. 其他请求导致服务异常到达阈值,所有服务都会被降级

四、实际使用

**注意:**熔断服务降级方法必须保证与被降级方法相同的参数列表和返回值

两种编写方式:编写在类上,编写在方法上。在类的上边对类的所有方法都生效。在方法上,仅对当前方法有效。

方法上:服务降级的fallback兜底方法

  • 使用HystrixCommon注解,定义
  • @HystrixCommand(fallbackMethod="queryByIdFallBack")用来声明一个降级逻辑的fallback兜底方法。

类上:默认服务降级的fallback兜底方法

  • 刚才把fallback写在了某个业务方法上,如果方法很多,可以将FallBack配置加在类上,实现默认FallBack
  • @DefaultProperties(defaultFallback=”defaultFallBack“),在类上,指明统一的失败降级方法;
代码语言:javascript
复制
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "defaultFallback")
//开启默认的 FallBack,统一失败降级方法(兜底) 
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{id}")
    @HystrixCommand
    public String queryById(@PathVariable Long id) {
        //如果参数为1抛出异常,否则 执行REST请求返回user对象 
        if (id == 1) {
            throw new RuntimeException("too busy!!!");
        }
        String url = String.format("http://user-service/user/%d", id);
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * queryById的降级方法
     */
    public String queryByIdFallback(Long id) {
        return "对不起,网络太拥挤了!";
    }

    /**
     * 默认降级方法
     */
    public String defaultFallback() {
        return "默认提示:对不起,网络太拥挤了!";
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、快速开始
    • 1、pom依赖
      • 2、启动类注解
        • 3、服务降级配置@HystrixCommand
          • 4、配置熔断策略
            • 5、测试
            • 三、原理分析
            • 四、实际使用
            相关产品与服务
            负载均衡
            负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档