如何在SpringCloudGateway中为发现定位器编写复杂谓词?
我想自定义发现定位器的行为。例如,在我的案例中,有一个是从如何在SpringCloudGateway中为发现定位器编写复杂谓词?,spring,spring-cloud,spring-cloud-gateway,Spring,Spring Cloud,Spring Cloud Gateway,我想自定义发现定位器的行为。例如,在我的案例中,有一个是从网关\u主机/禁止路由到名为禁止ui的服务。为此,我使用以下配置: spring: cloud: gateway: discovery: locator: enabled: true lower-case-service-id: true filters: PreserveHostHeader include-expre
网关\u主机/禁止
路由到名为禁止ui
的服务。为此,我使用以下配置:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
filters: PreserveHostHeader
include-expression: serviceId.endsWith('-UI')
predicates: Path='/'+serviceId.substring(0,serviceId.indexOf('-UI'))+'/**'
然后错误:
Failed to bind properties under 'spring.cloud.gateway.discovery.locator.predicates' to java.util.List<org.springframework.cloud.gateway.handler.predicate.PredicateDefinition>:
Reason: failed to convert java.lang.String to org.springframework.cloud.gateway.handler.predicate.PredicateDefinition
无法将“spring.cloud.gateway.discovery.locator.predicates”下的属性绑定到java.util.List:
原因:未能将java.lang.String转换为org.springframework.cloud.gateway.handler.predicate.PredicateDefinition
我认为这个错误是由于传递给substring
方法的两个参数引起的。若我将方法调用更改为子字符串(0)
,则应用程序将成功启动,但这样的配置对我来说毫无意义:
谓词:Path='/'+serviceId.substring(0)+'/**'
属性spring.cloud.gateway.discoverycatorproperties
引用谓词定义列表,请参见org.springframework.cloud.gateway.discoverycatorproperties
。您指定的内容已转换为字符串
,因此无法转换为所需类型
您可以尝试按如下方式指定谓词:
spring:
cloud:
gateway:
discovery:
locator:
predicates:
- name: Path
args:
pattern: '/'+serviceId.substring(0,serviceId.indexOf('-UI'))+'/**'