io.grpc.alts.GoogleDefaultChannelBuilder Java Examples

The following examples show how to use io.grpc.alts.GoogleDefaultChannelBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StorageStubProvider.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private ChannelAndRequestCounter buildManagedChannel() {
  ActiveRequestCounter counter = new ActiveRequestCounter();
  ManagedChannel channel =
      GoogleDefaultChannelBuilder.forTarget(
              isNullOrEmpty(readOptions.getGrpcServerAddress())
                  ? DEFAULT_GCS_GRPC_SERVER_ADDRESS
                  : readOptions.getGrpcServerAddress())
          .enableRetry()
          .defaultServiceConfig(getGrpcServiceConfig())
          .intercept(counter)
          .build();
  return new ChannelAndRequestCounter(channel, counter);
}
 
Example #2
Source File: XdsClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a channel to the first server in the given list.
 */
@Override
ManagedChannel createChannel(List<ServerInfo> servers) {
  checkArgument(!servers.isEmpty(), "No management server provided.");
  XdsLogger logger = XdsLogger.withPrefix("xds-client-channel-factory");
  ServerInfo serverInfo = servers.get(0);
  String serverUri = serverInfo.getServerUri();
  logger.log(XdsLogLevel.INFO, "Creating channel to {0}", serverUri);
  List<ChannelCreds> channelCredsList = serverInfo.getChannelCredentials();
  ManagedChannelBuilder<?> channelBuilder = null;
  // Use the first supported channel credentials configuration.
  // Currently, only "google_default" is supported.
  for (ChannelCreds creds : channelCredsList) {
    if (creds.getType().equals("google_default")) {
      logger.log(XdsLogLevel.INFO, "Using channel credentials: google_default");
      channelBuilder = GoogleDefaultChannelBuilder.forTarget(serverUri);
      break;
    }
  }
  if (channelBuilder == null) {
    logger.log(XdsLogLevel.INFO, "Using default channel credentials");
    channelBuilder = ManagedChannelBuilder.forTarget(serverUri);
  }

  return channelBuilder
      .keepAliveTime(5, TimeUnit.MINUTES)
      .build();
}
 
Example #3
Source File: TestServiceClient.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
protected ManagedChannel createChannel() {
  if (customCredentialsType != null
      && customCredentialsType.equals("google_default_credentials")) {
    return GoogleDefaultChannelBuilder.forAddress(serverHost, serverPort).build();
  }
  if (useAlts) {
    return AltsChannelBuilder.forAddress(serverHost, serverPort).build();
  }
  AbstractManagedChannelImplBuilder<?> builder;
  if (!useOkHttp) {
    SslContext sslContext = null;
    if (useTestCa) {
      try {
        sslContext = GrpcSslContexts.forClient().trustManager(
                TestUtils.loadCert("ca.pem")).build();
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    }
    NettyChannelBuilder nettyBuilder =
        NettyChannelBuilder.forAddress(serverHost, serverPort)
            .flowControlWindow(65 * 1024)
            .negotiationType(useTls ? NegotiationType.TLS : NegotiationType.PLAINTEXT)
            .sslContext(sslContext);
    if (serverHostOverride != null) {
      nettyBuilder.overrideAuthority(serverHostOverride);
    }
    if (fullStreamDecompression) {
      nettyBuilder.enableFullStreamDecompression();
    }
    builder = nettyBuilder;
  } else {
    OkHttpChannelBuilder okBuilder = OkHttpChannelBuilder.forAddress(serverHost, serverPort);
    if (serverHostOverride != null) {
      // Force the hostname to match the cert the server uses.
      okBuilder.overrideAuthority(
          GrpcUtil.authorityFromHostAndPort(serverHostOverride, serverPort));
    }
    if (useTls) {
      try {
        SSLSocketFactory factory = useTestCa
            ? TestUtils.newSslSocketFactoryForCa(Platform.get().getProvider(),
                TestUtils.loadCert("ca.pem"))
            : (SSLSocketFactory) SSLSocketFactory.getDefault();
        okBuilder.sslSocketFactory(factory);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    } else {
      okBuilder.usePlaintext();
    }
    if (fullStreamDecompression) {
      okBuilder.enableFullStreamDecompression();
    }
    builder = okBuilder;
  }
  io.grpc.internal.TestingAccessor.setStatsImplementation(
      builder, createClientCensusStatsModule());
  return builder.build();
}
 
Example #4
Source File: TestServiceClient.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected ManagedChannel createChannel() {
  if (customCredentialsType != null
      && customCredentialsType.equals("google_default_credentials")) {
    return GoogleDefaultChannelBuilder.forAddress(serverHost, serverPort).build();
  }
  if (customCredentialsType != null
      && customCredentialsType.equals("compute_engine_channel_creds")) {
    return ComputeEngineChannelBuilder.forAddress(serverHost, serverPort).build();
  }
  if (useAlts) {
    return AltsChannelBuilder.forAddress(serverHost, serverPort).build();
  }
  AbstractManagedChannelImplBuilder<?> builder;
  if (!useOkHttp) {
    SslContext sslContext = null;
    if (useTestCa) {
      try {
        sslContext = GrpcSslContexts.forClient().trustManager(
                TestUtils.loadCert("ca.pem")).build();
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    }
    NettyChannelBuilder nettyBuilder =
        NettyChannelBuilder.forAddress(serverHost, serverPort)
            .flowControlWindow(65 * 1024)
            .negotiationType(useTls ? NegotiationType.TLS :
              (useH2cUpgrade ? NegotiationType.PLAINTEXT_UPGRADE : NegotiationType.PLAINTEXT))
            .sslContext(sslContext);
    if (serverHostOverride != null) {
      nettyBuilder.overrideAuthority(serverHostOverride);
    }
    if (fullStreamDecompression) {
      nettyBuilder.enableFullStreamDecompression();
    }
    builder = nettyBuilder;
  } else {
    OkHttpChannelBuilder okBuilder = OkHttpChannelBuilder.forAddress(serverHost, serverPort);
    if (serverHostOverride != null) {
      // Force the hostname to match the cert the server uses.
      okBuilder.overrideAuthority(
          GrpcUtil.authorityFromHostAndPort(serverHostOverride, serverPort));
    }
    if (useTls) {
      if (useTestCa) {
        try {
          SSLSocketFactory factory = TestUtils.newSslSocketFactoryForCa(
              Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
          okBuilder.sslSocketFactory(factory);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    } else {
      okBuilder.usePlaintext();
    }
    if (fullStreamDecompression) {
      okBuilder.enableFullStreamDecompression();
    }
    builder = okBuilder;
  }
  // Disable the default census stats interceptor, use testing interceptor instead.
  io.grpc.internal.TestingAccessor.setStatsEnabled(builder, false);
  return builder.intercept(createCensusStatsClientInterceptor()).build();
}