谓词 Path · SpringCloud微服务实战 · 看云

8.谓词 Path

导航

Path的使用户方式: 输入两个参数:SpringPathMatcher表达式列表、【可选】matchOptionalTrailingSeparator标识

本节代码地址


8.1 应用配置

这条路线,如果请求路径是/user/*或、blue/**是可以转发的,例如匹配/user/1/blue/green

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: host_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: host_path_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - Path=/user/{segment},/blue/{segment}
  profiles: host_path_route

8.2 应用启动

Postman 测试localhost:8699/user/2
affe0ef5a99c1328957ff5f5717d473f_MD5.webp

将请求地址调整为

@RestController
@RequestMapping("test")
@Slf4j
public class RibbonController {

94b3ab6d99ead1a49b2acb55f4b3007e_MD5.webp