Spring Cloud Gateway文档翻译

发布时间 2023-08-02 14:21:29作者: 三号小玩家

Spring Cloud Gateway

以下是这个地址的翻译:

https://cloud.spring.io/spring-cloud-gateway/reference/html/#gateway-starter
3.0.0-SNAPSHOT

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

翻译

该项目提供了一个建立在Spring生态系统之上的API网关,包括:Spring 5、Spring Boot 2和project Reactor。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到API,并为它们提供跨领域的关注点,例如:安全性、监控指标和弹性。

1.How to Include Spring Cloud Gateway

如何包含Spring Cloud 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.

翻译

要在您的项目中包括Spring Cloud Gateway,请使用组ID为org.springframework.Cloud和工件ID为Spring Cloud starter Gateway的启动器。有关使用当前Spring Cloud Release Train设置构建系统的详细信息,请参阅Spring Cloud Project页面。如果包含启动器,但不希望启用网关,请将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 of the new concepts before working with Spring Cloud Gateway. 

翻译

Spring Cloud Gateway构建在Spring Boot 2.x、Spring WebFlux和Project Reactor上。因此,当您使用Spring Cloud Gateway时,您所知道的许多熟悉的同步库(例如,Spring Data和Spring Security)和模式可能不适用。如果您不熟悉这些项目,我们建议您在使用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 Boot和Spring Webflux提供的Netty运行时。它不能在传统的Servlet容器中工作,也不能作为WAR构建。

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.

    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.

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


翻译

路线:网关的基本构建块。它由一个ID、一个目标URI、一组谓词和一组筛选器定义。如果聚合谓词为true,则匹配路由。谓词:这是一个Java 8函数谓词。输入类型为Spring Framework ServerWebExchange。这允许您匹配HTTP请求中的任何内容,例如标头或参数。Filter:这些是使用特定工厂构建的Spring Framework GatewayFilter的实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

3.How It Works

它的工作原理


The following diagram provides a high-level overview of how Spring Cloud Gateway works:

翻译

下图提供了Spring Cloud 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发出请求。如果网关处理程序映射确定请求与路由匹配,则会将其发送到网关Web处理程序。此处理程序通过特定于请求的筛选器链来运行请求。过滤器被虚线分隔的原因是过滤器可以在发送代理请求之前和之后运行逻辑。执行所有“预”过滤器逻辑。然后进行代理请求。在发出代理请求后,将运行“post”过滤器逻辑。
 URIs defined in routes without a port get default port values of 80 and 443 for the HTTP and HTTPS URIs, respectively. 

翻译

在没有端口的路由中定义的URI分别获得HTTP和HTTPS URI的默认端口值80和443。

4.Configuring Route Predicate Factories and Gateway Filter Factories

配置路由谓词工厂和网关筛选器工厂


There are two ways to configure predicates and filters: shortcuts and fully expanded arguments. Most examples below use the shortcut way.

The name and argument names will be listed as code in the first sentance or two of the each section. The arguments are typically listed in the order that would be needed for the shortcut configuration.

翻译

有两种方法可以配置谓词和筛选器:快捷方式和完全展开的参数。下面的大多数示例都使用快捷方式。名称和参数名称将作为代码列在每个部分的第一个或两个符号中。参数通常按快捷方式配置所需的顺序列出。

4.1Shortcut Configuration

快捷方式配置

Shortcut configuration is recognized by the filter name, followed by an equals sign (=), followed by argument values separated by commas (,).

翻译

快捷方式配置由筛选器名称识别,后跟等号(=),后跟用逗号分隔的参数值(,)。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue
The previous sample defines the Cookie Route Predicate Factory with two arguments, the cookie name, mycookie and the value to match mycookievalue.

翻译

上一个示例使用两个参数定义Cookie路由谓词工厂,即Cookie名称、mykokie和匹配mykokievalue的值。

4.2Fully Expanded Arguments

完全展开的参数

Fully expanded arguments appear more like standard yaml configuration with name/value pairs. Typically, there will be a name key and an args key. The args key is a map of key value pairs to configure the predicate or filter.

翻译

完全展开的参数看起来更像带有名称值对的标准yaml配置。通常,会有一个名称键和一个args键。args键是用于配置谓词或筛选器的键值对的映射。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - name: Cookie
          args:
            name: mycookie
            regexp: mycookievalue

This is the full configuration of the shortcut configuration of the Cookie predicate shown above.

翻译

这是上面显示的Cookie谓词的快捷方式配置的完整配置。