public static void main(String[] args) { logger.info("Starting grpc server on port: " + SERVER_PORT); try { ServerBuilder.forPort(SERVER_PORT) .addService(new HelloServiceImpl()) .addService(ProtoReflectionService.newInstance()) .build() .start() .awaitTermination(); } catch (InterruptedException | IOException e) { logger.info("Caught exception, shutting down", e); System.exit(0); } }
/** Starts a grpc server on the given port, throws {@link IOException} on failure. */ private static Server tryStartServer( int port, TestServiceImplBase testService, Optional<SslContext> sslContext) throws IOException { NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port) .addService(testService) .addService(ProtoReflectionService.newInstance()); if (sslContext.isPresent()) { serverBuilder.sslContext(sslContext.get()); } return serverBuilder.build().start(); }
/** * Load gRPC specified service definition and then add to gRPC registry. * * @param serverBuilder NettyServerBuilder * @return the instance of NettyServerBuilder */ @SuppressWarnings("unchecked") protected T bindService(T serverBuilder) { List<ServiceDefinitionAdapter<?>> services = ServiceDefinitionLoader.loader().getServiceList(); for (ServiceDefinitionAdapter<?> service : services) { serverBuilder .addService(ServerInterceptors.intercept((ServerServiceDefinition)service.getServiceDefinition(), adaptedInterceptors)); } serverBuilder.addService(ProtoReflectionService.newInstance()); return serverBuilder; }