快速搭建 Spring Cloud Hytrix

本文探讨了微服务架构中服务故障的“雪崩”效应,并介绍了Hystrix断路器如何通过服务降级、熔断、线程隔离等功能解决依赖隔离问题,提升系统稳定性和容错能力。

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


在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,产生了断路器等一系列的服务保护机制。

Hystrix

Spring Cloud Hytrix 实现了断路器、线程隔离等一系列服务保护功能。它是基于Netflix的开源框架实现的,该框架的目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hytrix具备服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大的功能。

Hystrix如何解决依赖隔离

1:Hystrix使用命令模式HystrixCommand(Command)包装依赖调用逻辑,每个命令在单独线程中/信号授权下执行。

2:可配置依赖调用超时时间,超时时间一般设为比99.5%平均时间略高即可.当调用超时时,直接返回或执行fallback逻辑。

3:为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间。

4:依赖调用结果分:成功,失败(抛出异常),超时,线程拒绝,短路。 请求失败(异常,拒绝,超时,短路)时执行fallback(降级)逻辑。

5:提供熔断器组件,可以自动运行或手动调用,停止当前依赖一段时间(10秒),熔断器默认错误率阈值为50%,超过将自动运行。

6:提供近实时依赖的统计和监控

在Ribbon中使用Hytrix

1、New Model -> Spring Initializer -> Next -> Next -> Web(Web)、Cloud Discovery(Eureka Discovery)、Cloud Routing(Ribbon)、Cloud Circuit Breaker(Hystrix)-> Next ->Finish

2、pom文件如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3、这里写图片描述

4、新建 HelloService (同上一篇 Ribbon 中的Service 不过新增了一个 fallback 方法 “ helloback ”)

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "helloback")
    public String helloService(String name){
        return restTemplate.getForObject("http://eureka-client/" +
                "hello?name="+name,String.class);

    }

    public String helloback(String name){
        return "hello, "+name+" , sorry , hystrix error back!";
    }
}

5、新建 HelloController(同Ribbon中的HelloControll)

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping(value="/hello")
    public String hello(){
        return helloService.helloService("cc");
    }

}

6、properties.yml 文件:


server:
   port: 4444

spring:
  application:
    name: client-ribbon-hystrix

eureka:
  client:
    service-url:
        defaultZone: http://peer1:1122/eureka/
#      这里写一个都行,因为 Eureka Server 是相互注册,高可用注册中心

7、启动两个 Eureka Server(高可用相互注册中心)、两个eureka client 客户端作为服务调用的测试服务器、再启动 Hystrix:
这里写图片描述
这里写图片描述这里写图片描述
当把2233这个端口服务器暂停时:轮询到2233端口就直接返回了报错信息,不会一直在等待或者不被访问
这里写图片描述
这里写图片描述

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

断路器监控:Hystrix 仪表盘

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
以上面的例子为例,继续:
1、在 pom.xml 文件中添加以下依赖:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

2、在应用程序启动类加上 @EnableHystrixDashBoard 注解,启用Hystrix Dashboard 功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值