文章目录
- 一、Gateway网关
- 1.网关的作用
- 二、配置网关
- 1.创建gateway模块
- 2.引入依赖
- 3.编写application.yml
- 4.启动gateway模块,查看是否能访问user-service服务
- 三、路由断言工厂Route Predicate Factory
- 四、GatewayFilter(过滤器)
- 1.添加过滤器方式一:
- 2.添加过滤器方式二:
- 3.添加过滤器方式三(全局过滤器):
- 五、过滤器执行顺序
- 六、Gateway跨域问题处理
一、Gateway网关
1.网关的作用
对用户请求做身份认证、权限校验。
将用户请求路由到微服务,并实现负载均衡。
对用户请求做限流。
二、配置网关
1.创建gateway模块
2.引入依赖
<!-- 网关依赖-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency><!-- nacos服务发现依赖-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.编写application.yml
server:port: 10010 #网关端口
spring:application:name: gateway #服务名称cloud:nacos:server-addr: localhost:8848 #nacos地址gateway:routes: #网关配置- id: user-service #路由id,自定义,只要唯一即可uri: lb://userservice #路由的目标地址,lb是负载均衡,后面跟服务名称predicates: #路由断言,就是判断请求是否符合路由规则的条件- Path=/user/** #这个是按照路径匹配,只要是以/user/开头的就符合要求
4.启动gateway模块,查看是否能访问user-service服务
三、路由断言工厂Route Predicate Factory
1、我们在配置文件中写的断言规则只是字符串,这些字符串会被Predicate Factory读取并处理,转变为路由判断的条件。
2、例如:Path=/user/**
是按照路径匹配,这个规则是由org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory类来处理的。
3、断言工厂:docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-after-route-predicate-factory
4、Spring提供的11中基本断言工厂:
四、GatewayFilter(过滤器)
GatewayFilter是网关中提供的一种过滤器,可以对进入网关的请求和微服务返回的响应做处理:
1.添加过滤器方式一:
server:port: 10010 #网关端口
spring:application:name: gateway #服务名称cloud:nacos:server-addr: localhost:8848 #nacos地址gateway:routes: #网关配置- id: user-service #路由id,自定义,只要唯一即可uri: lb://userservice #路由的目标地址,lb是负载均衡,后面跟服务名称predicates: #路由断言,就是判断请求是否符合路由规则的条件- Path=/user/** #这个是按照路径匹配,只要是以/user/开头的就符合要求filters: #只会对user-service这个路由的请求生效
2.添加过滤器方式二:
server:port: 10010 #网关端口
spring:application:name: gateway #服务名称cloud:nacos:server-addr: localhost:8848 #nacos地址gateway:routes: #网关配置- id: user-service #路由id,自定义,只要唯一即可uri: lb://userservice #路由的目标地址,lb是负载均衡,后面跟服务名称predicates: #路由断言,就是判断请求是否符合路由规则的条件- Path=/user/** #这个是按照路径匹配,只要是以/user/开头的就符合要求default-filters: #默认过滤器,会对所有的路由请求生效
3.添加过滤器方式三(全局过滤器):
自定义类,实现GlobalFilter接口,添加@Order
注解:
@Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {}
}
五、过滤器执行顺序
请求进入网关会碰到三类过滤器:当前路由的过滤器、DefaultFilter、GlobalFilter。
执行顺序:
每一个过滤器都必须指定一个int类型的order值,
order值越小,优先级越高,执行顺序越靠前
。
GlobalFilter通过实现Ordered接口,或者添加@Order注解来指定order值,由我们自己指定。
路由过滤器和defaultFilter的order由Spring指定,默认是按照声明顶序从1递增。
当过滤器的order值一样时,会按照defaultFilter > 路由过滤器 > GlobalFilter
的顺序执行。
六、Gateway跨域问题处理
网关处理跨域采用的同样是CORS方案,并且只需要简单配置即可实现:
spring:cloud:gateway:globalcors: #全局的跨域处理add-to-simple-url-handler-mapping: true #解决options请求被拦截问题corsConfigurations:'[/**]':allowedOrigins: #允许哪些网站的跨域请求- "http://localhost:8089"- "http://www.baidu.com"allowedMethods: #允许的跨域ajax的请求方式- "GET"- "POST"- "DELETE"- "PUT"- "OPTIONS"allowedHeaders: "*" #允许在请求中携带头信息allowCredentials: true #是否允许携带cookiemaxAge: 36000 #这次跨域检查的有效期