Spring Cloud Hystrix:服务容错保护

简介: Spring Cloud Hystrix:服务容错保护


Spring Cloud Hystrix:服务容错保护

Spring Cloud Hystrix是Spring Cloud中的一个子项目,主要用于服务容错保护;分布式系统中,一个服务的故障或延迟可能会导致整个系统出现问题,为提高系统的稳定性和可用性,Hystrix提供了一种机制来防止这种故障扩散并提供适当的备选方案

Hystrix 简介

Hystrix是Netflix开源的一个用于处理分布式系统延迟和容错的库;主要用于在分布式系统中防止级联故障,提高系统的弹性和稳定性

创建hystrix-service模块

  • 依赖导入
<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-web</artifactId>
</dependency>
  • 配置application.yml
server:
  port: 8401
spring:
  application:
    name: hystrix-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
service-url:
  user-service: http://user-service
  • 启动类添加@EnableCircuitBreaker开启Hystrix的断路器功能
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixServiceApplication.class, args);
    }
  • 创建UserHystrixController接口用于调用user-service服务
  • 服务降级演示
@GetMapping("/testFallback/{id}")
public CommonResult testFallback(@PathVariable Long id) {
    return userService.getUser(id);
}
  • UserService中添加调用方法与服务降级方法,方法上需要添加@HystrixCommand注解
@HystrixCommand(fallbackMethod = "getDefaultUser")
public CommonResult getUser(Long id) {
    return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
}
public CommonResult getDefaultUser(@PathVariable Long id) {
    User defaultUser = new User(-1L, "defaultUser", "123456");
    return new CommonResult<>(defaultUser);
}

@HystrixCommand

@HystrixCommand是Hystrix中的一个注解,用于将Hystrix的断路器功能与具体的方法进行集成;当在方法上添加@HystrixCommand注解时,该方法的执行将由Hystrix进行包装和管理,以提供容错保护和服务降级的能力

  • fallbackMethod:指定服务降级处理方法
  • ignoreExceptions:忽略某些异常,不发生服务降级
  • commandKey:命令名称,用于区分不同的命令
  • groupKey:分组名称,Hystrix会根据不同的分组来统计命令的告警及仪表盘信息
  • threadPoolKey:线程池名称,用于划分线程池

Hystrix 单个实例监控

Hystrix中,可以通过Hystrix Dashboard来监控单个实例的运行情况;Hystrix Dashboard提供可视化的界面,可以实时监控和查看Hystrix命令的执行情况、断路器的状态、请求的成功和失败等信息

创建hystrix-dashboard模块
  • 依赖导入
<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-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 配置application.yml
server:
  port: 8501
spring:
  application:
    name: hystrix-dashboard
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • 启动类上添加@EnableHystrixDashboard来启用监控功能
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }
}

Hystrix 集群实例监控

Hystrix集群实例监控,使用Netflix Turbine来聚合多个实例的Hystrix数据,并提供集群级别的监控;Netflix Turbine是一个用于监控Hystrix Stream数据的工具,它允许您在分布式系统中查看多个服务实例的断路器状态和指标

创建turbine-service模块
  • 依赖导入
<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-turbine</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 配置application.yml,添加Turbine相关配置
server:
  port: 8601
spring:
  application:
    name: turbine-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
turbine:
  app-config: hystrix-service #指定需要收集信息的服务名称
  cluster-name-expression: new String('default') #指定服务所属集群
  combine-host-port: true #以主机名和端口号来区分服务
  • 启动类上添加@EnableTurbine来启用Turbine相关功能
@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(TurbineServiceApplication.class, args);
    }
}

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


目录
相关文章
|
1月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
22天前
|
Java Spring
Spring boot 运行服务jar外配置配置文件方式总结
Spring boot 运行服务jar外配置配置文件方式总结
134 0
|
22天前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
140 15
|
28天前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14204 19
|
22天前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
41 3
|
1月前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
119 5
|
1月前
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成
|
26天前
|
消息中间件 Java Nacos
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
48 0
|
26天前
|
缓存 监控 Java
通用快照方案问题之Spring Boot Admin的定义如何解决
通用快照方案问题之Spring Boot Admin的定义如何解决
34 0