使用 grpc 和 protobuf(生成的客户端)通过 HTTP 2 访问 Google Cloud Pubsub
Posted
技术标签:
【中文标题】使用 grpc 和 protobuf(生成的客户端)通过 HTTP 2 访问 Google Cloud Pubsub【英文标题】:Accessing Google Cloud Pubsub over HTTP 2 using grpc and protobuf (generated client) 【发布时间】:2016-03-23 16:14:27 【问题描述】:我正在尝试通过使用https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto 的服务定义来使用谷歌云发布订阅服务
我能够生成客户端代码并拥有以下代码来获取主题:
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(
GoogleCredentials.getApplicationDefault().createScoped(SCOPES),
ForkJoinPool.commonPool());
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
PublisherBlockingClient publisher = newBlockingStub(channel);
Topic topicInstance;
try
topicInstance = publisher.getTopic(GetTopicRequest.newBuilder().setTopic(topic).build());
if (topicInstance != null)
logger.info("Found existing topic: ", topicInstance.getName());
return publisher;
catch (Exception ex)
logger.info("Unable to find topic: ", topic);
这总是以 404 失败。网络流量(在调试日志中)似乎确认客户端正在访问错误的端点(对于发布消息请求):
DEBUG 2015-12-17 00:58:07,962 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------OUTBOUND--------------------
HEADERS: streamId=5, headers=DefaultHttp2Headers[:authority: pubsub.googleapis.com:443, :path: /google.pubsub.v1.Publisher/Publish, :method: POST, :scheme: https, authorization: Bearer ya29.TQIWiptEf4KsAKObPIaLaJRcq49SQ2vAutO4eIC-kgM4XwvX9p-9sFBR4eNfzE_yLiBs1A, content-type: application/grpc, te: trailers, user-agent: grpc-java-netty/0.9.0], streamDependency=0, weight=16, exclusive=false, padding=0, endStream=false
------------------------------------
DEBUG 2015-12-17 00:58:07,975 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------OUTBOUND--------------------
DATA: streamId=5, padding=0, endStream=true, length=725, bytes=00000002d00a136465765f616368617568616e2d6576656e747312b8050af9047b22486f7374223a226c6f63616c686f73743a3434303830222c22436f6e6e65...
------------------------------------
DEBUG 2015-12-17 00:58:11,492 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------INBOUND--------------------
HEADERS: streamId=5, headers=DefaultHttp2Headers[:status: 404, date: Thu, 17 Dec 2015 08:58:08 GMT, content-type: text/html; charset=UTF-8, server: ESF, content-length: 1595, x-xss-protection: 1; mode=block, x-frame-options: SAMEORIGIN, x-content-type-options: nosniff, alternate-protocol: 443:quic,p=1, alt-svc: quic=":443"; ma=604800; v="30,29,28,27,26,25"], padding=0, endStream=false
------------------------------------
DEBUG 2015-12-17 00:58:26,357 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------INBOUND--------------------
DATA: streamId=5, padding=0, endStream=true, length=1595, bytes=3c21444f43545950452068746d6c3e0a3c68746d6c206c616e673d656e3e0a20203c6d65746120636861727365743d7574662d383e0a20203c6d657461206e61...
------------------------------------
DEBUG 2015-12-17 00:58:26,357 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------INBOUND--------------------
PING: ack=false, length=8, bytes=0000000000000002
------------------------------------
DEBUG 2015-12-17 00:58:26,358 [grpc-default-worker-ELG-0] i.g.n.NettyClientTransport:
----------------OUTBOUND--------------------
PING: ack=true, length=8, bytes=00000000000000
我在这里做错了什么?是否支持通过 HTTP 2(使用 protobuf)的 pubsub?
【问题讨论】:
【参考方案1】:截至 2015 年 12 月 17 日,我们尚未在 pubsub.googleapis.com
端点上启用 gRPC。现在我们正在积极测试 gRPC 支持,希望我们能在不久的将来提供它。
【讨论】:
这仍然有效吗?我在github.com/GoogleCloudPlatform/cloud-pubsub-samples-java/tree/… 看到了一些 grpc-samples,想知道这是否可能。以上是关于使用 grpc 和 protobuf(生成的客户端)通过 HTTP 2 访问 Google Cloud Pubsub的主要内容,如果未能解决你的问题,请参考以下文章
gRPC -- 在 Protobuf 中将 .png 图像从 Java 客户端发送到 Python 服务器
如何为 gRPC 生成的 Java 代码实现 com.google.protobuf.BlockingRpcChannel?