[英]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 = 7126eureka.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.