下面列出了io.grpc.ManagedChannelBuilder#forTarget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/** 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();
}
/**
* Returns a new gRPC ManagedChannelBuilder with instrumentation.
*/
public ManagedChannelBuilder<?> newManagedChannelBuilder(String target) {
final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target);
addDefaultBuilderProperties(builder);
return builder;
}
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);
}
/**
* 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();
}
public ManagedChannelBuilder<?> forTarget(String target) {
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target);
customize(builder);
return builder;
}