首页 > 解决方案 > Google Cloud Endpoints (ESP) gRPC 转码为驼峰式

问题描述

我已按照此处的说明使用 Google Cloud Endpoints / ESP 部署了 gRPC 服务器:

https://cloud.google.com/endpoints/docs/grpc/get-started-kubernetes-engine

在我的 proto 文件中,我的字段按照 Protocol Buffers 命名约定 ( https://developers.google.com/protocol-buffers/docs/style#message-and-field-names ) 以 snake_case 命名,如下所示:

message MyMessage {
  string my_field = 1;
}

部署到 Cloud Endpoints 时,字段名称将转换为 camelCase。因此,如果我使用 HTTP 端点调用 API,JSON 响应如下所示:

{
  "myField":"field value"
}

如果我使用 GRPC 客户端 (Node.js) 调用服务,则会以原始格式返回响应对象:

{
  "my_field":"field value"
}

在 Cloud Endpoints Developer Portal 文档中,字段名称也会转换为驼峰式。

在替换现有 API 时,我更喜欢对 GRPC 和 HTTP 客户端使用蛇形案例。如果我可以防止字段名称被转换,我基本上可以将后端切换到新服务,而无需对客户端进行任何更改。

有没有办法强制 ESP 在转码为 HTTP/JSON 时使用驼峰式字段名称?

标签: kubernetesgoogle-cloud-platformgoogle-kubernetes-enginegoogle-cloud-endpointsgrpc

解决方案


我正在使用 ESP 将我的 grpc 应用程序部署到 k8s 中,下面的配置会将 protobuf 响应转码为蛇案例而不是骆驼案例

- name: esp
    image: gcr.io/endpoints-release/endpoints-runtime:1
    imagePullPolicy: Always
    args: [
      "--http_port=9000",
      "--backend=grpc://127.0.0.1:8080",
      "--service=xxxxxx",
      "--version=xxxxxx",
      "--transcoding_preserve_proto_field_names",
      "--transcoding_always_print_primitive_fields"
    ]
    ports:
    - name: http
      containerPort: 9000

推荐阅读