gateway 转发时去掉前缀
时间: 2023-08-02 12:11:01 浏览: 200
您可以通过在Spring Cloud Gateway中使用StripPrefix过滤器来实现转发时去掉前缀。具体步骤如下:
1. 在application.yml或application.properties中配置路由规则,例如:
```
spring:
cloud:
gateway:
routes:
- id: my-service
uri: http://localhost:8080
predicates:
- Path=/my-service/**
filters:
- StripPrefix=1
```
2. 配置RouteLocator,例如:
```java
@Configuration
public class RouteConfiguration {
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p -> p
.path("/my-service/**")
.filters(f -> f.stripPrefix(1))
.uri("http://localhost:8080"))
.build();
}
}
```
上述示例中,我们在路由规则中设置了匹配路径为/my-service/**,并在过滤器中添加了StripPrefix过滤器,表示要去掉前缀(即/my-service),然后将请求转发到uri为http://localhost:8080的服务上。
注意:StripPrefix=1表示去掉一个前缀,如果需要去掉多个前缀,可以设置StripPrefix=2或更多。
阅读全文
相关推荐


















