官网:https://spring.io/projects/spring-cloud-gateway#learn
依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
配置样例:
spring:
cloud:
gateway:
routes:
- id: cainiao_route
uri: https://www.runoob.com/
predicates:
# 如果有名为 cainiao 的参数就跳转到 https://www.runoob.com/
- Query=cainiao
- id: baidu_route
uri: https://www.baidu.com/
predicates:
# 如果有名为 cainiao 的参数 并且值为 baidu 就跳转到 https://www.baidu.com/
- Query=url,baidu
- id: product_route
# lb: Load Balance 负载均衡 去注册中心 找名为 gulimall-product 的服务
uri: lb://gulimall-product
predicates:
- Path=/api/product/**
filters:
# 过滤掉 /api 保留后部分
# eg: localhost:88/api/product/brand/list -> gulimall-product/product/brand/list
# 网关去注册中心找实际地址 -> localhost:10000/product/brand/list
- RewritePath=/api/(?<segment>.*), /$\{segment}
- id: third_party_route
uri: lb://gulimall-third-party
predicates:
- Path=/api/thirdparty/**
filters:
- RewritePath=/api/thirdparty/(?<segment>.*), /$\{segment}
- id: member_route
uri: lb://gulimall-member
predicates:
- Path=/api/member/**
filters:
- RewritePath=/api/(?<segment>.*), /$\{segment}
- id: ware_route
uri: lb://gulimall-ware
predicates:
- Path=/api/ware/**
filters:
- RewritePath=/api/(?<segment>.*), /$\{segment}
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
# 过滤 /api 并且在后部分的前面 拼接 /renren-fast
- RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}