能否在Google API Gateway中通过通配符路径路由至多后端并移除前缀?
问题解答
完全可行,你只需要调整Google API Gateway的路径转换配置,通过strip_prefix_count参数去掉指定的前缀段即可实现需求。
正确配置示例
将你的原配置修改为以下形式:
paths: '/service_1/*': get: x-google-backend: address: https://service_1/ path_translation: STRIP_PREFIX strip_prefix_count: 1 '/service_2/*': get: x-google-backend: address: https://service_2/ path_translation: STRIP_PREFIX strip_prefix_count: 1
配置说明
path_translation: STRIP_PREFIX:指定网关需要从请求路径中移除指定数量的前缀段,再将剩余路径追加到后端地址。strip_prefix_count: 1:定义要移除的前缀段数量。当客户端请求/service_1/api/data时,网关会去掉第一个路径段service_1,把剩余的/api/data转发到后端https://service_1/,最终后端收到的请求路径就是/api/data。
通用化配置(可选)
如果不想逐个定义每个服务的路径规则,可以用路径变量实现更简洁的配置(前提是服务ID和后端域名一一对应):
paths: '/{service_id}/*': get: x-google-backend: address: https://{service_id}/ path_translation: STRIP_PREFIX strip_prefix_count: 1
内容的提问来源于stack exchange,提问作者David Beaudway
相关产品推荐
相关产品推荐

