SpringCloud - Zuul(四)

简介: SpringCloud - Zuul(四)

image.png

package com.imooc.apigateway.filter;
import com.google.common.util.concurrent.RateLimiter;
import com.imooc.apigateway.exception.RateLimitException;
import com.netflix.zuul.ZuulFilter;
import org.springframework.stereotype.Component;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVLET_DETECTION_FILTER_ORDER;
/**
 * 限流
 */
@Component
public class RateLimitFilter extends ZuulFilter{
  // 谷歌提供的guava组件实现令牌桶的场景,每秒钟往里面放100个令牌。
  private static final RateLimiter RATE_LIMITER = RateLimiter.create(100);
  /**
   * to classify a filter by type. Standard types in Zuul are "pre" for pre-routing filtering,
   * "route" for routing to an origin, "post" for post-routing filters, "error" for error handling.
   * We also support a "static" type for static responses see  StaticResponseFilter.
   * Any filterType made be created or added and run by calling FilterProcessor.runFilters(type)
   *
   * @return A String representing that type
   */
  @Override
  public String filterType() {
    return PRE_TYPE;
  }
  /**
   * filterOrder() must also be defined for a filter. Filters may have the same  filterOrder if precedence is not
   * important for a filter. filterOrders do not need to be sequential.
   *
   * @return the int order of a filter
   */
  @Override
  public int filterOrder() {
    return SERVLET_DETECTION_FILTER_ORDER - 1;
  }
  /**
   * a "true" return from this method means that the run() method should be invoked
   *
   * @return true if the run() method should be invoked. false will not invoke the run() method
   */
  @Override
  public boolean shouldFilter() {
    return true;
  }
  /**
   * if shouldFilter() is true, this method will be invoked. this method is the core method of a ZuulFilter
   *
   * @return Some arbitrary artifact may be returned. Current implementation ignores it.
   */
  @Override
  public Object run() {
    if (!RATE_LIMITER.tryAcquire()) {
      throw new RateLimitException();
    }
    return null;
  }
}
目录
相关文章
|
1月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
37 6
|
1月前
|
负载均衡 Java 开发者
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
144 5
|
1月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
59 5
|
1月前
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
43 5
|
4月前
|
负载均衡 Java 网络架构
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
200 5
|
3月前
|
负载均衡 Java API
【Spring Cloud生态】Spring Cloud Gateway基本配置
【Spring Cloud生态】Spring Cloud Gateway基本配置
68 0
|
5月前
|
Java Spring
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
|
6月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
650 15
|
6月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
137 3
|
5月前
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM