Java Code Examples for io.grpc.netty.NettyChannelBuilder#forAddress()

The following examples show how to use io.grpc.netty.NettyChannelBuilder#forAddress() . 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: ChaincodeBase.java    From fabric-chaincode-java with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("deprecation")
final ManagedChannelBuilder<?> newChannelBuilder() throws IOException {

    // Consider moving this to be pure GRPC
    // This is being reworked in master so leaving this 'as-is'
    final NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);
    LOGGER.info("Configuring channel connection to peer.");

    if (tlsEnabled) {
        builder.negotiationType(NegotiationType.TLS);
        builder.sslContext(createSSLContext());
    } else {
        builder.usePlaintext();
    }

    // there is a optional in GRPC to use 'directExecutor' rather than the inbuilt
    // gRPC thread management
    // not seen to make a marked difference in performance.
    // However if it ever does, then this is where it should be enabled
    return builder;
}
 
Example 2
Source File: GrpcManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructors a new managed channel configuration.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service to use
 */
public GrpcManagedChannelConfiguration(String name, Environment env, ExecutorService executorService) {
    this.name = name;
    final Optional<SocketAddress> socketAddress = env.getProperty(PREFIX + '.' + name + SETTING_URL, SocketAddress.class);
    if (socketAddress.isPresent()) {
        resolveName = false;
        this.channelBuilder = NettyChannelBuilder.forAddress(socketAddress.get());
    } else {
        final Optional<String> target = env.getProperty(PREFIX + '.' + name + SETTING_TARGET, String.class);
        if (target.isPresent()) {
            this.channelBuilder = NettyChannelBuilder.forTarget(
                    target.get()
            );

        } else {
            final URI uri = name.contains("//") ? URI.create(name) : null;
            if (uri != null && uri.getHost() != null && uri.getPort() > -1) {
                resolveName = false;
                this.channelBuilder = NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort());
            } else {
                this.channelBuilder = NettyChannelBuilder.forTarget(name);
            }
        }
    }
    this.getChannelBuilder().executor(executorService);
}
 
Example 3
Source File: GrpcManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructors a new managed channel configuration.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service to use
 */
public GrpcManagedChannelConfiguration(String name, Environment env, ExecutorService executorService) {
    this.name = name;
    final Optional<SocketAddress> socketAddress = env.getProperty(PREFIX + '.' + name + SETTING_URL, SocketAddress.class);
    if (socketAddress.isPresent()) {
        resolveName = false;
        this.channelBuilder = NettyChannelBuilder.forAddress(socketAddress.get());
    } else {
        final Optional<String> target = env.getProperty(PREFIX + '.' + name + SETTING_TARGET, String.class);
        if (target.isPresent()) {
            this.channelBuilder = NettyChannelBuilder.forTarget(
                    target.get()
            );

        } else {
            final URI uri = name.contains("//") ? URI.create(name) : null;
            if (uri != null && uri.getHost() != null && uri.getPort() > -1) {
                resolveName = false;
                this.channelBuilder = NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort());
            } else {
                this.channelBuilder = NettyChannelBuilder.forTarget(name);
            }
        }
    }
    this.getChannelBuilder().executor(executorService);
}
 
Example 4
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 5
Source File: DefaultChannelFactory.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public ManagedChannel build(String channelName, String host, int port) {
    final NettyChannelBuilder channelBuilder = NettyChannelBuilder.forAddress(host, port);
    channelBuilder.usePlaintext();
    channelBuilder.eventLoopGroup(eventLoopGroup);
    setupInternal(channelBuilder);

    addHeader(channelBuilder);
    addClientInterceptor(channelBuilder);

    channelBuilder.executor(executorService);
    if (this.nameResolverProvider != null) {
        logger.info("Set nameResolverProvider {}. channelName={}, host={}, port={}", this.nameResolverProvider, channelName, host, port);
        channelBuilder.nameResolverFactory(this.nameResolverProvider);
    }
    setupClientOption(channelBuilder);

    final ManagedChannel channel = channelBuilder.build();

    return channel;
}
 
Example 6
Source File: AgentClientMock.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public AgentClientMock(final String host, final int port, final boolean agentHeader) {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);

    if (agentHeader) {
        HeaderFactory headerFactory = new AgentHeaderFactory("mockAgentId", "mockApplicationName", System.currentTimeMillis());
        final Metadata extraHeaders = headerFactory.newHeader();
        final ClientInterceptor headersInterceptor = MetadataUtils.newAttachHeadersInterceptor(extraHeaders);
        builder.intercept(headersInterceptor);
    }
    builder.usePlaintext();
    channel = builder.build();
    this.agentStub = AgentGrpc.newStub(channel);
    this.metadataStub = MetadataGrpc.newBlockingStub(channel);
}
 
