1. 核心概念
2.过滤器
2.1 网关过滤器 Gateway Filler
2.1.1 Path路径过滤器
Path路径过滤器
可以实现 URL 的重写.通过URL重写,
可以实现隐藏实际路径,提高安全性
易于记忆,和键盘输入
易于被搜索引擎收录等优点
由:http://localhost:7000/api-gateway/goods/test改写http://localhost:7000/goods/test
server:
port: 7000
spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
discovery:
locator:
# 是否与服务发现组件进行结合,通过 serviceId 转发到具体的微服务
enabled: true #是否开启基于服务发现得路由规则
lower-case-service-id: true # 是否将服务名称转小写
routes:
- id: shop-goods # 路由 ID,唯一
uri: lb://shop-goods # 根据服务名称从注册中心获取服务请求地址
predicates: # 断言(判断条件)
# 匹配对应 URL 的请求,将匹配到的请求追加在目标 URI 之后
- Path=/goods/**,/api-gateway/**
filters: # 网关过滤器
# 将/api-gateway/goods/test 重写成 /goods/test
- RewritePath=/api-gateway(?<segment>/?.*),$\{
segment}
2.1.2 PrefixPath
请求时,为 URL
添加指定的前缀
例如:http://localhost/test重写为http://localhost/goods/test
server:
port: 7000
spring:
application:
name: gateway-server # 应用名称
cloud:
gateway:
discovery:
locator:
# 是否与服务发现组件进行结合,通过 serviceId 转发到具体的微服务
enabled: true #是否开启基于服务发现得路由规则
lower-case-service-id: true # 是否将服务名称转小写
routes:
- id: shop-goods # 路由 ID,唯一
uri: lb://shop-goods # 根据服务名称从注册中心获取服务请求地址
predicates: # 断言(判断条件)
# 匹配对应 URL 的请求,将匹配到的请求追加在目标 URI 之后
- Path=/**
filters: # 网关过滤器
# 将 /test 重写成 /goods/test
- PrefixPath=/goods
nacos:
# 注册中心配置
discovery:
server-addr: localhost:8848 # nacos 注册中心地址
namespace: 0e883794-4414-4d41-a177-6d335d304e42 # 开发环境 指定 具体的命名空间id
#group: DEFAULT_GROUP # 分组 默认分组可以省略
# 配置中心配置
config:
#enabled: false #关闭配置
server-addr: localhost:8848 # Nacos