Java 如何使用服务发现重写Spring云网关的serviceId
我正在将zuul gateway迁移到SCG。我的服务在kubernetes运行,并通过领事注册。 典型的服务名称是xxx服务。因此,使用当前网关配置,我可以通过 我的当前配置:Java 如何使用服务发现重写Spring云网关的serviceId,java,spring-cloud-gateway,Java,Spring Cloud Gateway,我正在将zuul gateway迁移到SCG。我的服务在kubernetes运行,并通过领事注册。 典型的服务名称是xxx服务。因此,使用当前网关配置,我可以通过 我的当前配置: spring: cloud: gateway: discovery: locator: enabled: true lower-case-service-id: true predicates:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
predicates:
- name: Path
args:
pattern: "'/api/' + serviceId + '/**'"
filters:
- name: RewritePath
args:
regexp: "'/api/' + serviceId + '/(?<remaining>.*)'"
replacement: "'/${remaining}'"
我处于完全相同的位置。你找到解决这个问题的诀窍了吗? 我知道我们可以在RouteLocator中定义所有路线,如下所示:
/**
* Route constructions.
*
* @param builder
* the builder instance
* @return the final route locator
*/
@Bean
public RouteLocator gatewayRoutes(final RouteLocatorBuilder builder) {
return builder.routes()
// Declare First service
.route(r -> r.path("/prefix/myservice1-service/**")
.filters(f -> f.rewritePath("/prefix/myservicename1-service/(?<remaining>.*)", "/${remaining}"))
.uri("lb://myservice1").id("myservice1"))
// Declare Second service
.route(r -> r.path("/prefix/myservice2-service/**")
.filters(f -> f.rewritePath("/prefix/myservicename2-service/(?<remaining>.*)", "/${remaining}"))
.uri("lb://myservice2").id("myservice2"))
// Etc... Then build
.build();
}
但如果能找到一个不那么脏的解决方案,那就太好了
/**
* Route constructions.
*
* @param builder
* the builder instance
* @return the final route locator
*/
@Bean
public RouteLocator gatewayRoutes(final RouteLocatorBuilder builder) {
return builder.routes()
// Declare First service
.route(r -> r.path("/prefix/myservice1-service/**")
.filters(f -> f.rewritePath("/prefix/myservicename1-service/(?<remaining>.*)", "/${remaining}"))
.uri("lb://myservice1").id("myservice1"))
// Declare Second service
.route(r -> r.path("/prefix/myservice2-service/**")
.filters(f -> f.rewritePath("/prefix/myservicename2-service/(?<remaining>.*)", "/${remaining}"))
.uri("lb://myservice2").id("myservice2"))
// Etc... Then build
.build();
}
spring:
cloud:
gateway:
discovery:
locator:
enabled: false