2016-01-27 58 views
2

我已經設置了Pubsub.Builder,其根URL指向本地PubSub仿真器(在我的情況下爲localhost:8036)。它似乎工作,我看到模擬器正在接受我的測試推送,但給我錯誤400錯誤請求。使用來自Java的Google Cloud PubSub仿真器

發佈代碼:

auth = GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) 
     .createScoped(PubsubScopes.all()); 
client = new Pubsub.Builder(HTTP_TRANSPORT, JSON_FACTORY, auth) 
     .setApplicationName "test" 
     .setRootUrl("http://localhost:8036/") 
     .build(); 
msg = new PubsubMessage().encodeData("{\"a\": 1, \"b\": 2}".getBytes()); 
req = new PublishRequest().setMessages(Arrays.asList(msg)); 
client.projects() 
    .topics() 
    .publish("projects/GCLOUD-DEFAULT-PROJECT/topics/test-topic", req) 
    .execute(); 

在模擬器控制檯輸出我看到以下(這是非常基本的PublishRequest):

[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.FakePubsubGrpcServer$2 operationComplete 
[pubsub] INFO: Adding handler(s) to newly registered Channel. 
[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.NettyUtil$HttpVersionDecider channelRead 
[pubsub] INFO: Detected non-HTTP/2 connection. 
[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.NettyUtil$HttpJsonAdapter channelRead 
[pubsub] INFO: Invalid input: Expect message object but got: "\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�M-.NLO-V���VJI" 
[pubsub] com.google.protobuf.InvalidProtocolBufferException: Expect message object but got: "\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�M-.NLO-V���VJI" 
[pubsub] at com.google.protobuf.util.JsonFormat$ParserImpl.mergeMessage(JsonFormat.java:1099) 
[pubsub] at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:1075) 
[pubsub] at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:973) 
[pubsub] at com.google.protobuf.util.JsonFormat$Parser.merge(JsonFormat.java:201) 
[pubsub] at com.google.cloud.pubsub.testing.v1.PubsubJsonGrpcAdapters$PublisherAdapter.handleRequest(PubsubJsonGrpcAdapters.java:231) 
[pubsub] at com.google.cloud.pubsub.testing.v1.NettyUtil$HttpJsonAdapter.channelRead(NettyUtil.java:94) 
[pubsub] at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83) 

似乎默認PubSub的客戶端使用不同的協議,但我不看任何方式來配置它。

如何在本地模擬器中使用com.google.apis:google-api-services-pubsub庫?

回答

2

模擬器不需要或處理認證或授權;我猜這就是問題所在。你能否嘗試將null作爲最後一個參數傳遞給構建器?

此外,它似乎Gzip壓縮必須顯式禁用。請爲此在Builder:

.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() { 
    @Override 
    public void initialize(AbstractGoogleClientRequest<?> request) throws IOException { 
    request.setDisableGZipContent(true); 
    } 
}) 
+0

注:gzip壓縮的請求被支持作爲SDK版本96.見https://cloud.google.com/sdk/release_notes的。 –