如何配置端点和esp以使用启用的ServerReflection功能发布gRPC

时间:2018-07-20 19:49:58

标签: google-cloud-platform google-cloud-endpoints google-kubernetes-engine

我创建了一个简单的gRPC API服务,并使用GKE-ESP端点将其发布。有用。现在,我想发布具有ServerReflection功能的服务。反射已添加到gRPC服务器中,当某些grpc客户端(例如grpcurl)直接向服务器发送list请求时,服务器将返回正确的答案以及可访问的gRPC服务列表:

$ grpcurl --plaintext localhost:8000 list
grservices.diagnostic

当同一请求发送到负载均衡器的公共IP时,答案为Failed to list services: rpc error: code = NotFound desc = Method does not exist.

esp日志:

"POST /grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo HTTP/2.0" 404 0 "-" "grpc-go/1.14.0-dev"

端点日志:

Endpoints management skipped for an unrecognized HTTP call: POST /grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo

对我来说,原因似乎是因为endpoints不了解grpc.reflection.v1alpha.ServerReflection服务,并且不允许将请求路由到gRPC容器。我查看了所有可以谷歌搜索的示例,没有发现启用了api-config.yaml和没有ServerReflection的gRPC在ServerReflection个文件之间没有任何更改。

不幸的是,我尝试更改api_config.yaml并将其应用于部署没有成功,因为我应该为{{grpc.reflection.v1alpha.ServerReflection}}使用描述符{{.pb}}文件。 / p>

所以我的问题是如何配置端点和esp以提供reflection

非常感谢任何想法和建议。

api_config.yaml:

type: google.api.Service
config_version: 3

#
# Name of the service configuration.
#
name: diagnostic.endpoints.[id-project].cloud.goog

#
# API title to appear in the user interface (Google Cloud Console).
#
title: diagnostic gRPC API
apis:
- name: grservices.diagnostic
# -name: grpc.reflection.v1alpha.ServerReflection

#
# API usage restrictions.
#
#usage:
#  rules:
#  # Diagnostic methods can be called without an API Key.
#  - selector: grservices.diagnostic.GetDateTime
#    allow_unregistered_calls: true
#
#  - selector: grservices.diagnostic.GetTimeZones
#    allow_unregistered_calls: true
#
##  - selector: grpc.reflection.v1alpha.ServerReflection.ServerReflectionInfo
##    allow_unregistered_calls: true

usage:
  rules:
  # Allow unregistered calls for all methods.
  - selector: "*"
    allow_unregistered_calls: true

2 个答案:

答案 0 :(得分:0)

浏览器希望HTTP响应包含有效的CORS标头。但是后端是gRPC,它无法处理CORS。

您可以找到自己的解决方案Here

答案 1 :(得分:0)

解决方案非常简单。我刚刚下载了reflection.proto,并使用.pb和我自己的服务reflection.proto文件编译了新的proto文件:

wget https://raw.githubusercontent.com/grpc/grpc/master/src/proto/grpc/reflection/v1alpha/reflection.proto

python -m grpc.tools.protoc --include_imports --include_source_info --proto_path=.  --proto_path="../googleapis" --python_out=. --grpc_python_out=. --descriptor_set_out=api_descriptor.pb reflection.proto diagnostic.proto

# delete _pb2.py files of reflection. The files are already presented in virtenv as the part of grpcio-reflection package
rm reflection_pb2.py
rm reflection_pb2_gprc.py

# upload the new config to endpoints
gcloud endpoints services deploy api_descriptor.pb api_config.yaml

测试:

$ grpcurl -insecure ${EXT_IP}:443 list                      
grservices.diagnostic

$ grpcurl -insecure ${EXT_IP}:443 list grservices.diagnostic
GetDateTime
GetTimeZones

grpcurl -insecure ${EXT_IP}:443 grservices.diagnostic.GetDateTime
{
  "datetime": "2018-07-23 17:21:16"
}

$ grpcurl -insecure ${EXT_IP}:443 grservices.diagnostic/GetdateTime

$ grpcurl -d '{"time_zone": "Israel"}' -insecure ${EXT_IP}:443 grservices.diagnostic.GetDateTime
{
  "datetime": "2018-07-23 20:25:41Z+0300"
}

api_config.yaml文件的更改部分:

apis:
- name: grservices.diagnostic
- name: grpc.reflection.v1alpha.ServerReflection