Spring Cloud Tutorial - 4

In this series of previous article, we implemented a Zuul-based Gateway and accessed the todo microservice through the Gateway. In this article, we will demonstrate how to implement a simple flow restriction function in Gateway.

Increase dependency library

In the (% post_link microservice/spring-cloud/spring-cloud-tutorial-03_en previous article %) the completed Gateway project adds the following dependency libraries:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Because the integrated Netflix Zuul provided by Spring Cloud does not provide a simple configuration method to complete the current limiting function, we use a third-party library here: spring-cloud-zuul-ratelimit Official website, this library implements current limiting in multiple ways.

Modify the configuration file

Modify the original application.xml file to increase the current limiting function. In the example, we simply restrict access to 3 within 10 seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
zuul:
prefix: /api
routes:
todo-by-service:
path: /todo/*
serviceId: cloud-todo-service
ratelimit:
enabled: true
repository: JPA
policy-list:
todo-by-service:
- limit: 3
refresh-interval: 30
type:
- origin

Comparing the original application.yaml file, you can see that only the ratelimit content is added in the zuul configuration section. Note: Each configuration item in the policy-list needs to correspond to the route name configured in the routes.

Run and verify

Run the project, then open it in the browser:

1
http://localhost:9098/api/todo/todo

After seeing the correct result for the first time, quickly refresh the browser and you can see that if there are more than three times within 30 seconds, the page returns an error message as follows:

1
2
3
4
5
6
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Nov 02 16:42:47 CST 2019
There was an unexpected error (type=Too Many Requests, status=429).
429

You can also see ZuulException thrown in the background

1
2
3
4
5
6
o.s.c.n.z.filters.post.SendErrorFilter   : Error during filtering

com.netflix.zuul.exception.ZuulException: 429
at com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitExceededException.<init>(RateLimitExceededException.java:13) ~[spring-cloud-zuul-ratelimit-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.filters.RateLimitPreFilter.lambda$run$0(RateLimitPreFilter.java:106) ~[spring-cloud-zuul-ratelimit-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_191]

本文标题:Spring Cloud Tutorial - 4

文章作者:Morning Star

发布时间:2021年07月18日 - 16:07

最后更新:2021年07月22日 - 06:07

原始链接:https://www.mls-tech.info/microservice/spring-cloud/spring-cloud-tutorial-04_en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。