springcloud的网关(四)gateway之限流熔断和降级

一:网管服务的网关策略

在上述中我们说明了网关的断言,过滤等网管服务的基本的机制。因此,我们整合了上述机制来讨论网关服务中的限流熔断和降级等网关策略。

 

二:熔断降级

  1.业务场景:在传统的项目中,当访问一个路径出现不可访问比如404,浏览器不同在加载甚至在10几秒后才会反馈出404,这明

                         显对用户不友好,如果用户在加载404等还在不停的刷新当前网页将会导致后台不同的请求。这就需要我们做熔断降

                         级。

 

  2.技术要点:在当前的网关服务中,熔断降级主要是使用hystrix来实现服务降级,当访问的内部url超过限定的时间救出自动出现。

  3.代码说明:

yml:中的配置:

server:
  port: 9999
spring:
  profiles:
    active: dev
  application:
    name: gateway-service
  redis:
    host: localhost
    port: 6379
    password: 123456
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: http://localhost:8001/customFilter
        predicates:
        - Path=/test
        filters:
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/defaultfall
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
一、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/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值