SpringBoot2 整合 SpringCloud 的 Gateway 入门实例

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

1. 网关工程

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

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

网关工程目录如下图所示:
SpringBoot2 整合 SpringCloud 的 Gateway 入门实例_第1张图片
pom.xml 如下所示,引入了 spring-cloud-starter-gateway 的依赖:


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.4.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.syrdbtgroupId>
    <artifactId>studyartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>studyname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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,工程目录如下图所示:
SpringBoot2 整合 SpringCloud 的 Gateway 入门实例_第2张图片
pom.xml 如下所示,引入了 spring-boot-starter-web :


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.5.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.syrdbtgroupId>
    <artifactId>adminartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>adminname>
    <description>Demo project for Spring Bootdescription>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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 。
SpringBoot2 整合 SpringCloud 的 Gateway 入门实例_第3张图片
访问 http://localhost:8080/hello1,因为不符合断言,无法进行转发,所以 404 找不到对应的页面。
SpringBoot2 整合 SpringCloud 的 Gateway 入门实例_第4张图片

3. 参考文献

  • Spring Cloud Gateway 2.1.0 中文官网文档
  • Spring Cloud Gateway 初体验

你可能感兴趣的:(#,----SpringCloud,三,后端开发)