如何在 Spring Cloud Netflix Zuul 上配置简单的速率限制?
How to configure simple rate limiting on Spring Cloud Netflix Zuul?
我的服务前面有一个简单的 Spring Cloud Netflix Zuul。我想为到达此 Zuul 的所有请求设置 速率限制。
我看过这个post:https://www.baeldung.com/spring-cloud-zuul-rate-limit
但是,我的 Zuul 或 JPA 存储库中都没有控制器。 Zuul 从 Eureka 收到的所有路由。
祖尔:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {
public static void main(String[] args) {
SpringApplication.run(BlitzZuulApplication.class, args);
}
}
application.properties:
spring.application.name=zuul
server.port = 7125
my.eureka.port=7126
eureka.client.service-url.defaultZone=http://localhost:${my.eureka.port}/eureka
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>
如何配置 Zuul 以进行速率限制并限制整体传入请求的数量?
我使用以下 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
此时属性zuul.ratelimit.repository应该不为空。如果您错过了,可以列出一些选项。
我开始使用 JPA 存储库。为此,应在项目中添加 spring-boot-starter-data-jpa 依赖项,并照常配置 datasource 。
如果您现在开始一个项目,您将得到这个异常:
java.sql.SQLSyntaxErrorException: Table 'rate' doesn't
exist
在此源中,您可以在 config 文件夹中找到 Rate.java class,其结构如下:
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#
所以 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
}
使用此配置并创建 table 一切正常,Zuul 将有关请求的信息保存在 table 中。在我的例子中,60 秒内允许 2 个请求。
我的服务前面有一个简单的 Spring Cloud Netflix Zuul。我想为到达此 Zuul 的所有请求设置 速率限制。
我看过这个post:https://www.baeldung.com/spring-cloud-zuul-rate-limit 但是,我的 Zuul 或 JPA 存储库中都没有控制器。 Zuul 从 Eureka 收到的所有路由。
祖尔:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {
public static void main(String[] args) {
SpringApplication.run(BlitzZuulApplication.class, args);
}
}
application.properties:
spring.application.name=zuul
server.port = 7125
my.eureka.port=7126eureka.client.service-url.defaultZone=http://localhost:${my.eureka.port}/eureka
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>
如何配置 Zuul 以进行速率限制并限制整体传入请求的数量?
我使用以下 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
此时属性zuul.ratelimit.repository应该不为空。如果您错过了,可以列出一些选项。
我开始使用 JPA 存储库。为此,应在项目中添加 spring-boot-starter-data-jpa 依赖项,并照常配置 datasource 。
如果您现在开始一个项目,您将得到这个异常:
java.sql.SQLSyntaxErrorException: Table 'rate' doesn't exist
在此源中,您可以在 config 文件夹中找到 Rate.java class,其结构如下: 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#
所以 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
}
使用此配置并创建 table 一切正常,Zuul 将有关请求的信息保存在 table 中。在我的例子中,60 秒内允许 2 个请求。