【gateway系列】一步步带你学习gateway路由规则实践

目录

1、准备

微服务项目

新创建一个springBoot项目作为微服务,简单一个demo,如下:

@RestController
public class MemberController {


    @RequestMapping("/query-demo")
    public String queryDemo(String name) {
        return "https://blog.csdn.net/"+ name + "/article/details/120475609";
    }

}
  • yml
server:
  port: 8083
spring:
  application:
    name: member-server

gateway网关服务

  • 引入依赖
        <!--    gateway依赖    -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
<!-- springcloud依赖,dependencies同级,非子级 -->
 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • yml配置
server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: https://blog.csdn.net/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Path=/xuan_lu/**    #匹配对应的URL请求,将匹配到的请求追加在目标URI之后

以下路由规则,均改变predicates断言项内容;

2、Path

server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: https://blog.csdn.net/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Path=/xuan_lu/**    #匹配对应的URL请求,将匹配到的请求追加在目标URI之后

请求地址http://localhost:8082/xuan_lu/article/details/120475609
直接跳转到:https://blog.csdn.net/xuan_lu/article/details/120475609
id:我们自定义的路由 ID,保持唯一
uri:目标服务地址
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。

3、Query

  • 请求参数断言
    配置如下:
server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Query=name    #匹配请求参数中包含name的请求

表示:请求地址中参数名字,必须含有name才会允许通过;
http://localhost:8082/query-demo?name=xl

  • 请求参数和请求值断言
    上面参数值可能路由请求结果不是很明显是否成功,那么结合参数值就会会明显:
server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Query=name,xuanlu.   #匹配请求参数中包含name的请求

请求路由转发失败:http://localhost:8082/query-demo?name=xuan
请求路由转发成功:http://localhost:8082/query-demo?name=xuanlu1

4、Method

通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。

server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Method=GET   #匹配请求类型为GET方式
  1. 未通过网关访问:为了验证请求方式,先将代码请求改为@RequestMapping注解标识,表明即支持GET请求,也允许POST请求,分别用浏览器和Postman工具验证,直接请求准备的微服务项目,端口为8083;
    【gateway系列】一步步带你学习gateway路由规则实践
  2. 通过网关访问:微服务项目请求仍使用@RequestMapping注解,网关路由将断言设置为GET请求方式,使用PostMan访问如下:明显路由不通过;
    【gateway系列】一步步带你学习gateway路由规则实践

5、DateTime

关键字 格式 说明
Before - Before=2021-09-27T17:42:47.000+08:00[Asia/Shanghai] 在某个时间之前的请求才会被转发到 http://localhost:8083服务器上
After - After=2021-09-27T17:42:47.000+08:00[Asia/Shanghai] 在某个时间之后的请求才会被转发
Between - Between=2021-09-27T17:42:47.000+08:00Asia/Shanghai],2021-09-27T17:42:47.000+08:00[Asia/Shanghai] 在某个时间段之间的才会被转发
server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - After=2021-09-27T17:42:47.000+08:00[Asia/Shanghai]    #匹配请求时间上海时间17点42分之后

验证通过:
【gateway系列】一步步带你学习gateway路由规则实践
反之,因为小编测试时,时间是20点,所以改为21点,测试验证是否通过:结果很明显失败;
【gateway系列】一步步带你学习gateway路由规则实践

6、RemoteAddr

通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。

server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - RemoteAddr=192.168.31.49/0

7、Header

Header Route Predicate:可以接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。

server:
  port: 8082
spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      # 路由规则
      routes:
        - id: csdn-service  #路由id,唯一
          uri: http://localhost:8083/  #目标URI,路由到微服务的地址
          predicates:           #断言(判断条件)
            - Header=X-Request-Id, \d+

【gateway系列】一步步带你学习gateway路由规则实践

上一篇:开发者社区精选直播合集(二十)| Python入门及大数据应用


下一篇:leetcode 260