1.依赖坐标
< 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>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-loadbalancer</ artifactId>
</ dependency>
2.路由属性
网关路由对应的Java类型是RouteDefinition,其中常见的属性有
属性 描述 id 路由唯一标识 uri 路由目标地址 predicates 路由断言,判断请求是否符合当前路由 filter 路由过滤器,对请求或响应做特殊处理
3.常用路由过滤器
过滤器 说明 AddRequestHeader 给当前请求添加一个请求头 RemoveRequestHeader 移除请求中的一个请求头 AddResponseHeader 给响应结果中添加一个响应头 RemoveResponseHeader 移除响应结果中的一个响应头 RewritePath 请求路径重写 StripPrefix 去除请求路径中的N段前缀
4.常见路由断言
名称 说明 After 是某个时间点后的请求 Before 是某个时间点之前的请求 Between 是某两个时间点之间的请求 Cookie 请求必须包含某些cookie Header 请求必须包含某些header Host 请求必须是访问某个域名 Method 请求方式必须是指定方式 Path 请求路径必须符合指定规则 Query 请求参数必须包含指定参数 RomoteAddr 请求者的IP必须是指定范围 Weight 权重处理 XForwarded Remote Addr 基于请求的来源IP做判断
5.配置文件
spring :
application :
name : 服务名称
cloud :
nacos :
server-addr : 端口: IP
discovery :
cluster-name : 集群名称
namespace : 命名空间ID
ephemeral : true
gateway :
routes :
- id : 路由唯一标识
uri : 路由目标地址
predicates :
- Path=请求路径
- id : 路由唯一标识
uri : 路由目标地址
predicates :
- 请求路径