Java 如何在Spring云Netflix Zuul上配置简单的速率限制?
我的服务面前有一个简单的Spring云Netflix Zuul。我想为所有发送到此Zuul的请求设置速率限制 我看过这个帖子: 但是,我的Zuul或JPA存储库中都没有控制器。Zuul从尤里卡收到的所有路线 Zuul:Java 如何在Spring云Netflix Zuul上配置简单的速率限制?,java,spring,spring-boot,netflix-zuul,Java,Spring,Spring Boot,Netflix Zuul,我的服务面前有一个简单的Spring云Netflix Zuul。我想为所有发送到此Zuul的请求设置速率限制 我看过这个帖子: 但是,我的Zuul或JPA存储库中都没有控制器。Zuul从尤里卡收到的所有路线 Zuul: @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class BlitzZuulApplication { public static void main(String[] args)
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {
public static void main(String[] args) {
SpringApplication.run(BlitzZuulApplication.class, args);
}
}
<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>
应用程序属性:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {
public static void main(String[] args) {
SpringApplication.run(BlitzZuulApplication.class, args);
}
}
<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>
spring.application.name=zuulserver.port=7125
my.eureka.port=7126 eureka.client.service url.defaultZone=:${my.eureka.port}/eureka pom.xml:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class BlitzZuulApplication {
public static void main(String[] args) {
SpringApplication.run(BlitzZuulApplication.class, args);
}
}
<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>
org.springframework.cloud
spring cloud starter netflix eureka客户端
org.springframework.cloud
春季云入门netflix zuul
com.marcosbarbero.cloud
春云尊率限核
2.2.4.1发布
如何配置Zuul以限制速率并限制总传入请求的数量?我使用以下应用程序.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存储库。对于这个SpringBootStarter数据,应该在项目中添加jpa依赖项,并且应该像往常一样配置数据源
如果您现在开始一个项目,您将得到以下异常:
java.sql.SQLSyntaxErrorException:表“rate”没有
存在
在此源代码中,您可以在配置文件夹中找到Rate.java类,其结构如下:
因此,比率实体为:
@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
}
通过这种配置和创建表,Zuul可以将有关请求的信息保存在表中。在我的情况下,60秒内允许2个请求。我使用以下应用程序.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存储库。对于这个SpringBootStarter数据,应该在项目中添加jpa依赖项,并且应该像往常一样配置数据源
如果您现在开始一个项目,您将得到以下异常:
java.sql.SQLSyntaxErrorException:表“rate”没有
存在
在此源代码中,您可以在配置文件夹中找到Rate.java类,其结构如下:
因此,比率实体为:
@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
}
通过这种配置和创建表,Zuul可以将有关请求的信息保存在表中。在我的例子中,60秒内允许2个请求。是否有任何可用信息/基准显示这如何影响请求吞吐量?是否有任何可用信息/基准显示这如何影响请求吞吐量?