SpringCloud Gateway 是 SpringCloud 官方推出的第二代网关框架(第一代:Zuul),网关的常见功能:路由转发、权限校验和流量控制。

1. 网关工程

使用 RouteLocatorBuilder 的 bean 去创建路由,RouteLocatorBuilder 中可以添加各种 Predicate 和 Filter:

  • Predicate :断言,路由转发的判断条件;
  • Filter :过滤器,路由请求时所经过的过滤逻辑,可用于修改请求、响应内容;

网关工程目录如下图所示:
在这里插入图片描述
pom.xml 如下所示,引入了 spring-cloud-starter-gateway 的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.syrdbt</groupId>
    <artifactId>study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动类,GatewaStudyApplication.java ,使用 RouteLocator 进行路由转发,转发到下游业务,下游业务的地址为 http://localhost:8081 ,本网关项目的端口号为默认的 8080;断言用来判断,如果访问路径为/hello则进行过滤和转发,过滤器给转发的请求增加了一个header,header 的 key 为 Hello ,value 为 World。

package com.syrdbt.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.util.RouteMatcher;
import org.springframework.web.bind.annotation.RequestHeader;

@SpringBootApplication
public class GatewaStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewaStudyApplication.class, args);
    }

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes().route(p -> p
                // 断言,路由转发的判断条件,path 为使用过滤器修改 /hello
                .path("/hello")
                // 过滤器,路由请求时所经过的过滤逻辑,可用于修改请求、响应内容;
                .filters(f -> f.addRequestHeader("Hello", "World"))
                .uri("http://localhost:8081"))
                .build();
    }
}

2. Web 工程,网关的下游业务

网关的下游业务 admin-web,工程目录如下图所示:
在这里插入图片描述
pom.xml 如下所示,引入了 spring-boot-starter-web :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.syrdbt</groupId>
    <artifactId>admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制器,响应从网关发过来的请求,并打印了请求中key为Hello的Header,HelloController.java:

package com.syrdbt.admin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author syrdbt
 * @date 2020-03-03
 */
@RestController
public class HelloController {

    /**
     * 响应从网关发过来的请求,并打印了请求中key为Hello的Header
     */
    @RequestMapping("/hello")
    public String hello(HttpServletRequest request) {
        return "转发成功。请求头Hello的值" + request.getHeader("Hello");
    }
}

启动类,正常创建即可,不过需要修改一下工程的端口号为 8081,application.properties 如下所示 :

server.port=8081
2.3 测试

访问 http://localhost:8080/hello,请求被转发到 http://localhost:8081/hello 。
在这里插入图片描述
访问 http://localhost:8080/hello1,因为不符合断言,无法进行转发,所以 404 找不到对应的页面。
在这里插入图片描述

3. 参考文献

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