路由网关

Gateway

动嘴,理论。动手,架构

SpringCloud Gateway使用的Webflux中的reactor-netty响应式编程组件,底层使用了netty通讯框架。基于NIO(异步非阻塞模型)

传统框架:structs2,springmvc都是基于servlet API和Servlet容器。也就是阻塞框架,而Servlet3.1后就有了异步非阻塞的支持,WebFlux就是一个典型非阻塞异步的框架。他的核心基于Reactor

3大核心:路由,断言,过滤

静态配置

有两种方式:1,修改yml文件

  1. 建module
  2. 修改pom
    需要导入一些新的内容包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>top.zfxt.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
  1. 修改yml
    他主要就是eureka配置即可
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server:
    port: 9527

    spring:
    application:
    name: cloud-gateway

    eureka:
    instance:
    hostname: cloud-gateway-service
    client:
    service-url:
    register-with-eureka: true
    fetch-registry: true
    defaultZone: http://eureka7001.com:7001/eureka
  2. 路由网关配置
    在application.yml中设置如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    spring:
    application:
    name: cloud-gateway
    cloud:
    gateway:
    routes:
    - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
    uri: http://localhost:8001 #匹配后提供服务的路由地址
    predicates:
    - Path=/payment/get/** #断言,路径相匹配的进行路由


    - id: payment_routh2 #payment route #路由的ID,没有固定规则但要求唯一,建议配合服务名
    uri: http://localhost:8001 #匹配后提供服务的路由地址
    predicates: #断言,路径相匹配的进行路由
    - Path=/payment/lb/**
    完成配置后,即可通过9527这个端口访问服务

第二种方式,添加一个配置项

在代码中注入RouteLocatorBean

  1. 构建config类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Configuration
    open class GatewayConfig {

    @Bean
    open fun customRoutelocator(routeLocatorBuilder:RouteLocatorBuilder): RouteLocator? {
    val routes = routeLocatorBuilder.routes()
    routes.route("path_route_baidu"){
    it.path("/guonei").uri("https://news.baidu.com/guonei")
    }.build()
    return routes.build()
    }
    }

动态配置

允许利用注册中心动态创建路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true # 允许利用注册中心动态创建路由
routes:
- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://CLOUD-PAYMENT-SERVICE
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由


- id: payment_routh2 #payment route #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://CLOUD-PAYMENT-SERVICE
predicates: #断言,路径相匹配的进行路由
- Path=/payment/lb/**

predicates


根据这个图示,可以分辨出他具有那些可选的配置项,包括Path等

filter

路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。
自定义过滤器:需要实现两个接口。GlobalFilter,Ordered

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
val log: InfoLog = Log.get(InfoLog::class.java)

@Component
open class MyLogGlobalFilter : GlobalFilter, Ordered {
override fun filter(exchange: ServerWebExchange?, chain: GatewayFilterChain?): Mono<Void> {
log.info("*****************come in MyLogGlobalFilter*********************")
val name = exchange?.request?.queryParams?.getFirst("uname")
if (name == null) {
log.info("**********用户名为非法null")
exchange!!.response.statusCode = HttpStatus.NOT_ACCEPTABLE
return exchange.response.setComplete()
}
return chain!!.filter(exchange)
}
//用来设置优先级,数据越小,优先级越高
override fun getOrder(): Int {
return 0
}
}