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

The following examples show how to use io.grpc.ManagedChannelBuilder#build() . 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: LumongoConnection.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public void open(boolean compressedConnection) throws IOException {

		ManagedChannelBuilder<?> managedChannelBuilder = ManagedChannelBuilder.forAddress(member.getServerAddress(), member.getExternalPort())
				.maxInboundMessageSize(256 * 1024 * 1024).usePlaintext(true);
		channel = managedChannelBuilder.build();

		blockingStub = ExternalServiceGrpc.newBlockingStub(channel);
		if (compressedConnection) {
			blockingStub = blockingStub.withCompression("gzip");
		}

		asyncStub = ExternalServiceGrpc.newStub(channel);
		if (compressedConnection) {
			asyncStub = asyncStub.withCompression("gzip");
		}

		System.err.println("INFO: Connecting to <" + member.getServerAddress() + ">");

	}
 
Example 3
Source File: OcAgentMetricsExporterWorker.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private void connect() {
  ManagedChannelBuilder<?> channelBuilder;
  if (useInsecure) {
    channelBuilder = ManagedChannelBuilder.forTarget(endPoint).usePlaintext();
  } else {
    channelBuilder =
        NettyChannelBuilder.forTarget(endPoint)
            .negotiationType(NegotiationType.TLS)
            .sslContext(sslContext);
  }
  ManagedChannel channel = channelBuilder.build();
  MetricsServiceGrpc.MetricsServiceStub stub = MetricsServiceGrpc.newStub(channel);
  exportRpcHandler = OcAgentMetricsServiceExportRpcHandler.create(stub);
  ExportMetricsServiceRequest.Builder builder =
      ExportMetricsServiceRequest.newBuilder().setNode(OcAgentNodeUtils.getNodeInfo(serviceName));
  @Nullable Resource resourceProto = OcAgentNodeUtils.getAutoDetectedResourceProto();
  if (resourceProto != null) {
    builder.setResource(resourceProto);
  }
  exportRpcHandler.onExport(builder.build());
}
 
Example 4
Source File: GrpcServerChannel.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a managed server channel.
 * @param server The server
 * @param executorService The executor service
 * @param clientInterceptors The client interceptors
 * @return The channel
 */
@Singleton
@Named(NAME)
@Requires(beans = GrpcEmbeddedServer.class)
@Bean(preDestroy = "shutdown")
protected ManagedChannel serverChannel(
        GrpcEmbeddedServer server,
        @javax.inject.Named(TaskExecutors.IO) ExecutorService executorService,
        List<ClientInterceptor> clientInterceptors) {
    final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(
            server.getHost(),
            server.getPort()
    ).executor(executorService);
    if (!server.getServerConfiguration().isSecure()) {
        builder.usePlaintext();
    }
    if (CollectionUtils.isNotEmpty(clientInterceptors)) {
        builder.intercept(clientInterceptors);
    }
    return builder.build();
}
 
Example 5
Source File: GRPCChannel.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private GRPCChannel(String host, int port, List<ChannelBuilder> channelBuilders,
    List<ChannelDecorator> decorators) throws Exception {
    ManagedChannelBuilder channelBuilder = NettyChannelBuilder.forAddress(host, port);

    for (ChannelBuilder builder : channelBuilders) {
        channelBuilder = builder.build(channelBuilder);
    }

    this.originChannel = channelBuilder.build();

    Channel channel = originChannel;
    for (ChannelDecorator decorator : decorators) {
        channel = decorator.build(channel);
    }

    channelWithDecorators = channel;
}
 
Example 6
Source File: OkHttpClientInteropServlet.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
protected ManagedChannel createChannel() {
  assertEquals(
      "jdk7 required",
      "1.7",
      System.getProperty("java.specification.version"));
  assertEquals(
      "Can not run in dev servers because they lack org.conscrypt.OpenSSLProvider support",
      "Production",
      System.getProperty("com.google.appengine.runtime.environment"));
  ManagedChannelBuilder<?> builder =
      ManagedChannelBuilder.forTarget(INTEROP_TEST_ADDRESS)
          .maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
  assertTrue(builder instanceof OkHttpChannelBuilder);
  return builder.build();
}
 
