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

The following examples show how to use io.grpc.netty.NettyServerBuilder#addService() . 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: RemoteWorker.java    From bazel with Apache License 2.0 6 votes vote down vote up
public Server startServer() throws IOException {
  ServerInterceptor headersInterceptor = new TracingMetadataUtils.ServerHeadersInterceptor();
  NettyServerBuilder b =
      NettyServerBuilder.forPort(workerOptions.listenPort)
          .addService(ServerInterceptors.intercept(actionCacheServer, headersInterceptor))
          .addService(ServerInterceptors.intercept(bsServer, headersInterceptor))
          .addService(ServerInterceptors.intercept(casServer, headersInterceptor))
          .addService(ServerInterceptors.intercept(capabilitiesServer, headersInterceptor));

  if (workerOptions.tlsCertificate != null) {
    b.sslContext(getSslContextBuilder(workerOptions).build());
  }

  if (execServer != null) {
    b.addService(ServerInterceptors.intercept(execServer, headersInterceptor));
  } else {
    logger.atInfo().log("Execution disabled, only serving cache requests");
  }

  Server server = b.build();
  logger.atInfo().log("Starting gRPC server on port %d", workerOptions.listenPort);
  server.start();

  return server;
}
 
Example 2
Source File: ConfirmedRpcApiService.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void start() {
    try {
        NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port)
                .addService(new DatabaseApi());

        Args args = Args.getInstance();

        if (args.getRpcThreadNum() > 0) {
            serverBuilder = serverBuilder
                    .executor(Executors.newFixedThreadPool(args.getRpcThreadNum()));
        }

        serverBuilder = serverBuilder.addService(new WalletConfirmedApi());

        // Set configs from config.conf or default value
        serverBuilder
                .maxConcurrentCallsPerConnection(args.getMaxConcurrentCallsPerConnection())
                .flowControlWindow(args.getFlowControlWindow())
                .maxConnectionIdle(args.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS)
                .maxConnectionAge(args.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS)
                .maxMessageSize(args.getMaxMessageSize())
                .maxHeaderListSize(args.getMaxHeaderListSize());

        apiServer = serverBuilder.build().start();
    } catch (IOException e) {
        logger.debug(e.getMessage(), e);
    }

    logger.info("ConfirmedRpcApiService started, listening on " + port);

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.err.println("*** shutting down gRPC server on confirmed since JVM is shutting down");
        //server.this.stop();
        System.err.println("*** server on confirmed shut down");
    }));
}
 
Example 3
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 4
Source File: RpcApiService.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void start() {
    try {
        NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port)
                .addService(databaseApi);

        Args args = Args.getInstance();

        if (args.getRpcThreadNum() > 0) {
            serverBuilder = serverBuilder
                    .executor(Executors.newFixedThreadPool(args.getRpcThreadNum()));
        }

        if (args.isConfirmedNode()) {
            serverBuilder = serverBuilder.addService(walletConfirmedApi);
            if (args.isWalletExtensionApi()) {
                serverBuilder = serverBuilder.addService(new WalletExtensionApi());
            }
        } else {
            serverBuilder = serverBuilder.addService(walletApi);
        }

        // Set configs from config.conf or default value
        serverBuilder
                .maxConcurrentCallsPerConnection(args.getMaxConcurrentCallsPerConnection())
                .flowControlWindow(args.getFlowControlWindow())
                .maxConnectionIdle(args.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS)
                .maxConnectionAge(args.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS)
                .maxMessageSize(args.getMaxMessageSize())
                .maxHeaderListSize(args.getMaxHeaderListSize());

        apiServer = serverBuilder.build().start();
    } catch (IOException e) {
        logger.debug(e.getMessage(), e);
    }

    logger.info("RpcApiService started, listening on " + port);

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.err.println("*** shutting down gRPC server since JVM is shutting down");
        //server.this.stop();
        System.err.println("*** server shut down");
    }));
}