spring - 如何在 Spring Cloud Gateway 中为发现定位器编写复杂的谓词?
问题描述
我想自定义发现定位器行为。例如,在我的案例中,路由从gateway_host/prohibitions
名为prohibitions-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
我认为由于传递给substring
方法的两个参数而导致此错误上升。如果我将方法调用更改为substring(0)
然后应用程序成功启动,但这样的配置对我来说没有意义:
predicates: Path='/'+serviceId.substring(0)+'/**'
解决方案
该属性spring.cloud.gateway.discovery.locator.predicates
引用谓词定义列表,请参阅org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties
。您指定的内容将转换为 a String
,因此无法转换为所需的类型。
您可以尝试按如下方式指定谓词:
spring:
cloud:
gateway:
discovery:
locator:
predicates:
- name: Path
args:
pattern: '/'+serviceId.substring(0,serviceId.indexOf('-UI'))+'/**'
推荐阅读
- excel - 无法创建 Worksheet_FollowHyperlink VBA
- algorithm - 奇数算法
- python - 打印可以由一组 n 个字符组成的所有可能的长度为 k 的字符串返回 >n 个字符
- macos - 在 Mac mojavie 上通过 php7.1.33 与 home-brew 安装 php 7.3 时遇到问题
- javascript - 如何显示带有悬停效果标题的模态图像?
- javascript - 即使状态没有改变,反应也会改变受控输入值吗?
- c++ - 寻找一种算法,给我一个给定块大小作为输入的列表的所有分区
- python - 如何防止在新标签中打开烧瓶链接
- java - java、oracle中的错误,找不到ID,ResultSet错误
- java - Android Java 从 Firebase 获得每家餐厅的平均评分