Example 7
Source File: StatClientMock.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public StatClientMock(final String host, final int port) {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);
    HeaderFactory headerFactory = new AgentHeaderFactory("mockAgentId", "mockApplicationName", System.currentTimeMillis());
    final Metadata extraHeaders = headerFactory.newHeader();
    final ClientInterceptor headersInterceptor = MetadataUtils.newAttachHeadersInterceptor(extraHeaders);
    builder.intercept(headersInterceptor);
    builder.usePlaintext();

    channel = builder.build();
    this.statStub = StatGrpc.newStub(channel);
}
 
Example 8
Source File: CentralConnection.java    From glowroot with Apache License 2.0 4 votes vote down vote up
CentralConnection(String collectorAddress, @Nullable String collectorAuthority,
        List<File> confDirs, AtomicBoolean inConnectionFailure) throws SSLException {
    ParsedCollectorAddress parsedCollectorAddress = parseCollectorAddress(collectorAddress);
    eventLoopGroup = EventLoopGroups.create("Glowroot-GRPC-Worker-ELG");
    channelExecutor =
            Executors.newSingleThreadExecutor(ThreadFactories.create("Glowroot-GRPC-Executor"));
    NettyChannelBuilder builder;
    if (parsedCollectorAddress.targets().size() == 1) {
        CollectorTarget target = parsedCollectorAddress.targets().get(0);
        builder = NettyChannelBuilder.forAddress(target.host(), target.port());
        if (collectorAuthority != null) {
            builder.overrideAuthority(collectorAuthority);
        }
    } else {
        // this connection mechanism may be deprecated in the future in favor resolving a single
        // address to multiple collectors via DNS (above)
        String authority;
        if (collectorAuthority != null) {
            authority = collectorAuthority;
        } else if (!parsedCollectorAddress.https()) {
            authority = "dummy-service-authority";
        } else {
            throw new IllegalStateException("collector.authority is required when connecting"
                    + " over HTTPS to a comma-separated list of glowroot central collectors");
        }
        builder = NettyChannelBuilder.forTarget("dummy-target")
                .nameResolverFactory(new MultipleAddressNameResolverFactory(
                        parsedCollectorAddress.targets(), authority));
    }
    // single address may resolve to multiple collectors above via DNS, so need to specify round
    // robin here even if only single address (first part of conditional above)
    builder.loadBalancerFactory(RoundRobinLoadBalancerFactory.getInstance())
            .eventLoopGroup(eventLoopGroup)
            .executor(channelExecutor)
            // aggressive keep alive, shouldn't even be used since gauge data is sent every
            // 5 seconds and keep alive will only kick in after 10 seconds of not hearing back
            // from the server
            .keepAliveTime(10, SECONDS);
    if (parsedCollectorAddress.https()) {
        SslContextBuilder sslContext = GrpcSslContexts.forClient();
        File trustCertCollectionFile = getTrustCertCollectionFile(confDirs);
        if (trustCertCollectionFile != null) {
            sslContext.trustManager(trustCertCollectionFile);
        }
        channel = builder.sslContext(sslContext.build())
                .negotiationType(NegotiationType.TLS)
                .build();
    } else {
        channel = builder.negotiationType(NegotiationType.PLAINTEXT)
                .build();
    }
    retryExecutor = Executors.newSingleThreadScheduledExecutor(
            ThreadFactories.create("Glowroot-Collector-Retry"));
    this.inConnectionFailure = inConnectionFailure;
    this.collectorAddress = collectorAddress;
}
 
Example 9
Source File: XdsChannelBuilder.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new builder with the given server address. See {@link
 * NettyChannelBuilder#forAddress(SocketAddress)} for more info.
 */
@CheckReturnValue
public static XdsChannelBuilder forAddress(SocketAddress serverAddress) {
  return new XdsChannelBuilder(NettyChannelBuilder.forAddress(serverAddress));
}
 
Example 10
Source File: XdsChannelBuilder.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new builder with the given host and port. See {@link
 * NettyChannelBuilder#forAddress(String, int)} for more info.
 */
@CheckReturnValue
public static XdsChannelBuilder forAddress(String host, int port) {
  return new XdsChannelBuilder(NettyChannelBuilder.forAddress(host, port));
}