SpringCloud学习(六)使用gateway作为服务网关-predicate
1、搭建spring-cloud-gateway工程
配置pom信息
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.liulin</groupId>
<artifactId>spring-cloud-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-gateway</name>
<description>SpringCloud聚合学习gateway</description>
<parent>
<groupId>cn.liulin</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>spring-cloud-integration</artifactId>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
将application.properties替换为application.yml
server:
port: 8766
eureka:
client:
service-url:
defaultZone: http://localhost:9999/eureka/
spring:
application:
name: spring-cloud-gateway
2、添加自定义路由
3、启动eureka、client及gateway工程
http://127.0.0.1:8766/get
成功访问!说明路由起作用了
4、常见路由断言配置规则
server:
port: 8766
eureka:
client:
service-url:
defaultZone: http://localhost:9999/eureka/
spring:
application:
name: spring-cloud-gateway
profiles:
#加载具体的配置文件
active: remote_route
---
#1.All 路由断言 Factory
spring:
cloud:
gateway:
routes:
- id: after_time_route
#转发路径
uri: http://127.0.0.1:8764/getInfo
#根据ZonedDateTime now = ZonedDateTime.now()获取默认时区
predicates:
- After=2021-02-22T11:06:05.502+08:00[Asia/Shanghai]
- id: before_time_route
#转发路径
uri: http://127.0.0.1:8764/getInfo
#根据ZonedDateTime now = ZonedDateTime.now()获取默认时区
predicates:
- Before=2021-02-22T11:17:05.502+08:00[Asia/Shanghai]
profiles: all_route
---
#2.After 路由断言 Factory
#在该日期时间之后发生的请求都将被匹配。
spring:
cloud:
gateway:
routes:
- id: after_time_route
#转发路径
uri: http://127.0.0.1:8764/getInfo
#根据ZonedDateTime now = ZonedDateTime.now()获取默认时区
predicates:
- After=2021-02-22T11:06:05.502+08:00[Asia/Shanghai]
profiles: after_time_route
---
#3.Before 路由断言 Factory
#在该日期时间之前发生的请求都将被匹配。
spring:
cloud:
gateway:
routes:
- id: before_time_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Before=2021-02-22T11:10:05.502+08:00[Asia/Shanghai]
profiles: before_time_route
---
#4.Between 路由断言 Factory
#在datetime1和datetime2之间的请求将被匹配。
spring:
cloud:
gateway:
routes:
- id: between_time_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Between=2021-02-22T11:10:05.502+08:00[Asia/Shanghai], 2021-02-22T11:20:05.502+08:00[Asia/Shanghai]
profiles: between_time_route
---
#5.Cookie 路由断言 Factory
#请求包含次cookie名称且正则表达式为真的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Cookie=chocolate, ch.p
profiles: cookie_route
---
#6.Header 路由断言 Factory
#请求包含次header名称且正则表达式为真的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Header=X-Request-Id, \d+
profiles: header_route
---
#7.Host 路由断言 Factory
#Host 路由断言 Factory包括一个参数:host name列表。使用Ant路径匹配规则,.作为分隔符。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Host=localhost,127.0.0.1
profiles: host_route
---
#7.Method 路由断言 Factory
#Method 路由断言 Factory只包含一个参数: 需要匹配的HTTP请求方式
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://127.0.0.1:8764/getInfo
predicates:
- Method=GET
profiles: method_route
---
#8.Path 路由断言 Factory
#Path 路由断言 Factory 有2个参数: 一个Spring PathMatcher表达式列表和可选matchOptionalTrailingSeparator标识 .
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://127.0.0.1:8764/getInfo
predicates:
#127.0.0.1:8766/foo/1
- Path=/foo/{segment},/bar/{segment}
profiles: path_route
---
#9 Query 路由断言 Factory
#Query 路由断言 Factory 有2个参数: 必选项 param 和可选项 regexp.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://127.0.0.1:8764/getInfo
predicates:
#127.0.0.1:8766?foo=baz
- Query=foo, ba.
profiles: query_route
---
#10 RemoteAddr 路由断言 Factory
#RemoteAddr 路由断言 Factory的参数为 一个CIDR符号(IPv4或IPv6)字符串的列表,最小值为1,例如192.168.0.1/16(其中192.168.0.1是IP地址并且16是子网掩码)。
spring:
cloud:
gateway:
routes:
- id: remote_route
uri: http://127.0.0.1:8764/getInfo
predicates:
#转发本地访问请求
- RemoteAddr=127.0.0.1/24
profiles: remote_route
---
#10 RemoteAddr 路由断言 Factory
#RemoteAddr 路由断言 Factory的参数为 一个CIDR符号(IPv4或IPv6)字符串的列表,最小值为1,例如192.168.0.1/16(其中192.168.0.1是IP地址并且16是子网掩码)。
spring:
cloud:
gateway:
routes:
- id: remote_route
uri: http://127.0.0.1:8764/getInfo
predicates:
#只允许本地访问
- RemoteAddr=127.0.0.1/24
profiles: remote_route