微服务 之 Gateway 网关 ( 二 ) 路由规则详解

3.路由规则详解

基本概念:

  • Route:路由网关的基本构建块。它由ID,目的URI,断言(Predicate)集合和过滤器(filter)集合组成。如果断言聚合为真,则匹配该路由。
  • Predicate:这是一个 Java 8函数式断言。允许开发人员匹配来自HTTP请求的任何内容,例如请求头或参数。
  • 过滤器:可以在发送下游请求之前或之后修改请求和响应。

路由根据断言进行匹配,匹配成功就会转发请求给URI,在转发请求之前或者之后可以添加过滤器。

3.1. 断言工厂

Spring Cloud Gateway包含许多内置的Route Predicate工厂。所有这些断言都匹配HTTP请求的不同属性。多路由断言工厂通过and组合。

官方提供的路由工厂:
微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第1张图片

这些断言工厂的配置方式,参照官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第2张图片

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第3张图片

这里重点掌握请求路径路由断言的配置方式:
微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第4张图片

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment},/bar/{segment}

这个路由匹配以/foo或者/bar开头的路径,转发到http:example.org。例如 /foo/1 or /foo/bar or /bar/baz.

3.2. 过滤器工厂

路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应。路径过滤器的范围限定为特定路由。Spring Cloud Gateway包含许多内置的GatewayFilter工厂。

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第5张图片

这些过滤器工厂的配置方式,同样参照官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第6张图片

过滤器 有 20 多个 实现类,根据过滤器工厂的用途来划分,可以分为以下几种:Header、Parameter、Path、Body、Status、Session、Redirect、Retry、RateLimiter和Hystrix

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第7张图片

这里重点掌握PrefixPath GatewayFilter Factory

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第8张图片

上面的配置中,所有的/foo/**开始的路径都会命中配置的router,并执行过滤器的逻辑,在本案例中配置了RewritePath过滤器工厂,此工厂将/foo/(?.*)重写为{segment},然后转发到http://example.org。比如在网页上请求localhost:8090/foo/forezp,此时会将请求转发到http://example.org/forezp的页面

​ 在开发中由于所有微服务的访问都要经过网关,为了区分不同的微服务,通常会在路径前加上一个标识,例如:访问服务提供方:http://localhost:18090/provider/hello ;访问服务消费方:http://localhost:18090/consumer/hi 如果不重写地址,直接转发的话,转发后的路径为:http://localhost:18070/provider/hellohttp://localhost:18080/consumer/hi明显多了一个provider或者consumer,导致转发失败。

这时,我们就用上了路径重写,配置如下:

server:
  port: 18090
spring:
  cloud:
    gateway:
      routes:
        - id: nacos-consumer
          uri: http://127.0.0.1:18080
          predicates:
            - Path=/consumer/**
          filters:
            - RewritePath=/consumer/(?>.*),/$\{segment}
        - id: nacos-provider
          uri: http://127.0.0.1:18070
          predicates:
            - Path=/provider/**
          filters:
            - RewritePath=/provider/(?>.*),/$\{segment}

注意Path=/consumer/**Path=/provider/**的变化

测试:

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第9张图片

3.3. 路由的java代码配置方式(了解)

参见官方文档:

微服务 之 Gateway 网关 ( 二 ) 路由规则详解_第10张图片

代码如下:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
    return builder.routes()
            .route(r -> r.host("**.abc.org").and().path("/image/png")
                .filters(f ->
                        f.addResponseHeader("X-TestHeader", "foobar"))
                .uri("http://httpbin.org:80")
            )
            .route(r -> r.path("/image/webp")
                .filters(f ->
                        f.addResponseHeader("X-AnotherHeader", "baz"))
                .uri("http://httpbin.org:80")
            )
            .route(r -> r.order(-1)
                .host("**.throttle.org").and().path("/get")
                .filters(f -> f.filter(throttle.apply(1,
                        1,
                        10,
                        TimeUnit.SECONDS)))
                .uri("http://httpbin.org:80")
            )
            .build();
}

你可能感兴趣的:(微服务,微服务,gateway,spring,cloud)