我开始使用pubsub模拟器来测试我的基本实现,并在尝试创建新主题时遇到了问题。
我的模拟器侦听localhost:8085,如果我通过api
创建主题PUT http://localhost:8085/v1/projects/testproject/topics/test
一切正常,主题就会被创建。 但是如果我运行以下代码片段没有按预期工作,也没有创建任何主题:
TopicName topicName = TopicName.create("testproject", "test");
ChannelProvider channelProvider =
TopicAdminSettings.defaultChannelProviderBuilder()
.setEndpoint("localhost:8085")
.setCredentialsProvider(
FixedCredentialsProvider.create(NoCredentials.getInstance()))
.build();
TopicAdminClient topicClient = TopicAdminClient.create(
TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
topicClient.createTopic(topicName);
在运行此模拟器时记录
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFORMATION: Unknown request URI: /bad-request
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFORMATION: Unknown request URI: /bad-request
...
[pubsub] Apr 27, 2017 1:10:49 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:49 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.
我在ChannelProvider上遗漏了什么?或者我没有正确配置我的TopicAdminClient?自从我使用以来,我没有看到什么错误 this as reference
也许有人可以帮我解决这个问题。
答案 0 :(得分:3)
用于与模拟器通信的通道需要将negotiationType
属性设置为NegotiationType.PLAINTEXT
。这意味着您需要创建自定义ChannelProvider
。以下内容应该有效:
public class PlainTextChannelProvider implements ChannelProvider {
@Override
public boolean shouldAutoClose() {
return false;
}
@Override
public boolean needsExecutor() {
return false;
}
@Override
public ManagedChannel getChannel() throws IOException {
return NettyChannelBuilder.forAddress("localhost", 8085)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
}
@Override
public ManagedChannel getChannel(Executor executor) throws IOException {
return getChannel();
}
}
答案 1 :(得分:0)
这篇文章有点老了,希望这可以作为更新。
code snippet中的Testing apps locally with the emulator也可以使用。如果您点击链接页面上的“在GitHub上查看”链接,则完整的代码段位于GitHub上。
String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
try {
TransportChannelProvider channelProvider =
FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
TopicAdminClient topicClient =
TopicAdminClient.create(
TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build());
try {
response = topicClient.createTopic(topicName);
System.out.printf("Topic %s created.\n", response);
} catch (ApiException e) {
System.out.println(e.getStatusCode().getCode());
System.out.println(e.isRetryable());
System.out.println("No topic was created.");
}
} finally {
channel.shutdown();
}