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

The following examples show how to use io.grpc.netty.NettyServerBuilder#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: GrpcStartable.java    From txle with Apache License 2.0 6 votes vote down vote up
GrpcStartable(GrpcServerConfig serverConfig, Tracing tracing, BindableService... services) {
    ServerBuilder<?> serverBuilder;
    if (serverConfig.isSslEnable()) {
      serverBuilder = NettyServerBuilder.forAddress(
          new InetSocketAddress(serverConfig.getHost(), serverConfig.getPort()));

      try {
        ((NettyServerBuilder) serverBuilder).sslContext(getSslContextBuilder(serverConfig).build());
      } catch (SSLException e) {
        throw new IllegalStateException("Unable to setup grpc to use SSL.", e);
      }
    } else {
      serverBuilder = ServerBuilder.forPort(serverConfig.getPort());
    }
//    Arrays.stream(services).forEach(serverBuilder::addService);
    // add interceptor for grpc server By Gannalyo
    Arrays.stream(services).forEach(service ->
            serverBuilder.addService(ServerInterceptors.intercept(service,
                    GrpcTracing.create(tracing).newServerInterceptor())));
    server = serverBuilder.build();
  }
 
Example 2
Source File: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example 3
Source File: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example 4
Source File: GrpcServer.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static Server startServer(String bindAddress, int port, boolean https, File confDir,
        @Nullable ExecutorService confDirWatchExecutor, DownstreamServiceImpl downstreamService,
        CollectorServiceImpl collectorService) throws IOException {
    NettyServerBuilder builder =
            NettyServerBuilder.forAddress(new InetSocketAddress(bindAddress, port));
    if (https) {
        builder.sslContext(
                DelegatingSslContext.create(confDir, checkNotNull(confDirWatchExecutor)));
    }
    return builder.addService(collectorService.bindService())
            .addService(downstreamService.bindService())
            // need to override default max message size of 4mb until streaming is implemented
            // for DownstreamService.EntriesResponse and FullTraceResponse
            .maxInboundMessageSize(64 * 1024 * 1024)
            // aggressive keep alive is used by agent to detect silently dropped connections
            // (see org.glowroot.agent.central.CentralConnection)
            .permitKeepAliveTime(20, SECONDS)
            // aggressive max connection age forces agents to re-resolve DNS often for DNS-based
            // load balancing (e.g. to pick up and spread load across new central collectors)
            .maxConnectionAge(20, MINUTES)
            .build()
            .start();
}
 
Example 5
Source File: NettyGrpcServerFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Override
protected NettyServerBuilder newServerBuilder() {
    final String address = getAddress();
    final int port = getPort();
    if (GrpcServerProperties.ANY_IP_ADDRESS.equals(address)) {
        return NettyServerBuilder.forPort(port);
    } else {
        return NettyServerBuilder.forAddress(new InetSocketAddress(InetAddresses.forString(address), port));
    }
}
 
Example 6
Source File: TccLoadBalanceSenderTest.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
private static void startServerOnPort(int port) {
  ServerBuilder<?> serverBuilder = NettyServerBuilder.forAddress(
      new InetSocketAddress("127.0.0.1", port));
  serverBuilder.addService(new MyTccEventServiceImpl(connected.get(port), eventsMap.get(port), delays.get(port)));
  Server server = serverBuilder.build();

  try {
    server.start();
    servers.put(port, server);
  } catch (Exception ex) {
    fail(ex.getMessage());
  }
}
 
Example 7
Source File: NettyGrpcServerFactory.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Override
protected NettyServerBuilder newServerBuilder() {
    final String address = getAddress();
    final int port = getPort();
    if (GrpcServerProperties.ANY_IP_ADDRESS.equals(address)) {
        return NettyServerBuilder.forPort(port);
    } else {
        return NettyServerBuilder.forAddress(new InetSocketAddress(InetAddresses.forString(address), port));
    }
}
 
Example 8
Source File: GRPCServer.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize() {
    InetSocketAddress address = new InetSocketAddress(host, port);
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(threadPoolQueueSize);
    ExecutorService executor = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 60, TimeUnit.SECONDS, blockingQueue, new CustomThreadFactory("grpcServerPool"), new CustomRejectedExecutionHandler());
    nettyServerBuilder = NettyServerBuilder.forAddress(address);
    nettyServerBuilder = nettyServerBuilder.maxConcurrentCallsPerConnection(maxConcurrentCallsPerConnection)
                                           .maxMessageSize(maxMessageSize)
                                           .executor(executor);
    logger.info("Server started, host {} listening on {}", host, port);
}
 
Example 9
Source File: ServerFactory.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public Server build() {
    InetSocketAddress bindAddress = new InetSocketAddress(this.hostname, this.port);
    NettyServerBuilder serverBuilder = NettyServerBuilder.forAddress(bindAddress);
    serverBuilder.bossEventLoopGroup(bossEventLoopGroup);
    serverBuilder.workerEventLoopGroup(workerEventLoopGroup);

    setupInternal(serverBuilder);

    for (Object service : this.bindableServices) {

        if (service instanceof BindableService) {
            logger.info("Add BindableService={}, server={}", service, name);
            serverBuilder.addService((BindableService) service);
        } else if (service instanceof ServerServiceDefinition) {
            final ServerServiceDefinition definition = (ServerServiceDefinition) service;
            logger.info("Add ServerServiceDefinition={}, server={}", definition.getServiceDescriptor(), name);
            serverBuilder.addService(definition);
        }
    }
    for (ServerTransportFilter transportFilter : this.serverTransportFilters) {
        logger.info("Add transportFilter={}, server={}", transportFilter, name);
        serverBuilder.addTransportFilter(transportFilter);
    }
    for (ServerInterceptor serverInterceptor : this.serverInterceptors) {
        logger.info("Add intercept={}, server={}", serverInterceptor, name);
        serverBuilder.intercept(serverInterceptor);
    }

    serverBuilder.executor(serverExecutor);
    setupServerOption(serverBuilder);

    Server server = serverBuilder.build();
    return server;
}
 
Example 10
Source File: OcAgentMetricsExporterIntegrationTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static Server getServer(String endPoint, BindableService service) {
  ServerBuilder<?> builder = NettyServerBuilder.forAddress(parseEndpoint(endPoint));
  Executor executor = MoreExecutors.directExecutor();
  builder.executor(executor);
  return builder.addService(service).build();
}
 
Example 11
Source File: OcAgentTraceExporterIntegrationTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static Server getServer(String endPoint, BindableService service) throws IOException {
  ServerBuilder<?> builder = NettyServerBuilder.forAddress(parseEndpoint(endPoint));
  Executor executor = MoreExecutors.directExecutor();
  builder.executor(executor);
  return builder.addService(service).build();
}
 
Example 12
Source File: XdsServerBuilder.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/** Creates a gRPC server builder for the given port. */
public static XdsServerBuilder forPort(int port) {
  NettyServerBuilder nettyDelegate = NettyServerBuilder.forAddress(new InetSocketAddress(port));
  return new XdsServerBuilder(nettyDelegate, port);
}
 
Example 13
Source File: AltsServerBuilder.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/** Creates a gRPC server builder for the given port. */
public static AltsServerBuilder forPort(int port) {
  NettyServerBuilder nettyDelegate = NettyServerBuilder.forAddress(new InetSocketAddress(port));
  return new AltsServerBuilder(nettyDelegate);
}