我开始使用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.
我在频道提供者上错过了一些东西吗?还是我不是正确配置了我的主题辅助符号?我看不到怎么了,因为我使用了这是参考。
也许有人可以帮助我。
用于与模拟器通信的通道需要将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();
}
}
这篇文章有点旧,希望这是一个更新。
使用模拟器本地测试应用程序中的代码段也可以使用。如果您在链接页面上按照" 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();
}