【问题标题】:grpc-gateway endpoints priority ordergrpc-gateway 端点优先级顺序
【发布时间】:2018-08-15 19:39:06
【问题描述】:

我有这样定义的服务:

service Service {
    rpc SearchCategory(SearchCategoryRequest) returns (SearchCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/search"
        };
    }
    rpc GetCategory(GetCategoryRequest) returns (GetCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/{id.val}"
        };
    }
}

问题是,即使我调用search?q=MyQuery,它也会被GetCategory 方法捕获并尝试获取id 为search 的类别。

我想这是因为路径非常接近。有没有一种方法可以像在经典 Web 应用程序中那样定义路由的优先级?

谢谢

【问题讨论】:

  • 您找到解决方案了吗?

标签: routes grpc grpc-gateway


【解决方案1】:

这个问题已经有一段时间了,但我最近自己遇到了完全相同的问题并找到了解决方案。

所以基本上,您将端点放在 protobuf 服务定义中的低,它的优先级高。

对于您的情况,您需要交换两个端点,以便 SearchCategory 位于 GetGategory 之下,从而赋予它更高的优先级。

service Service {
    rpc GetCategory(GetCategoryRequest) returns (GetCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/{id.val}"
        };
    }
    rpc SearchCategory(SearchCategoryRequest) returns (SearchCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/search"
        };
    }
}

那么/v1/categories/search 应该可以工作。

【讨论】:

    【解决方案2】:

    不使用{id.val},使用{id}。并且会工作

    message GetCategory {
        string id = 1;
    }
    

    【讨论】:

    • 对不起,问题不是来自这里。它与端点的顺序有关。
    • @c4k 是的,我的错,我是为了重现掉期订单
    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多