Spring cloud 熔断器Hystrix

本文详细介绍了在微服务架构下如何利用Hystrix实现服务容灾与服务降级,包括熔断机制、资源隔离、限流、超时设计及服务降级策略。通过Hystrix框架,开发者能够方便地对服务进行容错处理,提高系统的稳定性和可靠性。此外,文章还展示了如何在Ribbon和Feign中集成Hystrix,并提供了仪表盘监控和Turbine聚合服务信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 在微服务的体系下,服务之间是与依赖关系的!服务的调用中也存在着各种可能的问题,如果一个服务处理缓慢,可能会导致服务被阻塞住,导致整个系统被拖慢,甚至是崩溃。这种情况下可以使用熔断的机制,让服务降级,不让问题继续蔓延。
  • Hystrix,是Netflix的一个开源熔断器,通过Hystrix,我们可以很方便的实现资源隔离、限流、超时设计、服务降级等服务容灾措施,并且还提供了强大的监控,可以查看各个熔断器的允许情况。
  • Hystrix框架图
  •  
  • Hystrix提供了一个HystrixCommand用来包装调用请求。HystrixCommand的执行流程大概如下
  • 首先检查缓存中是否有结果。如果有则直接返回缓存结果。
  • 判断断路器是否开启,如果断路器闭合,执行降级业务逻辑并返回降级结果。
  • 判断信号量/线程池资源是否饱和,如饱和则执行降级业务逻辑并返回降级结果。
  • 调用实际服务,如发生异常,执行降级业务逻辑并返回降级结果,并调整断路器阈值。
  • 判断实际业务是否超时,超时则返回超时响应结果,并调整断路器阈值。
  • Ribbon使用Hystrix

  • 演示项目
  • 在需要进行熔断的服务上进行修改
  • 修改pom.xml文件,增加Hystrix的依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            </dependency>
    
  • 启动类上增加Hystrix的注解
  • @SpringBootApplication
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class HystrixRibbonApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixRibbonApp.class, args);
        }
        
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
  • 修改代码逻辑
  • @RestController
    public class HelloController {
        @Autowired
        HystrixRibbonService helloService;
        
        @RequestMapping("/hi")
        public String hello(){
            return helloService.helloService("姓名");
        }
    }
    
  • 在需要熔断的方法上增加@HystrixCommand注解,当调用有问题的时候就会使用fallbackMethod参数指定的方法进行服务降级
  • @Service
    public class HystrixRibbonService {
        private static final String SERVICE_NAME = "EUREKACLIENT";
        
        @Autowired
        RestTemplate restTemplate;
        
        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        @HystrixCommand(fallbackMethod = "helloServiceFallBack")
        public String helloService(String name) {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose(SERVICE_NAME);
            System.out.println("服务主机:" + serviceInstance.getHost());
            System.out.println("服务端口:" + serviceInstance.getPort());
            
            //  通过服务名来访问
            return restTemplate.getForObject("http://" + SERVICE_NAME + "/hello?name="+name,String.class);
        }
    
        @SuppressWarnings("unused")
        private String helloServiceFallBack(String name) {
            return "这个是失败的信息!";
        }
    }
    
  • Feign使用Hystrix

  • 演示项目
  • 修改pom.xml文件,增加Hystrix的依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
    
  • 增加注解
  • @SpringBootApplication
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class HystrixFeignApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixFeignApp.class, args);
        }
    }
    
  • 书写逻辑代码
  • @RestController
    public class HelloController {
        @Autowired
        HystrixFeignService helloService;
        
        @RequestMapping("/hifeign")
        public String hello(){
            return helloService.sayHiUseFeign("姓名");
        }
    }
    
  • 指定fallback的类
  • @FeignClient(value="EUREKACLIENT", fallback = HystrixFeignServiceFallback.class)
    public interface HystrixFeignService {
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        String sayHiUseFeign(@RequestParam(value = "name") String name);
    }
    
  • fallback的逻辑,是服务接口的一个实现,书写服务降级逻辑
  • @Component
    public class HystrixFeignServiceFallback implements HystrixFeignService{
        @Override
        public String sayHiUseFeign(String name) {
            return "feign调用错误!";
        }
    }
    
  • 仪表盘

  • 在pom.xml中增加依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  • 增加注解
  • @EnableHystrixDashboard
    @SpringCloudApplication
    public class HystrixRibbonApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixRibbonApp.class, args);
        }
        
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
  • 需要有@HystrixCommand熔断命令被使用
  • 启动应用:http://host:port/hystrix

    这是一个可爱的小熊

  • 监控的画面
  • 不错的资料:
  • http://blog.youkuaiyun.com/zheng0518/article/details/51713900
  • https://github.com/Netflix/Hystrix/wiki/How-To-Use
  • Hystrix Turbine聚合Hystrix stream信息

  • Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。
  • 新建一个项目:springCloudTurbine
  • 增加pom.xml依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  • 增加注解
  • @SpringBootApplication
    @EnableTurbine
    public class ServiceTurbineApplication {
        public static void main(String[] args) {
            new SpringApplicationBuilder(ServiceTurbineApplication.class).web(true).run(args);
        }
    }
    
  • 修改配置信息
    可以在turbine.appConfig配置监控的应用名称
  • eureka.client.serviceUrl.defaultZone=http://master:8671/eureka/,http://backup:8672/eureka
    #eureka.client.serviceUrl.defaultZone=http://lxm:111111@localhost:1111/eureka/
    security.basic.enabled=false
    # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
    turbine.aggregator.clusterConfig=default
    # 配置Eureka中的serviceId列表,表明监控哪些服务
    turbine.appConfig=hystrixClient1,hystrixClient2,hystrixClient3
    turbine.clusterNameExpression=new String("default")
    # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
    # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
    # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC  
    
  • 启动应用:http://host:port/hystrix
    监控:http://host:port/turbine.stream
  •  
  •  
  • 作者:hutou
  • 链接:https://www.jianshu.com/p/a28960a8e829
  • 來源:简书
  • 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用技术四大架构的内容关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级专业级价值流,细化业务能力、流程对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致高效使用。应用架构方面,提出了分层解耦服务化的设计原则,以提高灵活性响应速度。最后,技术架构部分围绕技术框架、组件、平台部署节点进行了详细设计,确保技术架构的稳定性扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理应用开发,确保数据的一致性应用的高效性;③为技术选型系统部署提供科学依据,确保技术架构的稳定性扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解实践,重点关注各架构模块之间的关联协同,以便更好地应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值