13.5、路由配置
分类: Gateway API网关
路由配置
路由配置是 Gateway 的核心功能。本节将学习如何配置路由。
本节将学习:静态路由、动态路由、路由规则,以及路由优先级。
在商城项目中配置路由
完整的路由配置
文件路径: mall-microservices/gateway-service/src/main/resources/application.yml
spring: cloud: gateway: routes: # 用户服务路由 - id: user-service-route uri: lb://user-service predicates: - Path=/api/users/** filters: - StripPrefix=1 order: 1 # 商品服务路由 - id: product-service-route uri: lb://product-service predicates: - Path=/api/products/** filters: - StripPrefix=1 order: 2 # 订单服务路由 - id: order-service-route uri: lb://order-service predicates: - Path=/api/orders/** filters: - StripPrefix=1 order: 3 # 支付服务路由(需要认证) - id: payment-service-route uri: lb://payment-service predicates: - Path=/api/payments/** - Method=POST,GET filters: - StripPrefix=1 # 后续会添加认证过滤器 order: 4 # 库存服务路由(内部服务,不对外暴露) - id: inventory-service-route uri: lb://inventory-service predicates: - Path=/api/inventory/** - Header=X-Internal-Request, true filters: - StripPrefix=1 order: 5
路由配置说明
商城项目路由设计:
- 用户服务:
/api/users/**→user-service - 商品服务:
/api/products/**→product-service - 订单服务:
/api/orders/**→order-service - 支付服务:
/api/payments/**→payment-service(需要认证) - 库存服务:
/api/inventory/**→inventory-service(内部服务)
动态路由配置
使用代码配置路由
文件路径: mall-microservices/gateway-service/src/main/java/com/mall/gateway/config/GatewayConfig.java
package com.mall.gateway.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 builder) { return builder.routes() // 用户服务路由 .route("user-service", r -> r .path("/api/users/**") .filters(f -> f.stripPrefix(1)) .uri("lb://user-service")) // 商品服务路由 .route("product-service", r -> r .path("/api/products/**") .filters(f -> f.stripPrefix(1)) .uri("lb://product-service")) // 订单服务路由 .route("order-service", r -> r .path("/api/orders/**") .filters(f -> f.stripPrefix(1)) .uri("lb://order-service")) // 支付服务路由(需要认证) .route("payment-service", r -> r .path("/api/payments/**") .and() .method("POST", "GET") .filters(f -> f.stripPrefix(1)) .uri("lb://payment-service")) .build(); } }
路由优先级配置
在商城项目中的路由优先级:
spring: cloud: gateway: routes: # 高优先级:精确匹配 - id: user-login-route uri: lb://user-service predicates: - Path=/api/users/login order: 1 # 中优先级:用户服务其他接口 - id: user-service-route uri: lb://user-service predicates: - Path=/api/users/** order: 2 # 低优先级:默认路由 - id: default-route uri: lb://user-service predicates: - Path=/** order: 100
路由规则详解
Path 规则
Path 规则示例:
predicates: - Path=/api/users/**, /api/users/{id}
Method 规则
Method 规则示例:
predicates: - Method=GET,POST
Header 规则
Header 规则示例(用于内部服务调用):
predicates: - Header=X-Internal-Request, true
Query 规则
Query 规则示例:
predicates: - Query=version, v1
组合规则
商城项目中的组合规则示例:
# 支付服务路由:需要 POST 方法且路径匹配 - id: payment-service-route uri: lb://payment-service predicates: - Path=/api/payments/** - Method=POST - Header=Authorization, Bearer.* filters: - StripPrefix=1
路由测试
测试路由配置:
# 测试用户服务路由 curl http://localhost:8080/api/users/1 # 测试商品服务路由 curl http://localhost:8080/api/products?page=1&size=10 # 测试订单服务路由 curl -X POST http://localhost:8080/api/orders \ -H "Content-Type: application/json" \ -d '{"userId": 1, "totalAmount": 100.00}' # 测试支付服务路由(需要认证) curl -X POST http://localhost:8080/api/payments \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"orderId": 1, "amount": 100.00}'
路由优先级
优先级说明
路由优先级:
- 使用 order 属性
- 数字越小优先级越高
- 匹配第一个路由
官方资源
- Spring Cloud Gateway 路由配置:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-route-filters
本节小结
在本节中,我们学习了:
第一个是静态路由。 使用 YAML 配置路由。
第二个是动态路由。 使用代码配置路由。
第三个是路由规则。 路由匹配规则。
第四个是路由优先级。 路由的优先级设置。
这就是路由配置。合理配置路由,可以实现灵活的服务转发。
在下一节,我们将学习断言配置。