使用GRPC调用Google Routes API遇Unimplemented错误,求代码排查
问题排查与修复方案
核心错误原因
你遇到的Unimplemented错误,本质是自定义的gRPC服务/方法名和Google Routes API官方提供的完全不匹配,服务端自然找不到对应的实现。另外代码里还有冗余的本地服务创建逻辑,完全没必要。
具体问题点与修复步骤
1. Proto文件完全不符合官方定义
Google Routes API的ComputeRouteMatrix接口,官方gRPC定义的关键信息如下:
- 包名:
google.maps.routing.v2 - 服务名:
RoutesMatrixService - 方法名:
ComputeRouteMatrix - HTTP注解路径:
/v2:computeRouteMatrix(不需要带域名)
你自定义的bkgrpc包、Routes服务、ComputeRouteMatrixService方法都是无效的,必须修正为和官方一致的定义。
修正后的proto示例:
syntax = "proto3"; package google.maps.routing.v2; option go_package = "./proto/routing"; import "google/api/annotations.proto"; // 需导入Google官方定义的其他依赖proto,如LatLng、Waypoint、请求响应结构体等,建议直接使用官方提供的完整Routes API proto定义 service RoutesMatrixService { rpc ComputeRouteMatrix(ComputeRouteMatrixRequest) returns (ComputeRouteMatrixResponse) { option (google.api.http) = { post: "/v2:computeRouteMatrix" body: "*" }; } } // 请补充官方定义的ComputeRouteMatrixRequest、ComputeRouteMatrixResponse等所有相关结构体,不要自行定义
2. 删除冗余的本地gRPC Server代码
你在ComputeRoute函数里创建了grpc.NewServer()并注册自己的Server,这是完全错误的——你作为客户端是调用远程Google服务,不是在本地启动服务。直接删除以下代码:
// 以下代码全部删掉 s := grpc.NewServer() // Attach the google compute route matrix service to the server routepb.RegisterRoutesServer(s, &Server{})
3. 调用官方标准方法
修正proto后,客户端需调用官方标准的ComputeRouteMatrix方法,而非自定义的ComputeRouteMatrixService。
4. 确保请求结构体匹配官方定义
代码中使用的RouteMatrixOrigin、RouteMatrixDestinations等结构体,必须和官方proto定义完全一致,否则请求结构不匹配也会报错。建议直接使用Google官方生成的Go客户端代码,避免手写proto出错。
简化后的正确调用流程
- 获取Google官方提供的完整Routes API proto定义及依赖文件
- 使用
protoc生成对应的Go客户端代码 - 在Go代码中,通过生成的客户端调用
RoutesMatrixService的ComputeRouteMatrix方法 - 确保请求头的
X-Goog-Api-Key和X-Goog-Fieldmask参数正确设置
内容的提问来源于stack exchange,提问作者Sahildeep Kaur
相关产品推荐
相关产品推荐

