Spring cloud gateway 实现网关路由转发和过滤功能

Spring cloud gateway 实现网关路由转发和过滤功能简单demo,源码下载:https://download.csdn.net/download/m0_37732829/12558327

demo介绍

1. 网关

1.1.网关pom.xml配置

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

1.2.网关application.yml

spring:
  cloud:
    gateway:
      routes:
        - id: demo                          #路由的唯一id
          uri: http://localhost:8080        #指向注册中心的服务,使用lb:// 加上ServiceName,当然也可以通过http://localhost:8080指向
          predicates:                       #要进行的断言
            - Path=/d1/**                 #路径匹配
            - Header=auth-name              #请求头匹配
            - Header=auth-token
          filters:                          #过滤器
            - StripPrefix=1                 #截取url,本例中就是会把/demo截掉,后面的部分才是转发的url
server:
  port: 80

2. 被转发的demo服务器配置

2.1.pom.xml配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

2.2.application.yml

server:
  port: 8080

3. 成果展示

1)启动网关服务器gateway和测试服务器demo

2)打开postman,通过网关服务器gateway访问demo服务器,配置请求头auth-name和auth-token

springcloud-gateway的简单路由转发和请求过滤就完成了。

猜你喜欢

转载自blog.csdn.net/m0_37732829/article/details/106999957