Spring Cloud Gateway
概念解释:
Spring Cloud GateWay 工作流程如下所示: 客户端向 开始使用
在本章会侧重针对配置文件方式进行讲解,当然 添加依赖 添加 //...省略部分内容<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <!--Spring Cloud Gateway--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>//...省略部分内容 Spring Cloud Gateway Predicates 在我们开始本章内容之前我们要来先了解下 每一个 Path 方式匹配转发 通过 配置文件匹配地址转发 我们在 spring: application: name: spring-cloud-gateway-sample cloud: gateway: routes: - id: blog uri: http://blog. predicates: # 匹配路径转发 - Path=/api-boot-datasource-switch.html# 端口号server: port: 9090 先来解释下
在上面的配置中,当访问 访问效果如下所示: RouteLocator 匹配路径转发 在上面的配置中,如果使用 如下所示: @Beanpublic RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route('blog', r -> r.path('/api-boot-datasource-switch.html').uri('http://blog.')) .build();} Before 方式匹配转发 当部署有访问时间限制的接口时,我们可以通过 spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Before=2019-05-01T00:00:00+08:00[Asia/Shanghai] 在上面配置中,我们允许 After 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - After=2019-04-29T00:00:00+08:00[Asia/Shanghai] 在上面配置中允许 Between 方式匹配转发 那如果是一个时间段内允许请求转发,通过 spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Between=2019-04-29T00:00:00+08:00[Asia/Shanghai], 2019-05-01T00:00:00+08:00[Asia/Shanghai] 在上面配置中,允许在 Cookie 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Cookie=hengboy, yuqiyu 在上面配置中,如果客户端发送请求时携带了 测试Cookie方式转发: curl http://localhost:9090 --cookie 'hengboy=yuqiyu' 通过上面方式我们是可以成功转发请求的,如果我们修改 Header 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Header=X-Request-Id, \d+ 在上面配置中,如果 curl http://localhost:9090 -H 'X-Request-Id:123456' 如果头信息为 Host 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Host=**. 测试如下所示: 1. curl http://localhost:9090 -H 'Host: ' // 匹配2. curl http://localhost:9090 -H 'Host: api.' // 匹配3. curl http://localhost:9090 -H 'Host: admin.' // 匹配3. curl http://localhost:9090 -H 'Host: hengboy.com' // 不匹配 请求方式 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Method=POST 发送 ~ curl http://localhost:9090{'timestamp':'2019-04-29T06:27:41.121+0000','path':'/','status':404,'error':'Not Found','message':null} 我们的请求并未被 curl -X POST http://localhost:9090 是可以被转发到目标地址 请求参数 方式匹配转发
请求中存在 spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Query=xxx 我们通过 请求中存在 spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Query=xxx, zzz 根据上面配置,我们限定了参数 请求路径 方式匹配转发
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Path=/article/{articleId} 在上面配置中 ~ curl http://localhost:9090/article/1 // 匹配~ curl http://localhost:9090/article/abc // 匹配~ curl http://localhost:9090/article/1/1 // 不匹配
spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - RemoteAddr=192.168.1.56/24 在上面我们配置了 组合示例 相同的 spring: cloud: gateway: routes: - id: blog uri: http://blog. predicates: - Query=author, hengboy - Query=yuqiyu - Method=GET - Cookie=hengboy, yuqiyu - Header=X-Request-Id, \d+ - RemoteAddr=192.168.1.56/24 总结 本章节讲解了 源码位置
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。 |
|
来自: jackeyqing > 《待分类》