💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
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);
}
}
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍收藏
⭐️评论
📝