SpringCloud微服务中如何详细配置和运用熔断器?

2026-05-25 21:071阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1179个文字,预计阅读时间需要5分钟。

SpringCloud微服务中如何详细配置和运用熔断器?

目录

一、简介

二、作用

三、核心概念

3.1 熔断目的 3.1.1 熔断目标 3.1.2 降级目标 3.2 降级目标

四、实例

4.1 基于 Hystrix 4.1.1 熔断触发降级 4.1.2 超时触发降级 4.1.3 资源隔离触发降级 4.2 基于 OpenFeign+pom.xml

一、简介

二、作用

三、核心概念

3.1 熔断目的 3.1.1 熔断目标 3.1.2 降级目标 3.2 降级目标

四、实例

4.1 基于 Hystrix 4.1.1 熔断触发降级 4.1.2 超时触发降级 4.1.3 资源隔离触发降级 4.2 基于 OpenFeign+pom.xml

目录
  • 一、简介
  • 二、作用
  • 三、核心概念
    • 3.1 熔断目的
    • 3.2 降级目的
  • 四、实例
    • 4.1 基于Hystrix
      • 4.1.1 熔断触发降级
      • 4.1.2 超时触发降级
      • 4.1.3 资源隔离触发降级
    • 4.2 基于OpenFeign pom.xml

    一、简介

    当微服务中的某个子服务,发生异常服务器宕机,其他服务在进行时不能正常访问而一直占用资源导致正常的服务也发生资源不能释放而崩溃,这时为了不造成整个微服务群瘫痪,进行的保护机制 就叫做熔断,是一种降级策略

    熔断的目的:保护微服务集群

    二、作用

    • 对第三方访问的延迟和故障进行保护和控制
    • 防止复杂分布式系统中的雪崩效应(级联故障)
    • 快速失败,快速恢复
    • 回退,尽可能优雅的降级
    • 启用近实时监控、报警和操作控制

    三、核心概念

    3.1 熔断目的

    应对雪崩效应,快速失败,快速恢复

    3.2 降级目的

    保证整体系统的高可用性

    四、实例

    4.1 基于Hystrix

    pom.xml

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

    application

    @EnableHystrix // 开启熔断 @SpringBootApplication public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } }

    4.1.1 熔断触发降级

    @RestController @RequestMapping("/hystrix") public class HystrixController { @Autowired private RestTemplate restTemplate; /** * @param num 参数 * @return 字符串 */ @HystrixCommand( commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // 默认20, 最小产生5次请求 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // 熔断时间, 该时间内快速失败 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 10s内失败率达到50%触发熔断 }, // 10s 内最少 5 个请求, 如果失败率大于 50% 则触发熔断 fallbackMethod = "fallback" ) // 服务调用失败时,触发熔断进行降级 @GetMapping("/test") public String test(@RequestParam Integer num) { if (num % 2 == 0) { return "访问正常"; } List<?> list = restTemplate.getForObject("localhost:7070/product/list", List.class); assert list != null; return list.toString(); } /** * 熔断时触发降级 * * @param num 参数 * @return 字符串 */ private String fallback(Integer num) { // 降级操作 return "系统繁忙"; } }

    4.1.2 超时触发降级

    @RestController @RequestMapping("/hystrix") public class HystrixController { @Autowired private RestTemplate restTemplate; @HystrixCommand( commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500") }, fallbackMethod = "timeout" ) @GetMapping("/timeout") public String timeoutTest(){ return restTemplate.getForObject("localhost:7070/product/list", String.class); } /** * 超时时触发降级 * * @return 字符串 */ private String timeout() { // 降级操作 return "请求超时"; } }

    4.1.3 资源隔离触发降级

    平台隔离、业务隔离、部署隔离

    线程池隔离、信号量隔离

    SpringCloud微服务中如何详细配置和运用熔断器?

    4.2 基于OpenFeign pom.xml

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

    application

    @SpringBootApplication @ComponentScan(basePackages = { "com.study.provider.service", // feign 包目录 "com.study.hystrix" // 当前模块启动目录 }) @EnableFeignClients(basePackages = "com.study.provider.service") // feign 目录 public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } }

    HystrixFeignController

    @RestController @RequestMapping("/hystrixFeign") public class HystrixFeignController { @Qualifier("com.study.provider.service.ProductionService") @Autowired ProductionService productService; // feign Client @GetMapping("test") public String test(){ return productService.getProduction(1).toString(); // 远程调用 } }

    feign Client

    @FeignClient(value = "provider", fallback = ProductionFallback.class) // fallback 用于熔断 public interface ProductionService { @RequestMapping("/product/getProduction") Object getProduction(@RequestParam Integer id); }

    ProductionFallback

    @Component public class ProductionFallback implements ProductionService { @Override public Object getProduction(Integer id) { return "请求失败"; } @Override public Map createProduction(Object production) { return new HashMap<>(); } @Override public Object selectByProduct(Object product) { return "请求失败"; } }

    到此这篇关于SpringCloud微服务熔断器使用详解的文章就介绍到这了,更多相关SpringCloud熔断器内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

    本文共计1179个文字,预计阅读时间需要5分钟。

    SpringCloud微服务中如何详细配置和运用熔断器?

    目录

    一、简介

    二、作用

    三、核心概念

    3.1 熔断目的 3.1.1 熔断目标 3.1.2 降级目标 3.2 降级目标

    四、实例

    4.1 基于 Hystrix 4.1.1 熔断触发降级 4.1.2 超时触发降级 4.1.3 资源隔离触发降级 4.2 基于 OpenFeign+pom.xml

    一、简介

    二、作用

    三、核心概念

    3.1 熔断目的 3.1.1 熔断目标 3.1.2 降级目标 3.2 降级目标

    四、实例

    4.1 基于 Hystrix 4.1.1 熔断触发降级 4.1.2 超时触发降级 4.1.3 资源隔离触发降级 4.2 基于 OpenFeign+pom.xml

    目录
    • 一、简介
    • 二、作用
    • 三、核心概念
      • 3.1 熔断目的
      • 3.2 降级目的
    • 四、实例
      • 4.1 基于Hystrix
        • 4.1.1 熔断触发降级
        • 4.1.2 超时触发降级
        • 4.1.3 资源隔离触发降级
      • 4.2 基于OpenFeign pom.xml

      一、简介

      当微服务中的某个子服务,发生异常服务器宕机,其他服务在进行时不能正常访问而一直占用资源导致正常的服务也发生资源不能释放而崩溃,这时为了不造成整个微服务群瘫痪,进行的保护机制 就叫做熔断,是一种降级策略

      熔断的目的:保护微服务集群

      二、作用

      • 对第三方访问的延迟和故障进行保护和控制
      • 防止复杂分布式系统中的雪崩效应(级联故障)
      • 快速失败,快速恢复
      • 回退,尽可能优雅的降级
      • 启用近实时监控、报警和操作控制

      三、核心概念

      3.1 熔断目的

      应对雪崩效应,快速失败,快速恢复

      3.2 降级目的

      保证整体系统的高可用性

      四、实例

      4.1 基于Hystrix

      pom.xml

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

      application

      @EnableHystrix // 开启熔断 @SpringBootApplication public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } }

      4.1.1 熔断触发降级

      @RestController @RequestMapping("/hystrix") public class HystrixController { @Autowired private RestTemplate restTemplate; /** * @param num 参数 * @return 字符串 */ @HystrixCommand( commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // 默认20, 最小产生5次请求 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // 熔断时间, 该时间内快速失败 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 10s内失败率达到50%触发熔断 }, // 10s 内最少 5 个请求, 如果失败率大于 50% 则触发熔断 fallbackMethod = "fallback" ) // 服务调用失败时,触发熔断进行降级 @GetMapping("/test") public String test(@RequestParam Integer num) { if (num % 2 == 0) { return "访问正常"; } List<?> list = restTemplate.getForObject("localhost:7070/product/list", List.class); assert list != null; return list.toString(); } /** * 熔断时触发降级 * * @param num 参数 * @return 字符串 */ private String fallback(Integer num) { // 降级操作 return "系统繁忙"; } }

      4.1.2 超时触发降级

      @RestController @RequestMapping("/hystrix") public class HystrixController { @Autowired private RestTemplate restTemplate; @HystrixCommand( commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500") }, fallbackMethod = "timeout" ) @GetMapping("/timeout") public String timeoutTest(){ return restTemplate.getForObject("localhost:7070/product/list", String.class); } /** * 超时时触发降级 * * @return 字符串 */ private String timeout() { // 降级操作 return "请求超时"; } }

      4.1.3 资源隔离触发降级

      平台隔离、业务隔离、部署隔离

      线程池隔离、信号量隔离

      SpringCloud微服务中如何详细配置和运用熔断器?

      4.2 基于OpenFeign pom.xml

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

      application

      @SpringBootApplication @ComponentScan(basePackages = { "com.study.provider.service", // feign 包目录 "com.study.hystrix" // 当前模块启动目录 }) @EnableFeignClients(basePackages = "com.study.provider.service") // feign 目录 public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } }

      HystrixFeignController

      @RestController @RequestMapping("/hystrixFeign") public class HystrixFeignController { @Qualifier("com.study.provider.service.ProductionService") @Autowired ProductionService productService; // feign Client @GetMapping("test") public String test(){ return productService.getProduction(1).toString(); // 远程调用 } }

      feign Client

      @FeignClient(value = "provider", fallback = ProductionFallback.class) // fallback 用于熔断 public interface ProductionService { @RequestMapping("/product/getProduction") Object getProduction(@RequestParam Integer id); }

      ProductionFallback

      @Component public class ProductionFallback implements ProductionService { @Override public Object getProduction(Integer id) { return "请求失败"; } @Override public Map createProduction(Object production) { return new HashMap<>(); } @Override public Object selectByProduct(Object product) { return "请求失败"; } }

      到此这篇关于SpringCloud微服务熔断器使用详解的文章就介绍到这了,更多相关SpringCloud熔断器内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!