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。
1.2 SpringCloud Gateway 特征
SpringCloud官方,对SpringCloud Gateway 特征介绍如下:
(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
(2)集成 Hystrix 断路器
(3)集成 Spring Cloud DiscoveryClient
(4)Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters
(5)具备一些网关的高级功能:动态路由、限流、路径重写
从以上的特征来说,和Zuul的特征差别不大。SpringCloud Gateway和Zuul主要的区别,还是在底层的通信框架上。
简单说明一下上文中的三个术语:
(1)Filter(过滤器):
和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。
(2)Route(路由):
网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。
(3)Predicate(断言):
这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。
2.什么是服务网关
3.SpringCloud Gateway怎么用
3.1 引入依赖(这里springboot版本2.2.8.RELEASE,springboot不能引入web(tomcat)组件)
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </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> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
3.2 YML配置
server: port: 7001 spring: application: name: gateway-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: e8c32ac0-391b-44dd-94bf-bd23ebaf5aca gateway: routes: - id: member uri: http://localhost:9005/ predicates: - Path=/member/**
4.SpringCloud Gateway路由规则
4.1 Path
gateway: routes: - id: member uri: http://localhost:9005/ predicates: - Path=/member/**
4.2 Query
4.3 Method
4.4 DateTime
4.5 RemoteAddr
4.6 Header
5.SpringCloud Gateway动态路由(服务发现的路由--和注册中心搭配用)
5.1 动态获取URI
gateway: routes: - id: member uri: lb://member #lb代表负载均衡 member为注册中心上的服务名 predicates: - Path=/member/**
5.2 服务名称转发
gateway: discovery: locator: enabled: true lower-case-service-id: true
这样访问:http://localhost:7001/oneserver-service/oneserver/get/demo oneserver-service为服务在注册中心上的服务名
6.SpringCloud Gateway 过滤器
6.1 网关过滤器
6.2 全局过滤器
6.2.1 自定义全局过滤器
package com.zdyl.gateway.filters; import com.zdyl.gateway.common.utils.Constant; import com.zdyl.gateway.config.WebserverConfig; import com.zdyl.gateway.dto.SysUserEntityDTO; import com.zdyl.gateway.dto.SysUserTokenEntityDTO; import com.zdyl.gateway.utils.RedisUtil; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import javax.annotation.Resource; import java.util.Date; import java.util.List; /** * 全局过滤器 */ @Configuration public class CustomerFilter implements GlobalFilter, Ordered { @Resource private RedisUtil redisUtils; @Resource private WebserverConfig webserverConfig; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); String requestUrl = request.getPath().toString();
//指定地址不需要验证 if (contains(requestUrl)) { return chain.filter(exchange); } else { String token = getRequestToken(request); if (org.apache.commons.lang.StringUtils.isEmpty(token)) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } String tokenDto = redisUtils.getString("token:" + token, 0); if (tokenDto == null) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } SysUserTokenEntityDTO sysUserTokenEntityDTO = redisUtils.fromJson(tokenDto, SysUserTokenEntityDTO.class); Date loginTime = sysUserTokenEntityDTO.getExpireTime(); long exitTime = loginTime.getTime() / 1000; long nowTime = new Date().getTime() / 1000; if (nowTime > exitTime) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } String currentUser = redisUtils.getString("user:" + sysUserTokenEntityDTO.getUserId(), 0); if (currentUser == null) { response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } SysUserEntityDTO sysUserEntityDTO = redisUtils.fromJson(currentUser, SysUserEntityDTO.class); //设置用户信息到请求,注意,这里是追加头部信息,token信息已经有了 ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(Constant.CURRENTUSER, currentUser).build(); exchange = exchange.mutate().request(mutableReq).build(); }
//跨域设置 HttpHeaders headers = response.getHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST,GET,PUT,DELETE"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*"); return chain.filter(exchange); } @Override public int getOrder() { return 0; } /** * 获取请求的token */ private String getRequestToken(ServerHttpRequest httpRequest) { //从header中获取token String token = null; token = httpRequest.getHeaders().getFirst("token"); //如果header中不存在token,则从参数中获取token if (StringUtils.isEmpty(token)) { token = httpRequest.getQueryParams().getFirst("token"); } return token; } private boolean contains(String requestUrl) { List<String> ignoreUrlList = webserverConfig.getIgnoreUrlList(); for (String string : ignoreUrlList) { if (requestUrl.contains(string)) { return true; } } return false; } }
6.2.2 完整配置
spring: gateway: routes: - id: oneserver-service uri: lb://oneserver-service #lbu4EE3u8868u8D1Fu8F7Du5747u8861 memberu4E3Au6CE8u518Cu4E2Du5FC3u4E0Au7684u670Du52A1u540D predicates: - Path=/oneserver/** filters: - StripPrefix=1 # u9650u6D41u8FC7u6EE4u5668uFF0Cu4F7Fu7528gatewayu5185u7F6Eu4EE4u724Cu7B97u6CD5 - name: RequestRateLimiter args: # u4EE4u724Cu6876u6BCFu79D2u586Bu5145u5E73u5747u901Fu7387,u5373u884Cu7B49u4EF7u4E8Eu5141u8BB8u7528u6237u6BCFu79D2u5904u7406u591Au5C11u4E2Au8BF7u6C42u5E73u5747u6570 redis-rate-limiter.replenishRate: 1 # u4EE4u724Cu6876u7684u5BB9u91CFuFF0Cu5141u8BB8u5728u4E00u79D2u949Fu5185u5B8Cu6210u7684u6700u5927u8BF7u6C42u6570 redis-rate-limiter.burstCapacity: 8 # u7528u4E8Eu9650u6D41u7684u952Eu7684u89E3u6790u5668u7684 Bean u5BF9u8C61u7684u540Du5B57u3002u5B83u4F7Fu7528 SpEL u8868u8FBEu5F0Fu6839u636E#{@beanName}u4ECE Spring u5BB9u5668u4E2Du83B7u53D6 Bean u5BF9u8C61u3002 key-resolver: "#{@apiKeyResolver}" discovery: locator: enabled: true lower-case-service-id: true