Hystrix是Netflix开源的一个针对分布式系统容错处理的组件,Netflix公司的项目里大量用到了Hystrix,Hystrix单词意为:“豪猪”,浑身有刺来保护自己。
Hystrix是一个延迟和容错库,旨在隔离远程系统、服务和第三方库,阻止级联故障,在复杂的分布式系统中实现恢复能力。
设计目的
解决的问题
分布式系统环境下,服务间类似依赖非常常见,一个业务调用通常依赖多个基础服务。如下图,对于同步调用,当库存服务不可用时,商品服务请求线程被阻塞,当有大批量请求调用库存服务时,最终可能导致整个商品服务资源耗尽,无法继续对外提供服务。并且这种不可用可能沿请求调用链向上传递,这种现象被称为雪崩效应。
在这种服务之间依赖或者系统之间的依赖的情况下,需要一种机制来处理延迟和故障,并保护整个系统处于可用稳定的状态下,这就轮到Hystrix登场了。
创建工程cloud-hystrix-practice
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.3.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring.cloud-version>Hoxton.SR3spring.cloud-version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud-version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
创建注册中心
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
创建模块cloud-order-service
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
配置文件
server:
port: 8888
spring:
application:
name: cloud-order-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
控制层代码
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private IOrderService orderService;
@GetMapping("getOrder")
public String getOrder(@RequestParam String name) {
return orderService.getOrder(name);
}
}
服务层代码
public interface IOrderService {
String getOrder(String name);
}
@Service
public class OrderServiceImpl implements IOrderService {
// 服务熔断:开启熔断-10秒内-10次请求-60%的请求失败-触发熔断
@HystrixCommand(fallbackMethod = "defaultFallBack",
commandProperties = {
//是否开启断路器
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
//请求数达到后才计算
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
//休眠时间窗
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
//错误率达到多少跳闸,熔断
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
})
@Override
public String getOrder(String name) {
if ("hystrix".equalsIgnoreCase(name)) {
return "正确访问";
} else {
throw new RuntimeException("错误访问");
}
}
public String defaultFallBack(String name) {
return "this is defaultFallBack method!";
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
// 开启Hystrix
@EnableHystrix
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
启动注册中心和cloud-order-service
在访问http://localhost:8888/order/getOrder?name=hystrix时,参数检验正确返回正常。当传入的参数不是代码中要求的时候,发生异常,自动调用defaultFallBack方法,返回友好提示。
至此Hystrix的入门使用就是这么简单
1、@EnableHystrix
开启Hystrix断路器
2、@HystrixCommand(fallbackMethod = "defaultFallBack")
定义fallback的方法
可关注我的微信公众号