简体   繁体   English

如何在Spring Cloud Netflix Zuul上配置简单的速率限制?

[英]How to configure simple rate limiting on Spring Cloud Netflix Zuul?

I have a simple Spring Cloud Netflix Zuul in front of my services. 我在服务之前有一个简单的Spring Cloud Netflix Zuul。 I would like to set rate limiting for all requests coming to this Zuul. 我想为来到Zuul的所有请求设置速率限制

I have seen this post: https://www.baeldung.com/spring-cloud-zuul-rate-limit However I have neither controllers in my Zuul or JPA repository. 我看过这篇文章: https : //www.baeldung.com/spring-cloud-zuul-rate-limit但是,我的Zuul或JPA存储库中都没有控制器。 All routes Zuul receives from Eureka. Zuul从尤里卡(Eureka)接收的所有路线。

Zuul : Zuul

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlitzZuulApplication.class, args);
    }

}

application.properties : application.properties

spring.application.name=zuul spring.application.name =祖尔
server.port = 7125 server.port = 7125
my.eureka.port=7126 my.eureka.port = 7126

eureka.client.service-url.defaultZone= http://localhost :${my.eureka.port}/eureka eureka.client.service-url.defaultZone = http:// localhost :$ {my.eureka.port} / eureka

pom.xml : pom.xml

    <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-zuul</artifactId>
    </dependency>
    <dependency>
        <groupId>com.marcosbarbero.cloud</groupId>
        <artifactId>spring-cloud-zuul-ratelimit-core</artifactId>
        <version>2.2.4.RELEASE</version>
    </dependency>

How to configure Zuul for rate limiting and restrict the number of overall incoming requests? 如何配置Zuul进行速率限制并限制整体传入请求的数量?

I use the following application.yaml file: 我使用以下application.yaml文件:

zuul:
  routes:
    my-service:
      path: /
  ratelimit:
    enabled: true
    repository: JPA
    policy-list:
      my-service:
        - limit: 2
          refresh-interval: 60
          type:
            - origin
  strip-prefix: true

At this point property zuul.ratelimit.repository should be not empty. 此时属性zuul.ratelimit.repository应该不为空。 There is some options listed if you miss it. 如果您错过了其中列出的一些选项。

I start using JPA repository. 我开始使用JPA存储库。 For this spring-boot-starter-data-jpa dependency should be added in a project and datasource should be configured as usual. 为此,应该在项目中添加依赖项spring-boot-starter-data-jpa ,并且应该像往常一样配置数据源

If you now start a project you will get this Exception: 如果现在启动一个项目,则将出现以下异常:

java.sql.SQLSyntaxErrorException: Table 'rate' doesn't exist java.sql.SQLSyntaxErrorException:表'rate'不存在

In this source you can find Rate.java class in config folder with a structure: https://www.programcreek.com/java-api-examples/?code=marcosbarbero/spring-cloud-zuul-ratelimit/spring-cloud-zuul-ratelimit-master/spring-cloud-zuul-ratelimit-core/src/main/java/com/marcosbarbero/cloud/autoconfigure/zuul/ratelimit/RateLimitAutoConfiguration.java# 在此源代码中,您可以在config文件夹中找到具有以下结构的Rate.java类: https : //www.programcreek.com/java-api-examples/?code=marcosbarbero/spring-cloud-zuul-ratelimit/spring-cloud- zuul-ratelimit-master / spring-cloud-zuul-ratelimit-core / src / main / java / com / marcosbarbero / cloud / autoconfigure / zuul / ratelimit / RateLimitAutoConfiguration.java#

So Rate entity is: 因此, Rate实体为:

@Entity
public class Rate {

    @Id
    private String key;
    private Long remaining;
    private Long remainingQuota;
    private Long reset;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
    private Date expiration;

    // constructor, getters and setters 
}

With this configuration and creating the table all works fine, Zuul saves information about request in the table. 通过这种配置和创建表,一切正常,Zuul将有关请求的信息保存在表中。 In my case 2 requests allowed in 60 seconds. 以我为例,在60秒内允许2个请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring Cloud Netflix Zuul 作为 SSL 反向代理 - How to use Spring Cloud Netflix Zuul as SSL reverse proxy Spring Cloud Netflix Zuul。 SQLSyntaxErrorException:表&#39;rate&#39;不存在 - Spring Cloud Netflix Zuul. SQLSyntaxErrorException: Table 'rate' doesn't exist spring云网关如何实现Redis限速? - How to implement the Redis Rate Limiting in spring cloud gateway? Spring Boot2 和 Netflix Zuul - Spring Boot2 and Netflix Zuul Spring Cloud-如何在Zuul反向代理上配置OAuth2RestTemplate来传递授权令牌? - Spring Cloud - How to configure OAuth2RestTemplate on Zuul reverse proxy to pass authorization tokens through? 如何在Spring Cloud Zuul中配置路由 - How to config routes in Spring Cloud Zuul Spring 云 - Redis 速率限制 - 不适用于每分钟的请求 - Spring Cloud - Redis Rate Limiting - Not working for request per minute 如何在一个 Spring Boot 应用程序中的不同端口上运行 api-gateway(Netflix Zuul) 和 Eureka Server - How to run api-gateway(Netflix Zuul) and Eureka Server on different port in one Spring Boot application NoSuchMethodError: org.springframework.boot.web.servlet.error.ErrorController.getErrorPath() 在最新的 spring-cloud-starter.netflix-zuul - NoSuchMethodError: org.springframework.boot.web.servlet.error.ErrorController.getErrorPath() in latest spring-cloud-starter-netflix-zuul package org.springframework.cloud.netflix.zuul 不存在 - package org.springframework.cloud.netflix.zuul does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM