详细说明spring cloud hystrix的限流,熔断,降级并给出代码和配置示例。详细说明spring cloud gateway熔断,限流和降级并给出代码和示例
时间: 2023-05-24 16:06:53 浏览: 261
一、Spring Cloud Hystrix
1. 什么是 Hystrix
Hystrix 是一个延迟和容错库,用于隔离依赖服务的访问点,以防止这些依赖服务的故障导致雪崩效应。它提供了熔断、限流和降级等机制,保障了对依赖服务的访问。
2. Hystrix的核心概念
熔断:在一段时间内,如果服务的错误比例超过了设定的阈值,那么这个服务就会被熔断,示例代码:
```
@HystrixCommand(fallbackMethod = "fallbackMethod")
public String method() {
int i = new Random().nextInt(10);
if (i % 2 == 0) {
throw new RuntimeException("error");
}
return "success";
}
public String fallbackMethod(){
return "fallback";
}
```
限流:在一段时间内,如果服务的请求数量超过了设定的阈值,那么这个服务就会被限流,示例代码:
```
@HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000")})
public String method() {
return "success";
}
public String fallbackMethod(){
return "fallback";
}
```
降级:在一段时间内,如果依赖服务不可用,那么就会调用预先备选的服务逻辑或返回预先设定的响应,示例代码:
```
@HystrixCommand(fallbackMethod = "fallbackMethod")
public String method() {
return restTemplate.getForObject("http://service-provider/method", String.class);
}
public String fallbackMethod(){
return "fallback";
}
```
3. Hystrix的集成
添加依赖
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
```
开启熔断
```
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
添加注解
```
@Service
public class Service {
@HystrixCommand(fallbackMethod = "fallbackMethod")
public String method() {
return "success";
}
public String fallbackMethod() {
return "fallback";
}
}
```
4. 测试
启动服务后,访问 http://localhost:8080/method,可以看到服务正常返回"success"。将calledOnce设置为false并再次访问该URL,可以看到服务返回"fallback"。
二、Spring Cloud Gateway
1. 什么是 Gateway
Gateway 是 Spring Cloud 提供的一种 API 网关服务,基于 Spring 5.0、Spring Boot 2.0 和 Project Reactor 等技术开发。
2. Gateway 的三种机制
熔断:默认使用 Hystrix 进行熔断,示例代码:
```
spring.cloud.gateway.routes.id=route1
spring.cloud.gateway.routes.uri=http://httpbin.org:80
spring.cloud.gateway.routes.predicate[0]=Host=**.somehost.org
spring.cloud.gateway.routes.predicate[1]=Path=/get
spring.cloud.gateway.routes.filters[0]=Hystrix=hystrixCommandName
```
限流:使用 RequestRateLimiter Gateway Filter 进行限流,示例代码:
```
spring.cloud.gateway.routes[0].id=count_route
spring.cloud.gateway.routes[0].uri=http://httpbin.org:80
spring.cloud.gateway.routes[0].predicates[0]=Path=/get
spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=my-limiter-key,2,10,PT1S
```
降级:可以使用 Hystrix 进行降级,示例代码:
```
spring.cloud.gateway.routes[0].id=test_route
spring.cloud.gateway.routes[0].uri=http://localhost:9090
spring.cloud.gateway.routes[0].predicates[0]=Path=/test
spring.cloud.gateway.routes[0].filters[0]=Hystrix=mycommand
```
3. Gateway 的集成
添加依赖
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
```
4. 测试
启动服务后,访问 http://localhost:8080/get,可以看到服务正常返回。将请求频率配置为 0.1s,再次访问该 URL,可以看到服务返回 429 Too Many Requests。
参考资料:
1. Spring Cloud Hystrix 官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#spring-cloud-hystrix
2. Spring Cloud Gateway 官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
阅读全文