写配置类的方式配置路由
package com.caq.cloud.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route(“path_route_caq”,
r -> r.path(“/guonei”)
.uri(“http://news.baidu.com/guonei”)).build();
return routes.build();
}
}
locator
n. 定位器,探测器
默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能(不写死一个地址)。
启动
-
eureka7001
-
payment8001/8002
YML
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。
server:
port: 9527
spring:
application:
name: cloud-gateway
#############################新增网关配置###########################
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
-
Path=/payment/get/** # 断言,路径相匹配的进行路由
-
id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/** # 断言,路径相匹配的进行路由
####################################################################
eureka:
instance:
hostname: cloud-gateway-service
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
测试
浏览器输入 - http://localhost:9527/payment/lb
predicate
美: ['predɪkeɪt] 英: ['predɪkət]
v. 断言;使基于;使以…为依据;表明
adj. 述语的;谓项的
n. 谓语(句子成分,对主语加以陈述,如 John went home 中的 went home)
Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。
Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate工厂可以进行组合。
Spring Cloud Gateway创建Route 对象时,使用RoutePredicateFactory 创建 Predicate对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories。
所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。
常用的Route Predicate Factory
-
The After Route Predicate Factory
-
The Before Route Predicate Factory
-
The Between Route Predicate Factory
-
The Cookie Route Predicate Factory
-
The Header Route Predicate Factory
-
The Host Route Predicate Factory
-
The Method Route Predicate Factory
-
The Path Route Predicate Factory
-
The Query Route Predicate Factory
-
The RemoteAddr Route Predicate Factory
-
The weight Route Predicate Factory
The After Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
这个时间后才能起效
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
The Between Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: between_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]
The Cookie Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
测试
该命令相当于发get请求,且没带cookie
curl http://localhost:9527/payment/lb
带cookie的
curl http://localhost:9527/payment/lb --cookie “chocolate=chip”
The Header Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
测试
带指定请求头的参数的CURL命令
curl http://localhost:9527/payment/lb -H "X-Request-Id:123
…
总结
Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。