Skip to content

Commit

Permalink
desc:新增SpringCloudGateway工作原理与链路图文章
Browse files Browse the repository at this point in the history
  • Loading branch information
123yhm committed Mar 5, 2024
2 parents 07b17e8 + 69ea812 commit 3e147f4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
11 changes: 7 additions & 4 deletions .idea/Java-Interview-Tutorial.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ module.exports = {
text: 'Spring Cloud',
items: [
{text: 'SpringCloudAlibaba介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba介绍.md'},
{text: 'SpringCloudAlibaba技术栈介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba技术栈介绍.md'},
{text: '第二代SpringCloudAlibaba主流时代', link: '/md/spring/spring-cloud/第二代SpringCloudAlibaba主流时代.md'},
{text: 'SpringCloudAlibaba版本介绍', link: '/md/spring/spring-cloud/SpringCloudAlibaba版本介绍.md'}
{text: 'SpringCloudGateway工作原理与链路图', link: '/md/spring/spring-cloud/SpringCloudGateway工作原理与链路图.md'}
]
},
{
Expand Down Expand Up @@ -317,9 +315,7 @@ module.exports = {
sidebarDepth: 0,
children: [
"SpringCloudAlibaba介绍.md",
"SpringCloudAlibaba技术栈介绍.md",
"第二代SpringCloudAlibaba主流时代.md",
"SpringCloudAlibaba版本介绍.md"
"SpringCloudGateway工作原理与链路图.md"
]
}
],
Expand Down
1 change: 0 additions & 1 deletion docs/md/spring/spring-cloud/SpringCloudAlibaba介绍.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SpringCloudAlibaba介绍

> 大家好,我叫阿明。下面我会为大家准备Spring Cloud Alibaba系列知识体系,结合实战输出案列,让大家一眼就能明白得技术原理,应用于各公司得各种项目和生活中。让我们得生活越来越美好。
## SpringCloudAlibaba介绍
### Spring Cloud Alibaba 是什么?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# SpringCloudGateway基本介绍

> Spring Cloud Gateway 构建于Spring Boot 2.x、 Spring WebFlux和Project Reactor之上。因此,在使用 Spring Cloud Gateway 时,您可能不会应用许多熟悉的同步库(例如 Spring Data 和 Spring Security)和模式。
> Spring Cloud Gateway 需要 Spring Boot 和 Spring Webflux 提供的 Netty 运行时。它不能在传统的 Servlet 容器中工作,也不能作为 WAR 构建。
> 该项目提供了一个用于在 Spring WebFlux 或 Spring WebMVC 之上构建 API 网关的库。Spring Cloud Gateway 旨在提供一种简单而有效的方法来路由到 API 并为其提供横切关注点,例如:安全性、监控/指标和弹性。
## SpringCloudGateway工作原理
### Spring Cloud网关的特点:
1. 基于 Spring 框架和 Spring Boot 构建
2. 能够匹配任何请求属性上的路由。
3. 谓词和过滤器特定于路由。
4. 断路器集成。
5. Spring Cloud Discovery客户端集成
6. 易于编写谓词和过滤器
7. 请求速率限制
8. 路径重写

### Spring Cloud网关请求链路图
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401375667.png)
> Route:一个 Route 由路由 ID,转发 URI,多个 Predicates 以及多个 Filters 构成。Gateway 上可以配置多个 Routes。处理请求时会按优先级排序,找到第一个满足所有 Predicates 的 Route;
Predicate:表示路由的匹配条件,可以用来匹配请求的各种属性,如请求路径、方法、header 等。一个 Route 可以包含多个子 Predicates,多个子 Predicates 最终会合并成一个;
Filter:过滤器包括了处理请求和响应的逻辑,可以分为 pre 和 post 两个阶段。多个 Filter 在 pre 阶段会按优先级高到低顺序执行,post 阶段则是反向执行。Gateway 包括两类 Filter。
全局 Filter:每种全局 Filter 全局只会有一个实例,会对所有的 Route 都生效。
路由 Filter:路由 Filter 是针对 Route 进行配置的,不同的 Route 可以使用不同的参数,因此会创建不同的实例。

```java
调试Demo局部code
@SpringBootApplication
public class DemogatewayApplication {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/get")
.uri("http://httpbin.org"))
.route("host_route", r -> r.host("*.myhost.org")
.uri("http://httpbin.org"))
.route("rewrite_route", r -> r.host("*.rewrite.org")
.filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
.uri("http://httpbin.org"))
.route("hystrix_route", r -> r.host("*.hystrix.org")
.filters(f -> f.hystrix(c -> c.setName("slowcmd")))
.uri("http://httpbin.org"))
.route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
.filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
.uri("http://httpbin.org"))
.route("limit_route", r -> r
.host("*.limited.org").and().path("/anything/**")
.filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
.uri("http://httpbin.org"))
.build();
}
}
```
## SpringCloudGateway版本介绍
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401373075.png)

## SpringCloudGateway运行结构图
![在这里插入图片描述](http://124.222.54.192:4000/public/upload/2024/03/05/202403051401372859.png)

> 1、客户端向 Spring Cloud Gateway 发出请求。
> 2、如果网关处理程序映射确定请求与路由匹配,则会将其发送到网关 Web 处理程序。
> 3、此处理程序运行通过特定于请求的过滤器链发送请求。
> 4、过滤器被虚线分开的原因是过滤器可能在发送代理请求之前或之后执行逻辑。
> 5、执行所有“预”过滤器逻辑,然后发出代理请求。
> 6、发出代理请求后,执行“post”过滤器逻辑。
*在没有端口的路由中定义的 URI 将分别为 HTTP 和 HTTPS URI 设置为 80 和 443 的默认端口。*

0 comments on commit 3e147f4

Please sign in to comment.