Example 7
Source File: TesterOkHttpChannelBuilder.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public static ManagedChannel build(
    String host,
    int port,
    @Nullable String serverHostOverride,
    boolean useTls,
    @Nullable InputStream testCa) {
  ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
      .maxInboundMessageSize(16 * 1024 * 1024);
  if (serverHostOverride != null) {
    // Force the hostname to match the cert the server uses.
    channelBuilder.overrideAuthority(serverHostOverride);
  }
  if (useTls) {
    try {
      ((OkHttpChannelBuilder) channelBuilder).useTransportSecurity();
      ((OkHttpChannelBuilder) channelBuilder).sslSocketFactory(getSslSocketFactory(testCa));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  } else {
    channelBuilder.usePlaintext();
  }
  return channelBuilder.build();
}
 
Example 8
Source File: InternalRpcConnection.java    From lumongo with Apache License 2.0 5 votes vote down vote up
public InternalRpcConnection(String memberAddress, int internalServicePort) {
	this.memberAddress = memberAddress;
	this.internalServicePort = internalServicePort;

	ManagedChannelBuilder<?> managedChannelBuilder = ManagedChannelBuilder.forAddress(memberAddress, internalServicePort)
			.maxInboundMessageSize(256 * 1024 * 1024).usePlaintext(true);
	channel = managedChannelBuilder.build();

	blockingStub = InternalServiceGrpc.newBlockingStub(channel);

	log.info("Connecting to <" + memberAddress + ":" + internalServicePort + ">");
}
 
Example 9
Source File: Utils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link ManagedChannel} for the given parameters.
 */
