一、词汇表
- 路由: 是网关基本的模块,分别为id、目标uri、一组谓词+过滤器一起组合而成,如果谓词匹配成功,则路由匹配成功。
- 谓词: 匹配Http请求参数
- 过滤器: 对下游的服务器之前和之后实现处理。
二、过滤器举例
1.匹配时间之后
| 1 2 3 4 5 | ```
- id: mayikt uri: www.mayikt.com/ ###匹配规则 predicates: - After=2017-01-20T17:42:47.789-07:00[America/Denver]
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
此路由与 2017 年 1 月 20 日 17:42 MountainTime(Denver)之后的所有请求相匹配。
#### [](#2-%E5%8C%B9%E9%85%8D%E5%AF%B9%E5%BA%94%E7%9A%84host "2.匹配对应的host")2.匹配对应的host
| ```
1 2 3 4 5
``` | ```
- id: meite uri: http://www.tinner.com/ ###匹配规则 predicates: - Host=meta.tinner.com
``` |
| ------------------ | ----------------------------------------------------------------------------------------------------- |
访问 mete.tinner.com 转发到<http://www.tinner.com/>
#### [](#3-%E6%9D%83%E9%87%8D%E8%B0%93%E8%AF%8D "3.权重谓词")3.权重谓词
| ```
1 2 3 4 5 6 7 8
``` | ```
- id: weight_high uri: http://www.tinner.com/yushengjun predicates: - Weight=group1, 2 - id: weight_low uri: http://www.tinner.com predicates: - Weight=group1, 1
``` |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
根据权重比例实现转发,这条路线会将约80%的流量转发至[http://www.tinner.com/yushengjun,并将约20%的流量转发至http://www.tinner.com。](http://www.tinner.com/yushengjun%EF%BC%8C%E5%B9%B6%E5%B0%86%E7%BA%A620%EF%BC%85%E7%9A%84%E6%B5%81%E9%87%8F%E8%BD%AC%E5%8F%91%E8%87%B3http://www.tinner.com%E3%80%82)\
…\
还有更多,参考:[官方文档](https://cloud.spring.io/spring-cloud-gateway/reference/html/#gatewayfilter-factories)
### [](#%E4%B8%89%E3%80%81GateWay%E8%A7%A3%E5%86%B3%E8%B7%A8%E5%9F%9F%E7%9A%84%E9%97%AE%E9%A2%98 "三、GateWay解决跨域的问题")三、GateWay解决跨域的问题
**解决跨域的问题**
* 1.HttpClient转发
* 2.使用过滤器允许接口可以跨域 响应头设置
* 3.Jsonp 不支持我们的post 属于前端解决
* 4.Nginx解决跨域的问题保持我们域名和端口一致性
* 5.Nginx也是通过配置文件解决跨域的问题
* 6.基于微服务网关解决跨域问题,需要保持域名和端口一致性
* 7.使用网关代码允许所有的服务可以跨域的问题
* 8.使用SpringBoot注解形式@CrossOrigin
网关代码如下:
| ```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
``` | ```
*/ @Component public class CrossOriginFilter implements GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); HttpHeaders headers = response.getHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, OPTIONS, DELETE, PATCH"); 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); } }
``` |
| -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
### [](#%E5%9B%9B%E3%80%81%E7%BD%91%E5%85%B3GateWay%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90 "四、网关GateWay源码分析")四、网关GateWay源码分析
* 1.客户端向网关发送Http请求,会到达`DispatcherHandler`接受请求,匹配到`RoutePredicateHandlerMapping`。
* 2.根据RoutePredicateHandlerMapping匹配到具体的路由策略。
* 3.FilteringWebHandler获取的路由的GatewayFilter数组,创建 GatewayFilterChain 处理过滤请求执行我们的代理业务逻辑访问。

#### [](#SpringBoot%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81%E7%9A%84%E5%85%A5%E5%8F%A3 "SpringBoot项目源码的入口")SpringBoot项目源码的入口
* 1.GatewayClassPathWarningAutoConfiguration—–>检查是否配置我们webfux依赖。
* 2.GatewayAutoConfiguration—–>核心配置类加载了我们Gateway需要的注入的类。
* 3.GatewayLoadBalancerClientAutoConfiguration—–>网关需要使用的负载均衡(Lb//“服务名” 根据服务名称查找真实地址)
* 4.GatewayRedisAutoConfiguration—–>网关整合Redis整合Lua实现限流
* 5.GatewayDiscoveryClientAutoConfiguration—–>服务注册与发现功能
**注意:** gateway启动时,对于一个请求默认会有8个过滤器进行装配。