gateway官网解读(四)

news/2024/11/8 18:51:15/

这是最后一块了, 要吐了......读官网真的不是一件人干的事情啊,尤其是我这种四级都是磕磕绊绊的人. 读完之后我会产出一篇总结.算是对我, 主要是对我老大有个交代. 

9. TLS and SSL

我现百度了一下

SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层。SSL通过互相认证、使用数字签名确保完整性、使用加密确保私密性,以实现客户端和服务器之间的安全通讯。该协议由两层组成:SSL记录协议和SSL握手协议。

  TLS:(Transport Layer Security,传输层安全协议),用于两个应用程序之间提供保密性和数据完整性。该协议由两层组成:TLS记录协议和TLS握手协议。

https://blog.csdn.net/qq_33932782/article/details/55096383

简单来说我感觉这玩意有点像是socket的一种连接协议(我一共看了两分钟).

The gateway can listen for requests on HTTPS by following the usual Spring server configuration. The following example shows how to do so:

Example 63. application.yml

server:ssl:enabled: truekey-alias: scgkey-store-password: scg1234key-store: classpath:scg-keystore.p12key-store-type: PKCS12

You can route gateway routes to both HTTP and HTTPS backends. If you are routing to an HTTPS backend, you can configure the gateway to trust all downstream certificates with the following configuration:

Example 64. application.yml

spring:cloud:gateway:httpclient:ssl:useInsecureTrustManager: true

Using an insecure trust manager is not suitable for production. For a production deployment, you can configure the gateway with a set of known certificates that it can trust with the following configuration:

Example 65. application.yml

spring:cloud:gateway:httpclient:ssl:trustedX509Certificates:- cert1.pem- cert2.pem

If the Spring Cloud Gateway is not provisioned with trusted certificates, the default trust store is used (which you can override by setting the javax.net.ssl.trustStore system property).

9.1. TLS Handshake

The gateway maintains a client pool that it uses to route to backends. When communicating over HTTPS, the client initiates a TLS handshake. A number of timeouts are associated with this handshake. You can configure these timeouts can be configured (defaults shown) as follows:

Example 66. application.yml

spring:cloud:gateway:httpclient:ssl:handshake-timeout-millis: 10000close-notify-flush-timeout-millis: 3000close-notify-read-timeout-millis: 0

说的就是监控http和https的配置, 不过貌似我没用这个配置也不错...因为我把https解析放在nginx上了,加一句网关上面放了一层nginx我理解就是得给运维的小哥哥一口饭吃.开个玩笑, 留个nginx很多东西可以在ng去做,毕竟gateway属于应用范畴,指标不如nginx稳定.

10. Configuration

onfiguration for Spring Cloud Gateway is driven by a collection of RouteDefinitionLocator instances. The following listing shows the definition of the RouteDefinitionLocator interface:

Example 67. RouteDefinitionLocator.java

public interface RouteDefinitionLocator {Flux<RouteDefinition> getRouteDefinitions();
}

By default, a PropertiesRouteDefinitionLocator loads properties by using Spring Boot’s @ConfigurationProperties mechanism.

The earlier configuration examples all use a shortcut notation that uses positional arguments rather than named ones. The following two examples are equivalent:

Example 68. application.yml

spring:cloud:gateway:routes:- id: setstatus_routeuri: https://example.orgfilters:- name: SetStatusargs:status: 401- id: setstatusshortcut_routeuri: https://example.orgfilters:- SetStatus=401

可以通过属性名称来过滤

11. Route Metadata Configuration

您可以使用元数据为每个路由配置其他参数,如下所示:

You can configure additional parameters for each route by using metadata, as follows:

Example 69. application.yml

spring:cloud:gateway:routes:- id: route_with_metadatauri: https://example.orgmetadata:optionName: "OptionValue"compositeObject:name: "value"iAmNumber: 1

You could acquire all metadata properties from an exchange, as follows:

Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
// get all metadata properties
route.getMetadata();
// get a single metadata property
route.getMetadata(someKey);

12. Http timeouts configuration

12.1. Global timeouts

To configure Global http timeouts:
connect-timeout must be specified in milliseconds.
response-timeout must be specified as a java.time.Duration

global http timeouts example

spring:cloud:gateway:httpclient:connect-timeout: 1000response-timeout: 5s

12.2. Per-route timeouts

To configure per-route timeouts:
connect-timeout must be specified in milliseconds.
response-timeout must be specified in milliseconds.

per-route http timeouts configuration via configuration

      - id: per_route_timeoutsuri: https://example.orgpredicates:- name: Pathargs:pattern: /delay/{timeout}metadata:response-timeout: 200connect-timeout: 200

per-route timeouts configuration using Java DSL

