Skip to content

Commit 3e147f4

Browse files
committed
desc:新增SpringCloudGateway工作原理与链路图文章
2 parents 07b17e8 + 69ea812 commit 3e147f4

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

Diff for: .idea/Java-Interview-Tutorial.iml

+7-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/.vuepress/config.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ module.exports = {
151151
text: 'Spring Cloud',
152152
items: [
153153
{text: 'SpringCloudAlibaba介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba介绍.md'},
154-
{text: 'SpringCloudAlibaba技术栈介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba技术栈介绍.md'},
155-
{text: '第二代SpringCloudAlibaba主流时代', link: '/md/spring/spring-cloud/第二代SpringCloudAlibaba主流时代.md'},
156-
{text: 'SpringCloudAlibaba版本介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba版本介绍.md'}
154+
{text: 'SpringCloudGateway工作原理与链路图', link: '/md/spring/spring-cloud/SpringCloudGateway工作原理与链路图.md'}
157155
]
158156
},
159157
{
@@ -317,9 +315,7 @@ module.exports = {
317315
sidebarDepth: 0,
318316
children: [
319317
"SpringCloudAlibaba介绍.md",
320-
"SpringCloudAlibaba技术栈介绍.md",
321-
"第二代SpringCloudAlibaba主流时代.md",
322-
"SpringCloudAlibaba版本介绍.md"
318+
"SpringCloudGateway工作原理与链路图.md"
323319
]
324320
}
325321
],

Diff for: docs/md/spring/spring-cloud/SpringCloudAlibaba介绍.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# SpringCloudAlibaba介绍
2-
32
> 大家好,我叫阿明。下面我会为大家准备Spring Cloud Alibaba系列知识体系,结合实战输出案列,让大家一眼就能明白得技术原理,应用于各公司得各种项目和生活中。让我们得生活越来越美好。
43
## SpringCloudAlibaba介绍
54
### Spring Cloud Alibaba 是什么?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SpringCloudGateway基本介绍
2+
3+
> Spring Cloud Gateway 构建于Spring Boot 2.x、 Spring WebFlux和Project Reactor之上。因此,在使用 Spring Cloud Gateway 时,您可能不会应用许多熟悉的同步库(例如 Spring Data 和 Spring Security)和模式。
4+
> Spring Cloud Gateway 需要 Spring Boot 和 Spring Webflux 提供的 Netty 运行时。它不能在传统的 Servlet 容器中工作,也不能作为 WAR 构建。
5+
> 该项目提供了一个用于在 Spring WebFlux 或 Spring WebMVC 之上构建 API 网关的库。Spring Cloud Gateway 旨在提供一种简单而有效的方法来路由到 API 并为其提供横切关注点,例如:安全性、监控/指标和弹性。
6+
7+
## SpringCloudGateway工作原理
8+
### Spring Cloud网关的特点:
9+
1. 基于 Spring 框架和 Spring Boot 构建
10+
2. 能够匹配任何请求属性上的路由。
11+
3. 谓词和过滤器特定于路由。
12+
4. 断路器集成。
13+
5. Spring Cloud Discovery客户端集成
14+
6. 易于编写谓词和过滤器
15+
7. 请求速率限制
16+
8. 路径重写
17+
18+
### Spring Cloud网关请求链路图
19+
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401375667.png)
20+
> Route:一个 Route 由路由 ID,转发 URI,多个 Predicates 以及多个 Filters 构成。Gateway 上可以配置多个 Routes。处理请求时会按优先级排序,找到第一个满足所有 Predicates 的 Route;
21+
Predicate:表示路由的匹配条件,可以用来匹配请求的各种属性,如请求路径、方法、header 等。一个 Route 可以包含多个子 Predicates,多个子 Predicates 最终会合并成一个;
22+
Filter:过滤器包括了处理请求和响应的逻辑,可以分为 pre 和 post 两个阶段。多个 Filter 在 pre 阶段会按优先级高到低顺序执行,post 阶段则是反向执行。Gateway 包括两类 Filter。
23+
全局 Filter:每种全局 Filter 全局只会有一个实例,会对所有的 Route 都生效。
24+
路由 Filter:路由 Filter 是针对 Route 进行配置的,不同的 Route 可以使用不同的参数,因此会创建不同的实例。
25+
26+
```java
27+
调试Demo局部code
28+
@SpringBootApplication
29+
public class DemogatewayApplication {
30+
@Bean
31+
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
32+
return builder.routes()
33+
.route("path_route", r -> r.path("/get")
34+
.uri("http://httpbin.org"))
35+
.route("host_route", r -> r.host("*.myhost.org")
36+
.uri("http://httpbin.org"))
37+
.route("rewrite_route", r -> r.host("*.rewrite.org")
38+
.filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
39+
.uri("http://httpbin.org"))
40+
.route("hystrix_route", r -> r.host("*.hystrix.org")
41+
.filters(f -> f.hystrix(c -> c.setName("slowcmd")))
42+
.uri("http://httpbin.org"))
43+
.route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
44+
.filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
45+
.uri("http://httpbin.org"))
46+
.route("limit_route", r -> r
47+
.host("*.limited.org").and().path("/anything/**")
48+
.filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
49+
.uri("http://httpbin.org"))
50+
.build();
51+
}
52+
}
53+
```
54+
## SpringCloudGateway版本介绍
55+
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401373075.png)
56+
57+
## SpringCloudGateway运行结构图
58+
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401372859.png)
59+
60+
> 1、客户端向 Spring Cloud Gateway 发出请求。
61+
> 2、如果网关处理程序映射确定请求与路由匹配,则会将其发送到网关 Web 处理程序。
62+
> 3、此处理程序运行通过特定于请求的过滤器链发送请求。
63+
> 4、过滤器被虚线分开的原因是过滤器可能在发送代理请求之前或之后执行逻辑。
64+
> 5、执行所有“预”过滤器逻辑,然后发出代理请求。
65+
> 6、发出代理请求后,执行“post”过滤器逻辑。
66+
67+
*在没有端口的路由中定义的 URI 将分别为 HTTP 和 HTTPS URI 设置为 80 和 443 的默认端口。*

0 commit comments

Comments
 (0)