Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。 Spring Cloud Gateway构建于Spring生态系统之上,包括Spring5,SpringBoot2等。它的目标是提供简单、有效的方式路由的API
Spring Cloud Gateway不能在传统的Servlet容器中工作。
(1)父级pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>gateway</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>eureka-server</module>
<module>simple-geteway</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
(2)pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>simple-geteway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>simple-geteway</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>gateway</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!--因为spring cloud gateway是基于webflux的,不要导入spring-boot-start-web-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
添加spring-cloud-starter-gateway依赖。请注意,这里千万不能有spring-boot-starter-web(父级pom.xml也没有spring-boot-starter-web),它们两个不能同时存在。 (3)application.properties
server.port= 7010
(4)Application类
package com.cntaiping.tpa.simplegeteway;
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;
@SpringBootApplication
public class SimpleGetewayApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleGetewayApplication.class, args);
}
/**
* gateway中使用RouteLocator的Bean进行路由转发,将请求进行处理,最后转发到目标的下游服务。
* 本例中将请求转发到http://httpbin.org:80这个地址上
* @param builder
* @return
*/
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
//适用于lambda表达式的接口称之为函数型接口(只有一个抽象方法)
//含参Lambda表达式:(x) -> x.f()
//添加一个route让请求“/get”请求都转发到“http://httpbin.org/get”
.route(p -> p.path("/get")
.filters(f -> f.addRequestHeader("flag", "HelloWorld"))
.uri("http://httpbin.org:80"))
.build();
}
}
说明:
httpbin.org 这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Flag": "HelloWorld",
"Forwarded": "proto=http;host=\"localhost:7010\";for=\"0:0:0:0:0:0:0:1:55191\"",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
"X-Forwarded-Host": "localhost:7010"
},
"origin": "0:0:0:0:0:0:0:1, 180.169.108.92, ::1",
"url": "https://localhost:7010/get"
}