import static org.springframework.cloud.gateway.support.RouteMetadataUtils.CONNECT_TIMEOUT_ATTR;
import static org.springframework.cloud.gateway.support.RouteMetadataUtils.RESPONSE_TIMEOUT_ATTR;@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){return routeBuilder.routes().route("test1", r -> {return r.host("*.somehost.org").and().path("/somepath").filters(f -> f.addRequestHeader("header1", "header-value-1")).uri("http://someuri").metadata(RESPONSE_TIMEOUT_ATTR, 200).metadata(CONNECT_TIMEOUT_ATTR, 200);}).build();}

12.3. Fluent Java Routes API

就是jdk8的流式布局

12.4. The DiscoveryClient Route Definition Locator 结合注册中心,也不知道为啥这么重要的东西放在最后面,一如既往的坑

就是你要是用了注册中心需要spring.cloud.gateway.discovery.locator.enabled = true

12.4.1. Configuring Predicates and Filters For DiscoveryClient Routes

By default, the gateway defines a single predicate and filter for routes created with a DiscoveryClient.

The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is the ID of the service from the DiscoveryClient.

The default filter is a rewrite path filter with the regex /serviceId/(?<remaining>.*) and the replacement /${remaining}. This strips the service ID from the path before the request is sent downstream.

If you want to customize the predicates or filters used by the DiscoveryClient routes, set spring.cloud.gateway.discovery.locator.predicates[x] and spring.cloud.gateway.discovery.locator.filters[y]. When doing so, you need to make sure to include the default predicate and filter shown earlier, if you want to retain that functionality. The following example shows what this looks like:

Example 71. application.properties

spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"

最想要的居然他大爷的就这么点

12.4.1。为DiscoveryClient路由配置谓词和过滤器
默认情况下,网关为使用DiscoveryClient创建的路由定义单个谓词和过滤器。

默认谓词是使用/ serviceId / **模式定义的路径谓词,其中serviceId是来自DiscoveryClient的服务的ID。

缺省过滤器是带有正则表达式/serviceId/(?<remaining>.*)和替换项/ $ {remaining}的重写路径过滤器。这会在向下游发送请求之前从路径中剥离服务ID。

如果要自定义DiscoveryClient路由使用的谓词或过滤器,请设置spring.cloud.gateway.discovery.locator.predicates [x]和spring.cloud.gateway.discovery.locator.filters [y]。这样做时,如果要保留该功能,则需要确保包括前面显示的默认谓词和过滤器。下面的示例显示其外观:

让我平复一下吐槽的心情: 意思就是你可以接入网关, 通过spring.cloud.gateway.discovery.locator.enabled = true 开启, 他连个yml都没舍得给写, 我了去了.开启以后呢你可以根据之前的写若干的断言和过滤器....现在想想貌似这个也有原因,接入网关好像也的确不这么写, 我们一会再说

13. Reactor Netty Access Logs

我用的log4j2不是back, 我就不多说了哈

14. CORS Configuration 跨域

spring:cloud:gateway:globalcors:cors-configurations:'[/**]':allowedOrigins: "https://docs.spring.io"allowedMethods:- GET

I

15. Actuator API 健康 检测

The /gateway actuator endpoint lets you monitor and interact with a Spring Cloud Gateway application. To be remotely accessible, the endpoint has to be enabled and exposed over HTTP or JMX in the application properties. The following listing shows how to do so:

Example 74. application.properties

management.endpoint.gateway.enabled=true # default value
management.endpoints.web.exposure.include=gateway

15.1. Verbose Actuator Format

/actuator/gateway/routes

 /actuator/gateway/globalfilters

16. Troubleshooting 常见问题

这个日志级别会比较牛逼, 可以监听server和client日志

请分别为HttpServer和HttpClient设置spring.cloud.gateway.httpserver.wiretap = true或spring.cloud.gateway.httpclient.wiretap = true。不过好像是从G版的SR3才开始

17. Developer Guide 也不知道为啥在这整了个指南

17.1. Writing Custom Route Predicate Factories 路由工厂

In order to write a Route Predicate you will need to implement RoutePredicateFactory. There is an abstract class called AbstractRoutePredicateFactory which you can extend.

MyRoutePredicateFactory.java

