什么是SpringCloud Gateway
SpringCloud GateWay是Spring官方基于Spring5.0, SpringBoot2.0,SpringWebFlux, Project Reactor技术开发的高性能网关,旨在为微服务架构提供简单、有效且统一的API 路由管理方式。
整体工作架构如下:
核心组件说明
- Route(路由):代表转发规则,由id, 目标url, 一组断言工厂和一组过滤器组成。如果断言为真,说明请求的URL与配置的路由匹配
- Predict(断言):断言函数,与Java8中的Predict语义一致
- Filter(过滤器):代表一个标准的Spring WebFilter. Gateway中有2种Filter,GlobalFilter,全局范围生效,GatewayFilter,在路由上生效。
工作原理
简单使用
依赖引入
<?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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springcloud</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 如果需要把Gateway注册到Eureka上,引入eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
路由配置
代码方式
/***
* 通过代码API配置路由
* @param builder
* @return
*/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().route(r ->
r.path("/greet") // 满足Predict
.filters(f -> f.filter(new CustomGateFilter(), 0))
.uri("http://localhost:8080/greet")) // 转发到对应的URI
.build();
}
public static class CustomGateFilter implements GatewayFilter {
private static final Log log = LogFactory.getLog(GatewayFilter.class);
private static final String countStartTime = "countStartTime";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
exchange.getAttributes().put(countStartTime, System.currentTimeMillis());
return chain.filter(exchange).then(
Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute(countStartTime);
if (startTime != null) {
log.info(exchange.getRequest().getURI().getRawPath() + ":" + (System.currentTimeMillis() - startTime));
}
})
);
}
}
YML配置方式
spring:
application:
name: spring-cloud-gateway
cloud:
gateway:
routes:
- id: taobao_route
uri: http://www.taobao.com
predicates:
- Path=/tb
discovery:
locator:
enabled: true
lowerCaseServiceId: true