一、概念介绍
1、SpringCloud Gateway简介
SpringCloud Gateway是Spring Cloud的一个全新项目,该项目是基于Spring 5.0,Spring Boot 2.0和 Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。
SpringCloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。
Spring Cloud Gateway的目标,不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。
提前声明:
Spring Cloud Gateway 底层使用了高性能的通信框架Netty。
Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or when built as a WAR.
二、路由规则配置:
1、Route Predicate Factories
- The After Route Predicate Factory
- The Before Route Predicate Factory
- The Between Route Predicate Factory
- The Cookie Route Predicate Factory
- The Header Route Predicate Factory
- The Host Route Predicate Factory
- The Method Route Predicate Factory
- The Path Route Predicate Factory
- The Query Route Predicate Factory
- The RemoteAddr Route Predicate Factory
- The Weight Route Predicate Factory
2、新建测试模块springcloud-getway
2.1、添加pom依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 注意不能引入 spring-boot-starter-web ,否则会报错
Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux.
It does not work in a traditional Servlet Container or when built as a WAR.
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 服务网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
1.2、添加配置application.yml
这里我们先以Before、After、Between三种作配置为例:
- Between=2020-05-11T23:35:47.789+08:00,2022-05-07T19:52:47.789+08:00
- After=2020-02-11T20:35:47.789+08:00
- Before=2020-05-20T23:35:47.789+08:00
server:
port: 8000
spring:
application:
name: springcloud-getway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway: # 网关配置
routes:
- id: test_after_route
uri: http://localhost:8002
predicates: # 断言,如果时间在范围内则访问uri配置地址
- Between=2020-05-11T23:35:47.789+08:00,2022-05-07T19:52:47.789+08:00
- After=2020-02-11T20:35:47.789+08:00
- Before=2020-05-20T23:35:47.789+08:00
Spring Cloud Gateway-路由谓词工厂详解(Route Predicate Factories)
参考文章:http://www.imooc.com/article/290804
1.3、启动类代码:
@SpringBootApplication
public class GatwayApplication {
public static void main(String[] args) {
SpringApplication.run(GatwayApplication.class,args);
}
@Bean
public KeyResolver userKeyResolver(){
return new KeyResolver() {
@Override
public Mono<String> resolve(ServerWebExchange exchange) {
HttpCookie httpCookie=null;
ServerHttpRequest request = exchange.getRequest();
MultiValueMap<String, HttpCookie> cookies = request.getCookies();
for (Map.Entry<String, List<HttpCookie>> stringListEntry : cookies.entrySet()) {
if(stringListEntry.getKey().equals("c")){
httpCookie = stringListEntry.getValue().get(0);
}
}
return Mono.just(httpCookie.getValue());
}
};
}
}
1.4、路由全局配置
/**
* 路由全局配置(配置文件中不需要配置 routes: - id: test_after_route 等等)
*/
public class ShadowGlobaFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
MultiValueMap<String, HttpCookie> cookies = request.getCookies();
for (String s : cookies.keySet()) {
if(s.equals("x")){ // 如果cookie中包含x 则执行过滤
System.out.println("----------------------");
return chain.filter(exchange);
}
}
// 如果cookie中不包含x 则会抛出401
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
}
3、其他路由配置
Getway到服务之间有一个负载均衡,我们可以配置配置一个集群负载去测试。
配置代码如下:
可以通过配置uri: lb://register-server # 集群可以实现负载均衡
server:
port: 8000
spring:
application:
name: springcloud-getway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway: # 网关配置
routes:
- id: test_after_route
uri: http://localhost:8002
predicates: # 断言,如果时间在范围内则访问uri配置地址
- Between=2020-05-11T23:35:47.789+08:00,2022-05-07T19:52:47.789+08:00
- After=2020-02-11T20:35:47.789+08:00
- Before=2020-05-20T23:35:47.789+08:00
- id: path_route
uri: http://localhost:8081
predicates:
- Path=/provider/{segment} # 如果路劲以/provider开始,则访问springcloudalibaba-provider-user:8081/provider
- id: path_route_1
uri: http://localhost:8082
predicates:
- Path=/consumer/{segment} # 如果路劲以/consumer开始,则访问springcloudalibaba-consumer:8082/consumer/{segment}
- id: test_query_route
uri: lb://register-server # 集群可以实现负载均衡
predicates:
- Query=shadow #
filters: # 前置条件是断言predicates条件成立
- Av=true
添加Cookie设置方式如下所示:
更多配置详情参考官网文档:
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/
其他参考博客:
https://www.cnblogs.com/crazymakercircle/p/11704077.html