org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
1.18.4
provided
com.sun.xml.bind
jaxb-core
2.2.11
javax.xml.bind
jaxb-api
com.sun.xml.bind
jaxb-impl
2.2.11
org.glassfish.jaxb
jaxb-runtime
2.2.10-b140310.1920
javax.activation
activation
1.1.1
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
true
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
配置server:
port: 9002
eureka:
client:
serviceUrl: # eureka server的路径
defaultZone: http://democloudeurekaservera:8761/eureka/,http://democloudeurekaserverb:8762/eureka/ #把 eureka 集群中的所有 url 都填写了进来,也可以只写一台,因为各个 eureka server 可以同步注册表
instance:
#使用ip注册,否则会使用主机名注册了(此处考虑到对老版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
#自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{spring.application.name}{server.port}:@project.version@
spring:
application:
name: lagou-cloud-gateway
cloud:
gateway:
routes: # 路由可以有多个
- id: service-autodeliver-router # 我们自定义的路由 ID,保持唯一
#uri: http://127.0.0.1:8096 # 目标服务地址 自动投递微服务(部署多实例) 动态路由:uri配置的应该是一个服务名称,而不应该是一个具体的服务实例的地址
uri: lb://demo-service-autodeliver # gateway网关从服务注册中心获取实例信息然后负载后路由
predicates: # 断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
-
Path=/autodeliver/**
-
id: service-resume-router # 我们自定义的路由 ID,保持唯一
#uri: http://127.0.0.1:8081 # 目标服务地址
#http://localhost:9002/resume/openstate/1545132
#http://127.0.0.1:8081/openstate/1545132
uri: lb://demo-service-resume
断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。predicates:
- Path=/resume/**
filters:
- StripPrefix=1
上⾯这段配置的意思是,配置了⼀个 id 为 service-autodeliver-router 的路由规则,当向⽹关发起
请求
http://localhost:9002/autodeliver/checkAndBegin/1545132
请求会被分发路由到对应的微服务上。
GateWay路由规则详解
Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规则(通过Header、请求参数等作为条件)匹配到对应的路由。

时间点后匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
时间点前匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
时间区间匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
指定cookie正则匹配指定值
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- cookie=chocolate, ch.p
指定Header正则匹配指定值
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Header=X-Request-Id, d+
请求Host匹配指定值
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Host=.somehost.org,.anotherhost.org
请求Method匹配指定请求⽅式
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Method=GET,POST
请求路径正则匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
请求包含某参数
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Query=green
请求包含某参数并且参数值匹配正则表达式
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Query=red, gree.
远程地址匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
GateWay⽀持⾃动从注册中⼼中获取服务列表并访问,即所谓的动态路由
实现步骤如下
1)pom.xml中添加注册中⼼客户端依赖(因为要获取注册中⼼服务列表,eureka客户端已经引⼊)
2)动态路由配置
uri: lb://demo-service-resume
注意:动态路由设置时,uri 以 lb: // 开头( lb 代表从注册中⼼获取服务),后⾯是需要转发到的服务名称
GateWay过滤器
从过滤器⽣命周期(影响时机点)的⻆度来说,主要有两个pre和post:
-
pre:这种过滤器在请求被路由之前调⽤。我们可利⽤这种过滤器实现身份验证、在集群中选择 请求的微服务、记录调试信息等。
-
post:这种过滤器在路由到微服务以后执⾏。这种过滤器可⽤来为响应添加标准的 HTTPHeader、收集统计信息和指标、将响应从微服务发送给客户端等。
从过滤器类型的⻆度,Spring Cloud GateWay 的过滤器分为 GateWayFilter 和 GlobalFilter 两种
-
GateWayFilter :应⽤到单个路由路由上
-
GlobalFilter :应⽤到所有的路由上
在Gateway Filter可以去掉url中的占位后转发路由,⽐如
predicates:
- Path=/resume/**
filters:
- StripPrefix=1 # 可以去掉resume之后转发
请求过来时,判断发送请求的客户端的ip,如果在⿊名单中,拒绝访问⾃定义GateWay全局过滤器时,我们实现Global Filter接⼝即可,通过全局过滤器可以实现⿊⽩名单、限流等功能。



