Spring Cloud Gateway是Spring Cloud的一个全新项目,该项目是基于Spring5.0,Spring Boot 2.0和Project Reactor等响应式编程和事件流技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。
1.1、为什么需要网关Gateway网关是我们服务的守门神,所有微服务的统一入口。
网关的核心功能特性:
- 服务路由、负载均衡
- 身份认证和权限校验
- 请求限流
架构图:
权限控制:网关作为微服务入口,需要校验用户是否有请求资格,如果没有,则进行拦截。
路由和负载均衡:一切请求都必须先经过gateway,但网关不处理业务,而是根据某种规则,把请求转发到某个微服务,这个过程叫做路由。当然路由的目标服务有多个时,还需要做负载均衡。
限流:当请求流量过高时,在网关中按照下流的微服务能够接受的速度来放行请求,避免服务压力过大。
在SpringCloud中网关的实现包括两种:
- gateway
- zuul
zuul是基于servlet的实现,属于阻塞式编程。而SpringCloudGateWay则是基于Spring5中提供的WebFlux,属于响应式编程的实现,具备更好的性能。
下面,我们就演示下网关的基本路由功能。基本步骤如下:
- 创建SpringBoot功能gateway,引入网关依赖
- 编程启动类
- 编写基础配置和路由规则
- 启动网关服务进行测试
1.2.2、编写启动类org.springframework.cloud spring-cloud-starter-gateway com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
package com.xbmu.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
1.2.3、编写基础配置和路由规则
参考链接:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#the-path-route-predicate-factory
server:
port: 10010 # 网关端口
spring:
application:
name: gateway # 服务名称
cloud:
nacos:
server-addr: 192.168.184.110:8848 # nacos地址
gateway:
routes: # 网关路由配置
- id: user-service # 路由id,自定义,只要唯一即可
uri: http://127.0.0.1:8082 # 路由的目标地址 http就是固定地址
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/user/** # 这个是按照路径匹配,只要以/user/开头就符合要求
- id: order-service
uri: http://127.0.0.1:8080
predicates:
- Path=/order/**
我们将符合Path规则的一切请求,都代理到uri参数指定的地址。
1.2.4、重启测试在filter中编写自定义逻辑,可以实现下列功能: 需求:定义全局过滤器,拦截请求,判断请求的参数是否满足下面条件: 参数中是否有authorization, authorization参数值是否为admin 如果同时满足则放行,否则拦截 实现: 在gateway中定义一个过滤器: 请求进入网关会碰到三类过滤器:当前路由的过滤器、DefaultFilter、GlobalFilter 详细内容,可以查看源码: 跨域:域名不一致就是跨域,主要包括: 域名不同: https://www.taobao.com 和 https://www.taobao.org 和 https://www.jd.com 和 https://miaosha.jd.com 域名相同,端口不同:localhost:8080和localhost8081 跨域问题:浏览器禁止请求的发起者与服务端发生跨域ajax请求,请求被浏览器拦截的问题 解决方案:CORS,这个以前应该学习过,这里不再赘述了。不知道的小伙伴可以查看https://www.ruanyifeng.com/blog/2016/04/cors.html 在gateway服务的application.yml文件中,添加下面的配置:
本例中,我们将/user
Mono
1.5.2、自定义全局过滤器
package com.xbmu.gateway.filters;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter {
@Override
public Mono
1.5.3、过滤器执行顺序
请求路由后,会将当前路由过滤器和DefaultFilter、GlobalFilter,合并到一个过滤器链(集合)中,排序后依次执行每个过滤器:
排序的规则是什么呢?
org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator#getFilters()方法是先加载defaultFilters,然后再加载某个route的filters,然后合并。
org.springframework.cloud.gateway.handler.FilteringWebHandler#handle()方法会加载全局过滤器,与前面的过滤器合并后根据order排序,组织过滤器链
从localhost:8090访问localhost:10010,端口不同,显然是跨域的请求。spring:
cloud:
gateway:
# ...
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://localhost:8090"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期



