为什么使用gateway

为什么使用gateway

1,引言

​ 网关api:封装了系统内部架构,为每个客户端提供一个定制的 API。在微服务架构中,服务网关的核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。

​ 服务网关在完成客户端与服务器端报文格式转换的同时,它可能还具有身份验证、监控、缓存、请求管理、静态响应处理等功能。另一方面,也可以在网关层制定灵活的路由策略。针对一些特定的 API,我们需要设置白名单、路由规则等各类限制。

​ 如果没有网关的话,那么无论什么样的请求,比如恶意请求,无效的请求,都会打到后端,很容易造成服务器的宕机。

​ 由此可见,gateway的出现,直接把非业务层面的处理放到亲啊短来,大大缓解服务器的压力。同时也保障了服务器的安全。

2,Spring Cloud Gateway概览

​ 周所周知,学习一门新技术的最好方法就是去看官方文档的描述,就像高中时学语文,数学一样,学新知识点的时候,老师肯定会说,看看教材,而不是看教辅书。学技术也一样,虽然官方都是英文,确实对我这个萌新来说,可能会有些困难,但慢慢看,总会适应的。而是必须要适应,因为技术的更新总是瞬息的,每当一个新技术的出现,肯定会只有官方那一份资料,如果不养成看官方文档的习惯,学新技术肯定会有些困难的(萌新个人观点哈)

高中时,拿到教材的第一件事,就是去看目录。

​ 让我们看看spring cloud gateway的目录写了啥,也好对以后学习的内容有个大概。

1,How to Include Spring Cloud Gateway(如何在项目中包含spring cloud gateway)

让我猜猜,这个章节会讲什么,毫无疑问,肯定是讲怎么把gateway加入自己的项目中。

来看看官方原文。

/*
To include Spring Cloud Gateway in your project, use the starter with a group ID of org.springframework.
cloud and an artifact ID of spring-cloud-starter-gateway. 
See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

If you include the starter, but you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.*/

我来用我那蹩脚的英语翻译翻译

'''
为了让gateway1加入到你的项目中,
use the starter with a group ID of org.springframework.
    cloud and an artifact ID of spring-cloud-starter-gateway.
    说白了就是引依赖。去spring coloud project page查阅更多信息
If you include the starter, 
but you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.
'''

官方原文

'''

Spring Cloud Gateway is built on Spring Boot 2.x, Spring WebFlux,
and Project Reactor.
 As a consequence,
many of the familiar synchronous libraries (Spring Data and Spring Security, for example) and patterns you know may not apply when you use Spring Cloud Gateway. 
  If you are unfamiliar with these projects,
we suggest you begin by reading their documentation to familiarize yourself with some new concepts before working with Spring Cloud Gateway.
'''

​ 翻译

'''
Spring Cloud Gateway以Spring Boot 2.x, Spring WebFlux,and Project Reactor为基础.
因此,您所熟悉的许多同步库(例如Spring Data和Spring Security)和模式在使用Spring Cloud Gateway时可能不适用。
如果您不熟悉这些项目,
我们建议您在使用Spring Cloud Gateway之前先阅读它们的文档,熟悉一些新概念。
'''

​ 原文

'''

Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. 
    It does not work in a traditional Servlet Container or when built as a WAR.
'''

翻译

'''
Spring Cloud Gateway需要Spring BootSpring Webflux提供的Netty运行时。
    它不能在传统的Servlet容器中工作,也不能作为WAR构建。
'''

好嘞,第一章愉快的结束了,虽然我也不知道在说啥,感觉就是讲讲怎么把spring cloud gateway引入项目中。

来吧,第二章的学习

2,Glossary(词汇表)

原文

'''
Route: The basic building block of the gateway. 
    It is defined by an ID, a destination URI, 
a collection of predicates, and a collection of filters.
    A route is matched if the aggregate predicate is true.
'''

翻译

'''
Route:网关的基础构建。他是由an ID,a destination URI,a collection of predicates,and a collection of filters构成,
    敲黑板,感觉会是重点,记笔记,记笔记!
    A route is matched if the aggregate predicates is true,
这里感觉还是不要翻译的好,讲的应该是route匹配条件啥的。
'''

原文

'''
Predicate: This is a Java 8 Function Predicate. 
    The input type is a Spring Framework ServerWebExchange. 
    This lets you match on anything from the HTTP request,
such as headers or parameters.
'''

翻译

'''
Predicate:这是java8的新特性。(很陌生吧,我也是,我进去看了下,应该是个接口)。
    接口的输入类型为Spring Framework ServerWebExchange。这你匹配来自http的任何请求,
    比如说请求头或者来自前端传来的参数。
    (感觉很重要哈,感觉做过滤器和处理前端参数请求会用到哈)
'''

原文

'''
Filter: These are instances of GatewayFilter that have been constructed with a specific factory.
    Here, you can modify requests and responses before or after sending the downstream request
'''

翻译

'''
Filter:这些是使用特定工厂构造的GatewayFilter实例。
    在这里,您可以在发送下游请求之前或之后修改请求和响应。
'''

这一章讲的大概是说一些经常用的名词,Route,Predicate,Filter

3,How It Works

gateway架构图

在这里插入图片描述

原文

'''
Clients make requests to Spring Cloud Gateway. 
If the Gateway Handler Mapping determines that a request matches a route,
it is sent to the Gateway Web Handler.
This handler runs the request through a filter chain that is specific to the request. 
The reason the filters are divided by the dotted line is that filters can run logic both before and after the proxy request is sent. 
 All “pre” filter logic is executed. Then the proxy request is made. After the proxy request is made, the “post” filter logic is run.

'''

翻译

'''
	客户端向spring cloud gateway 发动请求。 如果Gateway Handler 匹配请求路径,
    他就会被打到Gateway Web Handler.此处理程序通过特定于请求的过滤器链运行请求。
  用虚线分割的原因是因为该过滤器可以在发送代理请求之前和之后运行逻辑。全部的"pre"filter被执行。然后发出代理请求,等代理请求被发出后,“post”filter开始执行.
'''

原文

'''
URIs defined in routes without a port get default port values 
    of 80 and 443 for the HTTP and HTTPS URIs, respectively.
'''

翻译

'''
在没有端口的路由中定义的uri将分别获得HTTPHTTPS 
    uri的默认端口值80443。
'''

这一章讲的大概是说的是gateway是如何工作的,还有FIlter是怎么处理前端发来的请求的

好了。今天就写这么多吧。其他的等有空的时候再继续写完。争取每天写一篇。也好当作自己的复习笔记啥的。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是梦磊OL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值