面试题答案
一键面试在Spring Cloud Gateway的application.yml
配置文件中,可以这样配置:
spring:
cloud:
gateway:
routes:
- id: user_route
uri: lb://user-service
predicates:
- Path=/api/user/**
上述配置中:
id
是路由的唯一标识。uri
使用lb://
前缀表示基于负载均衡(LoadBalancer)的方式转发到名为user-service
的微服务实例,这要求微服务注册在服务注册中心(如Eureka、Consul等)。predicates
中的Path
表示匹配以/api/user
开头的请求路径。