public static ManagedChannel newClientChannel(Transport transport, SocketAddress address,
      boolean tls, boolean testca, @Nullable String authorityOverride,
      int flowControlWindow, boolean directExecutor) {
  ManagedChannelBuilder<?> builder;
  if (transport == Transport.OK_HTTP) {
    builder = newOkHttpClientChannel(address, tls, testca);
  } else {
    try {
      builder = newNettyClientChannel(transport, address, tls, testca, flowControlWindow);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  if (authorityOverride != null) {
    builder.overrideAuthority(authorityOverride);
  }

  if (directExecutor) {
    builder.directExecutor();
  } else {
    // TODO(carl-mastrangelo): This should not be necessary.  I don't know where this should be
    // put.  Move it somewhere else, or remove it if no longer necessary.
    // See: https://github.com/grpc/grpc-java/issues/2119
    builder.executor(getExecutor());
  }

  return builder.build();
}
 
Example 10
Source File: NettyClientInteropServlet.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
protected ManagedChannel createChannel() {
  assertEquals(
      "jdk8 required",
      "1.8",
      System.getProperty("java.specification.version"));
  ManagedChannelBuilder<?> builder =
      ManagedChannelBuilder.forTarget(INTEROP_TEST_ADDRESS)
          .maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
  assertTrue(builder instanceof NettyChannelBuilder);
  ((NettyChannelBuilder) builder).flowControlWindow(65 * 1024);
  return builder.build();
}
 
Example 11
Source File: GRPCNoServerTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    ManagedChannelBuilder<?> channelBuilder = NettyChannelBuilder.forAddress("127.0.0.1", 8080)
                                                                 .nameResolverFactory(new DnsNameResolverProvider())
                                                                 .maxInboundMessageSize(1024 * 1024 * 50)
                                                                 .usePlaintext();
    ManagedChannel channel = channelBuilder.build();
    TraceSegmentReportServiceGrpc.TraceSegmentReportServiceStub serviceStub = TraceSegmentReportServiceGrpc.newStub(channel);
    final Status[] status = {null};
    StreamObserver<SegmentObject> streamObserver = serviceStub.collect(new StreamObserver<Commands>() {
        @Override
        public void onNext(Commands value) {

        }

        @Override
        public void onError(Throwable t) {
            status[0] = ((StatusRuntimeException) t).getStatus();
        }

        @Override
        public void onCompleted() {

        }
    });

    streamObserver.onNext(null);
    streamObserver.onCompleted();

    Thread.sleep(2 * 1000);

    Assert.assertEquals(status[0].getCode(), Status.UNAVAILABLE.getCode());
}
 
Example 12
Source File: NettyClientInteropServlet.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
protected ManagedChannel createChannel() {
  assertEquals(
      "jdk8 required",
      "1.8",
      System.getProperty("java.specification.version"));
  ManagedChannelBuilder<?> builder =
      ManagedChannelBuilder.forTarget(INTEROP_TEST_ADDRESS)
          .maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
  assertTrue(builder instanceof NettyChannelBuilder);
  ((NettyChannelBuilder) builder).flowControlWindow(65 * 1024);
  return builder.build();
}
 
Example 13
Source File: ControllerImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of the Controller client class.
 *  @param channelBuilder The channel builder to connect to the service instance.
 * @param config         The configuration for this client implementation.
 * @param executor       The executor service to be used internally.
 */
@VisibleForTesting
public ControllerImpl(ManagedChannelBuilder<?> channelBuilder, final ControllerImplConfig config,
                      final ScheduledExecutorService executor) {
    Preconditions.checkNotNull(channelBuilder, "channelBuilder");
    this.executor = executor;
    this.retryConfig = createRetryConfig(config);

    if (config.getClientConfig().isEnableTlsToController()) {
        log.debug("Setting up a SSL/TLS channel builder");
        SslContextBuilder sslContextBuilder;
        String trustStore = config.getClientConfig().getTrustStore();
        sslContextBuilder = GrpcSslContexts.forClient();
        if (!Strings.isNullOrEmpty(trustStore)) {
            sslContextBuilder = sslContextBuilder.trustManager(new File(trustStore));
        }
        try {
            channelBuilder = ((NettyChannelBuilder) channelBuilder).sslContext(sslContextBuilder.build())
                                                                   .negotiationType(NegotiationType.TLS);
        } catch (SSLException e) {
            throw new CompletionException(e);
        }
    } else {
        log.debug("Using a plaintext channel builder");
        channelBuilder = ((NettyChannelBuilder) channelBuilder).negotiationType(NegotiationType.PLAINTEXT);
    }

    // Trace channel.
    channelBuilder = channelBuilder.intercept(RPCTracingHelpers.getClientInterceptor());

    // Create Async RPC client.
    this.channel = channelBuilder.build();
    this.client = getClientWithCredentials(config);
    this.timeoutMillis = config.getTimeoutMillis();
}
 
Example 14
Source File: JupyterKernelClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
/**
 * Construct client for accessing RouteGuide server using the existing channel.
 */
public JupyterKernelClient(ManagedChannelBuilder<?> channelBuilder,
                           Properties properties,
                           String kernel) {
  channel = channelBuilder.build();
  blockingStub = JupyterKernelGrpc.newBlockingStub(channel);
  asyncStub = JupyterKernelGrpc.newStub(channel);
  this.properties = properties;
  this.kernel = kernel;
}
 
Example 15
Source File: LogdClient.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a LogdClient with the provided channel
 *
 * @param channelBuilder a channel to LogD server
 */
@VisibleForTesting
public LogdClient(
    ManagedChannelBuilder<?> channelBuilder, StreamObserverFactory streamObserverFactory) {
  channel = channelBuilder.build();
  blockingStub = LogdServiceGrpc.newBlockingStub(channel);
  asyncStub = LogdServiceGrpc.newStub(channel);
  this.streamObserverFactory = streamObserverFactory;
}
 
Example 16
Source File: OcAgentTraceExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static TraceServiceGrpc.TraceServiceStub getTraceServiceStub(
    String endPoint, Boolean useInsecure, SslContext sslContext) {
  ManagedChannelBuilder<?> channelBuilder;
  if (useInsecure) {
    channelBuilder = ManagedChannelBuilder.forTarget(endPoint).usePlaintext();
  } else {
    channelBuilder =
        NettyChannelBuilder.forTarget(endPoint)
            .negotiationType(NegotiationType.TLS)
            .sslContext(sslContext);
  }
  ManagedChannel channel = channelBuilder.build();
  return TraceServiceGrpc.newStub(channel);
}
 
Example 17
Source File: RouteGuideClient.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/** Construct client for accessing RouteGuide server using the existing channel. */
public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
  channel = channelBuilder.build();
  blockingStub = RouteGuideGrpc.newBlockingStub(channel);
  asyncStub = RouteGuideGrpc.newStub(channel);
}
 
Example 18
Source File: RouteGuideClient.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/** Construct client for accessing RouteGuide server using the existing channel. */
public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
  channel = channelBuilder.build();
  blockingStub = RouteGuideGrpc.newBlockingStub(channel);
  asyncStub = RouteGuideGrpc.newStub(channel);
}
 
Example 19
Source File: SubmarineRpcClient.java    From submarine with Apache License 2.0 4 votes vote down vote up
/** Construct client for accessing RouteGuide server using the existing channel. */
public SubmarineRpcClient(ManagedChannelBuilder<?> channelBuilder) {
  channel = channelBuilder.build();
  blockingStub = SubmarineServerProtocolGrpc.newBlockingStub(channel);
  asyncStub = SubmarineServerProtocolGrpc.newStub(channel);
}
 
Example 20
Source File: ChaincodeSupportClient.java    From fabric-chaincode-java with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param channelBuilder
 */
public ChaincodeSupportClient(final ManagedChannelBuilder<?> channelBuilder) {
    this.channel = channelBuilder.build();
    this.stub = ChaincodeSupportGrpc.newStub(channel);
}