public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> {public MyRoutePredicateFactory() {super(Config.class);}@Overridepublic Predicate<ServerWebExchange> apply(Config config) {// grab configuration from Config objectreturn exchange -> {//grab the requestServerHttpRequest request = exchange.getRequest();//take information from the request to see if it//matches configuration.return matches(config, request);};}public static class Config {//Put the configuration properties for your filter here}}

17.2. Writing Custom GatewayFilter Factories

PostGatewayFilterFactory.java

public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory<PostGatewayFilterFactory.Config> {public PostGatewayFilterFactory() {super(Config.class);}@Overridepublic GatewayFilter apply(Config config) {// grab configuration from Config objectreturn (exchange, chain) -> {return chain.filter(exchange).then(Mono.fromRunnable(() -> {ServerHttpResponse response = exchange.getResponse();//Manipulate the response in some way}));};}public static class Config {//Put the configuration properties for your filter here}}

17.2.1. Naming Custom Filters And References In Configuration

17.3. Writing Custom Global Filters

To write a custom global filter, you must implement GlobalFilter interface. This applies the filter to all requests.

The following examples show how to set up global pre and post filters, respectively:

@Bean
public GlobalFilter customGlobalFilter() {return (exchange, chain) -> exchange.getPrincipal().map(Principal::getName).defaultIfEmpty("Default User").map(userName -> {//adds header to proxied requestexchange.getRequest().mutate().header("CUSTOM-REQUEST-HEADER", userName).build();return exchange;}).flatMap(chain::filter);
}@Bean
public GlobalFilter customGlobalPostFilter() {return (exchange, chain) -> chain.filter(exchange).then(Mono.just(exchange)).map(serverWebExchange -> {//adds header to responseserverWebExchange.getResponse().getHeaders().set("CUSTOM-RESPONSE-HEADER",HttpStatus.OK.equals(serverWebExchange.getResponse().getStatusCode()) ? "It worked": "It did not work");return serverWebExchange;}).then();
}

总结起来呢 就是他建议你如果用单一的校验,就用工厂模式17.1那种,如果是全局的呢就用17.2

18. Building a Simple Gateway by Using Spring MVC or Webflux

19. Configuration properties

To see the list of all Spring Cloud Gateway related configuration properties, see the appendix.

重点是第19个, 我找到了我翻遍百度没找到的配置清单.......想哭


http://www.ppmy.cn/news/628751.html

相关文章

用C++写个进度条

实现一个命令行进度条&#xff0c;使用线程&#xff0c;不会换行欧。支持自定义进度条的条的字符&#xff0c;可以暂停和继续。 在写的过程中还遇到一个错误&#xff0c;之前多线程写的少不知道&#xff0c;贴出来给大家看一下&#xff1a; terminate called without an activ…

Virtualbox安装安卓模拟器

目录 下载 安装环境 安装步骤 新建模拟器 调整配置 安装 遇到问题 参考文档 下载 下载最新的安装包&#xff0c;本文使用android-x86_64-9.0-r2安装测试&#xff0c;下载地址&#xff1a;Android-x86 download latest version​​​​​​v virtual box下载地址&…

Oracle VM VirtualBox 虚拟机中桥接模式一直不能用 ,需要安装 VBoxNetLwf.inf

............\VirtualBox\drivers\network\netlwf\VBoxNetLwf.inf 文件位置

《关于VirtualBox在桥接模式下无法联网 解决方案》

环境基于&#xff1a;WIN7 &#xff0c; 笔记本wify , Vbox 博主遇到了这个问题&#xff0c;百度了大半天还是没有解决到&#xff0c;可能是由于下面链接的文章的标题关键词比较偏&#xff0c;很难精确的定位到此文章&#xff0c;于是博主就决定总结归纳了一下&#xff0c;希望…

【技术分享】IBM服务器系统安装安装指南

ServerGuide 引导安装指南 &#xff08;不配置阵列&#xff09;适用于当前System X 大部分机型 如果没有ServerGuide CD光盘的朋友可以去这里下载&#xff1a; http://www.verycd.com/groups/software/74859.topic 设置和配置概述&#xff1a; 使用ServerGuide 设置和安装CD 时…

Windows系统VirtualBox下载与安装

Windows系统VirtualBox下载与安装 1、下载&#xff1a;https://www.virtualbox.org/wiki/Downloads 1.1、安装包 1.2、扩展包&#xff08;对USB 2.0、USB 3.0、远程桌面协议 VRDP等实用功能的支持&#xff09; 2、安装 2.1、如果安装出现本机缺少必要包的情况&#xff0c;不…

统信UOS使用wine安装“方正ApabiReader_4.5.2.1790(ceb阅读器)”并解决安装过程中文显示成方块问题

本教程基于64位系统 缘由 统信UOS应用商店里提供的“方正ceb阅读器”和“方正版式阅读器”并不能正常工作&#xff0c;提示无效的加密许可&#xff0c;无法打开ceb文件。 下面我们使用wine来安装“方正ApabiReader_4.5.2.1790&#xff08;ceb阅读器&#xff09;”。 wine安装…

virtualbox的下载和安装详细过程

一、下载virtualbox 官网下载地址&#xff1a;Oracle VM VirtualBox 步骤一&#xff1a;进入官网后点击Downloads&#xff08;下载&#xff09; 步骤二&#xff1a;然后点击windows hosts(视窗主机&#xff09;&#xff0c;下载好后点击安装包进行安装 步骤三&#xff1a;下载v…