下面列出了怎么用io.grpc.auth.ClientAuthInterceptor的API类实例代码及写法,或者点击链接到github查看源代码。
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */
public static Channel getChannel(CredentialsProvider credentialsProvider) throws IOException {
ManagedChannel channelImpl =
NettyChannelBuilder.forAddress(ENDPOINT, 443)
.negotiationType(NegotiationType.TLS)
// Maximum Pub/Sub message size is 10MB.
.maxInboundMessageSize(10 * 1024 * 1024)
.build();
final ClientAuthInterceptor interceptor =
new ClientAuthInterceptor(
credentialsProvider.getCredentials(),
Executors.newCachedThreadPool());
return ClientInterceptors.intercept(channelImpl, interceptor);
}
/** Return channel with interceptor for returning credentials. */
private Channel newChannel() throws IOException {
checkState(publisherChannel != null, "PubsubGrpcClient has been closed");
ClientAuthInterceptor interceptor =
new ClientAuthInterceptor(credentials, Executors.newSingleThreadExecutor());
return ClientInterceptors.intercept(publisherChannel, interceptor);
}
public static void main(final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Please specify your project name.");
System.exit(1);
}
final String project = args[0];
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
// Down-scope the credential to just the scopes required by the service
creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
// Intercept the channel to bind the credential
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
// Create a stub using the channel that has the bound credential
PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10)
.setProject("projects/" + project)
.build();
ListTopicsResponse resp = publisherStub.listTopics(request);
System.out.println("Found " + resp.getTopicsCount() + " topics.");
for (Topic topic : resp.getTopicsList()) {
System.out.println(topic.getName());
}
}