组团学

路由(Route)

阅读 (5455648)

1、认识路由的谓词接口和谓词工厂

在Java 8 中引入了一个函数接口Predicate,它接收一个输入参数并返回一个布尔值结果。

Spring Cloud Gateway通过Predicate接口组合简单条件来判断当前路由是否满足给定条件。该接口包含许多默认方法,用于将Predicate组合成复杂逻辑,以校验请求参数、判断数据是否改变、是否需要更新等。

使用路由的流程:

1、加载路由中的Predicate

2、用Predicate判断路由是否可用

2、认识配置路由规则的方式

2.1、通过Java API的方式

直接通过RouteLocatorBuilder接口来构建路由规则。

@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){ return builder.routes().route("hello", r->r.path("/hello/**") .uri("http://localhost:500010")) .build(); }

2.2、通过配置文件的方式

可以通过application.properties或application.yml来配置路由规则。

1、application.yml

server: port: 50010 spring: cloud: gateway: routes: - id: hello uri: http://localhost:50010 predicates: - Path=/hello/**

2、application.properties

#id:自定义路由ID
spring.cloud.gateway.routes[0].id=hello
#uri:目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50010
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello/**

3、配置文件方式构建路由

3.1、新建工程,添加依赖

<dependencies> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>

3.2、在配置文件中配置路由

spring.application.name=gateway-java-api
server.port=50010
#id:自定义路由ID
spring.cloud.gateway.routes[0].id=hello
#uri:目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50009
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

说明:

此处配置了一个路由ID为"hello"的路由规则,其作用是:当请求访问http://localhost:50010/hello时,将请求转发到http://localhost:50009/hello

3.3、启动"服务提供者"和网关工程

访问http://localhost:50010/hello

1589206759790.png

4、通过Java API方式配置路由

4.1、修改"服务提供者"的HelloController

@RestController public class HelloController { @Value("${provider.name}") private String name; @GetMapping("/hello") public String hello(){ return name; } @GetMapping("/hello2") public String hello2(){ return name; } }

4.2、修改网关工程的application.properties

spring.application.name=gateway-java-api
server.port=50010
#id:自定义路由ID
#spring.cloud.gateway.routes[0].id=hello
#uri:目标服务地址
#spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
#spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

4.3、添加Java API方式配置

@SpringBootApplication public class GatewayJavaApiApplication { public static void main(String[] args) { SpringApplication.run(GatewayJavaApiApplication.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){ //配置路由id为hell02,转发到http://localhost:50021/hello return builder.routes().route("hello2", r->r.path("/hello") .uri("http://localhost:50021")) .build(); } }

4.4、启动"服务提供者"和网关工程

访问http://localhost:50010/hello2

1589207340087.png

5、Spring Cloud Gateway的11种路由规则

Spring Cloud Gateway通过RoutePredicateFactory创建Predicate。Spring Cloud Gateway预置了很多RoutePredicateFactory,进行简单的配置即可得到想要的路由规则(Predicate)。这些路由规则会根据HTTP请求的不同属性来进行匹配。多个路由规则可以通过逻辑进行组合。

Spring Cloud Gateway预置的规则如下:

谓词工厂 类型 说明
AfterRoutePredicateFactory datetime 请求时间满足在配置时间之后
BeforeRoutePredicateFactory datetime 请求时间满足在配置时间之前
BetweenRoutePredicateFactory datetime 请求时间满足在配置时间之间
CookieRoutePredicateFactory Cookie 请求指定Cookie正则匹配指定值
HeaderRoutePredicateFactory Header 请求指定Headers正则匹配指定值
CloudFoundryRouteServiceRoutePredicateFactory Header 请求指定Headers是否包含指定名称
MethodRoutePredicateFactory Method 请求Method匹配配置的Methods
PathRoutePredicateFactory Path 请求路径正则匹配指定值
QueryRoutePredicateFacotry Queryparam 请求查询参数正则匹配指定值
请求Method匹配配置的Methods Remoteaddr 请求远程地址匹配配置指定值
HostRoutePredicateFactory Host 请求Host匹配指定值

5.1、Path路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[0].id=hello
#uri:目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

5.2、After路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[2].id=after_route
#uri:目标服务地址
spring.cloud.gateway.routes[2].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[2].predicates[0]=After=时间

5.3、Before路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[1].id=before_route
#uri:目标服务地址
spring.cloud.gateway.routes[1].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[1].predicates[0]=Before=时间

5.4、Between路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[3].id=between_route
#uri:目标服务地址
spring.cloud.gateway.routes[3].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[3].predicates[0]=Between=时间,时间

5.5、Cookie路由谓词工厂

Cookie路由谓词工厂采用两个参数:Cookie名称和正则表达式。此谓词工厂匹配具有给定名称的Cookie,值与正则表达式匹配。

#id:自定义路由ID
spring.cloud.gateway.routes[4].id=cookie_route
#uri:目标服务地址
spring.cloud.gateway.routes[4].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[4].predicates[0]=Cookie=name,long

5.6、Header路由谓词工厂

Header路由谓词工厂采用两个参数:Header名称和正则表达式。此谓词工厂用正则表达式匹配Header。

#id:自定义路由ID
spring.cloud.gateway.routes[5].id=header_route
#uri:目标服务地址
spring.cloud.gateway.routes[5].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[5].predicates[0]=Header=X-Request-Id,\d+

5.7、Host路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[6].id=host_route
#uri:目标服务地址
spring.cloud.gateway.routes[6].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[6].predicates[0]=Host=**.tyschool.com

5.8、Method路由谓词工厂

#id:自定义路由ID
spring.cloud.gateway.routes[7].id=method_route
#uri:目标服务地址
spring.cloud.gateway.routes[7].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[7].predicates[0]=Method=GET

5.9、Query路由谓词工厂

Query路由谓词工厂包含1个必需的参数和1个可选的正则表达式。

#id:自定义路由ID
spring.cloud.gateway.routes[8].id=query_route
#uri:目标服务地址
spring.cloud.gateway.routes[8].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[8].predicates[0]=Query=name
#?name=x&id=1
#Query=name,lon.
#其中.代表一个字符
#?name=log

5.10、RemoteAddr路由谓词工厂

RemoteAddr路由谓词工厂采用CIDR符号(IPv4划IPv6)字符串列表(最小值为1),例如:192.168.0.1/16

#id:自定义路由ID
spring.cloud.gateway.routes[9].id=ip_route
#uri:目标服务地址
spring.cloud.gateway.routes[9].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[9].predicates[0]=RemoteAddr=192.168.1.1/255

5.11、Weight路由谓词工厂

Weight路由谓词工厂是路由权重的,在配置时需要指定分组和权重。

#id:自定义路由ID
spring.cloud.gateway.routes[10].id=weight_route1
#uri:目标服务地址
spring.cloud.gateway.routes[10].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[10].predicates[0]=Path=/hello
spring.cloud.gateway.routes[10].predicates[1]=Weight=Weight,4
#id:自定义路由ID
spring.cloud.gateway.routes[11].id=weight_route2
#uri:目标服务地址
spring.cloud.gateway.routes[11].uri=http://localhost:50021
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[11].predicates[0]=Path=/hello
spring.cloud.gateway.routes[11].predicates[1]=Weight=Weight,6
需要 登录 才可以提问哦