SpringCloud微服务熔断器使用详解

 更新时间:2022年09月30日 08:32:36   作者:Dily_Su  
这篇文章主要介绍了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一个组件,在整个生态中主要为我们提供服务隔离,服务熔断,服务降级功能,本文给大家介绍的非常详细,需要的朋友可以参考下

一、简介

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

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

二、作用

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

三、核心概念

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("http://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("http://localhost:7070/product/list", String.class);
    }
    /**
     * 超时时触发降级
     *
     * @return 字符串
     */
    private String timeout() {
        // 降级操作
        return "请求超时";
    }
}

4.1.3 资源隔离触发降级

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

线程池隔离、信号量隔离

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熔断器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java笔记之从IO模型到Netty框架学习初识篇

    Java笔记之从IO模型到Netty框架学习初识篇

    Netty作为一个已经发展了十多年的框架,已然非常成熟了,其中有大量的细节是普通使用者不知道或者不关心的,本文带你查缺补漏掌握Netty的使用
    2022-03-03
  • Spring中@Configuration注解和@Component注解的区别详解

    Spring中@Configuration注解和@Component注解的区别详解

    这篇文章主要介绍了Spring中@Configuration注解和@Component注解的区别详解,@Configuration 和 @Component 到底有何区别呢?我先通过如下一个案例,在不分析源码的情况下,小伙伴们先来直观感受一下这两个之间的区别,需要的朋友可以参考下
    2023-09-09
  • Springboot如何基于assembly服务化实现打包

    Springboot如何基于assembly服务化实现打包

    这篇文章主要介绍了Springboot如何基于assembly服务化实现打包,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • spring-boot中使用spring-boot-devtools的实现代码

    spring-boot中使用spring-boot-devtools的实现代码

    这篇文章主要介绍了spring-boot中使用spring-boot-devtools的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Java语法基础之循环结构语句详解

    Java语法基础之循环结构语句详解

    这篇文章主要为大家详细介绍了Java语法基础之循环结构语句,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 在Spring中如何处理循环依赖问题

    在Spring中如何处理循环依赖问题

    Spring解决循环依赖的关键在于提前曝光未完全创建的bean,在Spring中创建Bean分为实例化、属性填充和初始化三步,循环依赖的解决思路是先创建A的bean实例,此时A是不完整的,用一个Map保存不完整的A,再创建B,B需要注入A
    2025-01-01
  • SpringBoot 如何从配置文件读取值到对象中

    SpringBoot 如何从配置文件读取值到对象中

    这篇文章主要介绍了SpringBoot 如何从配置文件读取值到对象中,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java中BeanUtils.copyProperties基本用法与小坑

    Java中BeanUtils.copyProperties基本用法与小坑

    本文主要介绍了Java中BeanUtils.copyProperties基本用法与小坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • elasticsearch集群查询超10000的解决方案

    elasticsearch集群查询超10000的解决方案

    ES为了避免用户的过大分页请求造成ES服务所在机器内存溢出,默认对深度分页的条数进行了限制,默认的最大条数是10000条,这篇文章主要给大家介绍了关于elasticsearch集群查询超10000的解决方案,需要的朋友可以参考下
    2024-08-08
  • Java中ArrayList和LinkedList有什么区别举例详解

    Java中ArrayList和LinkedList有什么区别举例详解

    这篇文章主要介绍了Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影响、扩容机制、线程安全与并发方案,以及工程实践场景,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-02-02

最新评论