Java Code Examples for io.grpc.ManagedChannelBuilder#forTarget()

The following examples show how to use io.grpc.ManagedChannelBuilder#forTarget() . 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: OtlpGrpcSpanExporter.java    From opentelemetry-java with Apache License 2.0 7 votes vote down vote up
/**
 * Constructs a new instance of the exporter based on the builder's values.
 *
 * @return a new exporter's instance
 */
public OtlpGrpcSpanExporter build() {
  if (endpoint != null) {
    final ManagedChannelBuilder<?> managedChannelBuilder =
        ManagedChannelBuilder.forTarget(endpoint);

    if (useTls) {
      managedChannelBuilder.useTransportSecurity();
    } else {
      managedChannelBuilder.usePlaintext();
    }

    if (metadata != null) {
      managedChannelBuilder.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
    }

    channel = managedChannelBuilder.build();
  }
  return new OtlpGrpcSpanExporter(channel, deadlineMs);
}
 
Example 2
Source File: GrpcCallProvider.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Sets up the SSL provider and configures the gRPC channel. */
private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) {
  try {
    // We need to upgrade the Security Provider before any network channels are initialized.
    // `OkHttp` maintains a list of supported providers that is initialized when the JVM first
    // resolves the static dependencies of ManagedChannel.
    ProviderInstaller.installIfNeeded(context);
  } catch (GooglePlayServicesNotAvailableException /* Thrown by ProviderInstaller */
      | GooglePlayServicesRepairableException /* Thrown by ProviderInstaller */
      | IllegalStateException e /* Thrown by Robolectric */) {
    // Mark the SSL initialization as done, even though we may be using outdated SSL
    // ciphers. gRPC-Java recommends obtaining updated ciphers from GMSCore, but we allow
    // the device to fall back to other SSL ciphers if GMSCore is not available.
    Logger.warn(LOG_TAG, "Failed to update ssl context: %s", e);
  }

  ManagedChannelBuilder<?> channelBuilder;
  if (overrideChannelBuilderSupplier != null) {
    channelBuilder = overrideChannelBuilderSupplier.get();
  } else {
    channelBuilder = ManagedChannelBuilder.forTarget(databaseInfo.getHost());
    if (!databaseInfo.isSslEnabled()) {
      // Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
      // It merely turns off SSL encryption.
      channelBuilder.usePlaintext();
    }
  }

  // Ensure gRPC recovers from a dead connection. (Not typically necessary, as the OS will
  // usually notify gRPC when a connection dies. But not always. This acts as a failsafe.)
  channelBuilder.keepAliveTime(30, TimeUnit.SECONDS);

  // Wrap the ManagedChannelBuilder in an AndroidChannelBuilder. This allows the channel to
  // respond more gracefully to network change events (such as switching from cell to wifi).
  AndroidChannelBuilder androidChannelBuilder =
      AndroidChannelBuilder.usingBuilder(channelBuilder).context(context);

  return androidChannelBuilder.build();
}
 
Example 3
Source File: GrpcChannelBuilderFactory.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new gRPC ManagedChannelBuilder with instrumentation.
 */
public ManagedChannelBuilder<?> newManagedChannelBuilder(String target) {
  final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target);
  addDefaultBuilderProperties(builder);

  return builder;
}
 
Example 4
Source File: DropwizardPersonServiceGrpcImplTest.java    From dropwizard-grpc with Apache License 2.0 5 votes vote down vote up
private static void startClient() {
    final TestApplication application = DROPWIZARD.getApplication();
    final ManagedChannelBuilder<?> localhost =
            ManagedChannelBuilder.forTarget("localhost:" + application.getServer().getPort());
    channel = localhost.usePlaintext().build();
    client = PersonServiceGrpc.newBlockingStub(channel);
}
 
Example 5
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 6
Source File: SpringAwareManagedChannelBuilder.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
public ManagedChannelBuilder<?> forTarget(String target) {
	ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target);
	customize(builder);
	return builder;
}