/** * @return the port that server is bound to, or null if the server is not bound to an address yet. */ public int getBindPort() { return mServer.getPort(); }
public int getPort() { return server.getPort(); } }
@Override public void start() throws IOException { registerShutdownHook(); server.start(); logger.info("grpc: Server started, listening on " + server.getPort()); }
@Override public void run(String... args) throws Exception { log.info("Starting gRPC Server ..."); Collection<ServerInterceptor> globalInterceptors = getBeanNamesByTypeWithAnnotation(GRpcGlobalInterceptor.class, ServerInterceptor.class) .map(name -> applicationContext.getBeanFactory().getBean(name, ServerInterceptor.class)) .collect(Collectors.toList()); // Adding health service serverBuilder.addService(healthStatusManager.getHealthService()); // find and register all GRpcService-enabled beans getBeanNamesByTypeWithAnnotation(GRpcService.class, BindableService.class) .forEach(name -> { BindableService srv = applicationContext.getBeanFactory().getBean(name, BindableService.class); ServerServiceDefinition serviceDefinition = srv.bindService(); GRpcService gRpcServiceAnn = applicationContext.findAnnotationOnBean(name, GRpcService.class); serviceDefinition = bindInterceptors(serviceDefinition, gRpcServiceAnn, globalInterceptors); serverBuilder.addService(serviceDefinition); String serviceName = serviceDefinition.getServiceDescriptor().getName(); healthStatusManager.setStatus(serviceName, HealthCheckResponse.ServingStatus.SERVING); log.info("'{}' service has been registered.", srv.getClass().getName()); }); if (gRpcServerProperties.isEnableReflection()) { serverBuilder.addService(ProtoReflectionService.newInstance()); log.info("'{}' service has been registered.", ProtoReflectionService.class.getName()); } configurer.configure(serverBuilder); server = serverBuilder.build().start(); applicationContext.publishEvent(new GRpcServerInitializedEvent(server)); log.info("gRPC Server started, listening on port {}.", server.getPort()); startDaemonAwaitThread(); }
@Test public void releaseOnSuccess() { // Setup server final Server server = startServer((req, observer) -> { observer.onNext("response"); observer.onCompleted(); }); // Make Client call final Channel channel = NettyChannelBuilder.forAddress("localhost", server.getPort()) .usePlaintext(true) .build(); ClientCalls.blockingUnaryCall(channel, METHOD_DESCRIPTOR, CallOptions.DEFAULT, "foo"); Mockito.verify(limiter, Mockito.times(1)).acquire(Mockito.isA(GrpcServerRequestContext.class)); Mockito.verify(listener, Mockito.times(1)).onSuccess(); }
@Test public void releaseOnUncaughtException() throws IOException { // Setup server final Server server = startServer((req, observer) -> { throw new RuntimeException("failure"); }); // Make Client call final Channel channel = NettyChannelBuilder.forAddress("localhost", server.getPort()) .usePlaintext(true) .build(); try { ClientCalls.blockingUnaryCall(channel, METHOD_DESCRIPTOR, CallOptions.DEFAULT, "foo"); Assert.fail("Should have failed with UNKNOWN error"); } catch (StatusRuntimeException e) { // Verify Mockito.verify(limiter, Mockito.times(1)).acquire(Mockito.isA(GrpcServerRequestContext.class)); Mockito.verify(listener, Mockito.times(1)).onIgnore(); } } }
.build(); Channel channel = NettyChannelBuilder.forTarget("localhost:" + server.getPort()) .usePlaintext(true) .intercept(new ConcurrencyLimitClientInterceptor(limiter))
@Test public void releaseOnError() { // Setup server final Server server = startServer((req, observer) -> { observer.onError(Status.INVALID_ARGUMENT.asRuntimeException()); }); // Make Client call final Channel channel = NettyChannelBuilder.forAddress("localhost", server.getPort()) .usePlaintext(true) .build(); try { ClientCalls.blockingUnaryCall(channel, METHOD_DESCRIPTOR, CallOptions.DEFAULT, "foo"); Assert.fail("Should have failed with UNKNOWN error"); } catch (StatusRuntimeException e) { // Verify Assert.assertEquals(Status.Code.INVALID_ARGUMENT, e.getStatus().getCode()); Mockito.verify(limiter, Mockito.times(1)).acquire(Mockito.isA(GrpcServerRequestContext.class)); Mockito.verify(listener, Mockito.times(1)).onIgnore(); } }
@Override public int getPort() { return actual.server.getPort(); }
/** * Returns the actual port of the running gRPC server. The port is only available once the server is up and running. * * @throws IllegalStateException if called either before the server has started or after it has stopped */ public final int getPort() { final Server server = server(); if (server == null) { throw new IllegalStateException("Cannot fetch port until server has started."); } return server.getPort(); }
/** * Returns the actual port of the running gRPC server. The port is only available once the server is up and running. * * @throws IllegalStateException if called either before the server has started or after it has stopped */ public final int getPort() { final Server server = server(); if (server == null) { throw new IllegalStateException("Cannot fetch port until server has started."); } return server.getPort(); }
public final int getGrpcPort() { return runConfigure().grpcExternalServerRef.get().getPort(); }
public int getPort() { return server.getPort(); }
private void onApplicationEvent(ConfigurableApplicationContext applicationContext,GRpcServerInitializedEvent event) { MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources(); PropertySource<?> source = sources.get("server.ports"); if (source == null) { source = new MapPropertySource("server.ports", new HashMap<>()); sources.addFirst(source); } ((Map<String, Object>) source.getSource()).put("local.grpc.port", event.getServer().getPort()); }
private static void startGRPCServer(MessageDispatcher messageDispatcher) throws IOException, InterruptedException { Server server; LspServer lspServer = new LspServer(messageDispatcher); server = ServerBuilder.forPort(0).addService(lspServer).build(); lspServer.addServer(server); server.start(); int port = server.getPort(); System.out.println("Listening on port:" + port); server.awaitTermination(); }
private static void startGRPCServer(MessageDispatcher messageDispatcher) throws IOException, InterruptedException { Server server; LspServer lspServer = new LspServer(messageDispatcher); server = ServerBuilder.forPort(0).addService(lspServer).build(); lspServer.addServer(server); server.start(); int port = server.getPort(); System.out.println("Listening on port:" + port); server.awaitTermination(); }
@Test public void testDirectSuccessUsingIPAddress() throws Exception { final int serverPort1 = testRPCServer1.getPort(); final int serverPort2 = testRPCServer2.getPort(); final int serverPort3 = testRPCServer3.getPort(); // Directly use all 3 servers and verify. String localIP = InetAddress.getLoopbackAddress().getHostAddress(); @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor(); final ControllerImpl controllerClient = new ControllerImpl( ControllerImplConfig.builder() .clientConfig(ClientConfig.builder().controllerURI(URI.create("tcp://" + localIP + ":" + serverPort1 + "," + localIP + ":" + serverPort2 + "," + localIP + ":" + serverPort3)).build()) .retryAttempts(1).build(), executor); final Set<PravegaNodeUri> uris = fetchFromServers(controllerClient, 3); Assert.assertEquals(3, uris.size()); }
@Test public void testDirectSuccess() throws Exception { final int serverPort1 = testRPCServer1.getPort(); final int serverPort2 = testRPCServer2.getPort(); final int serverPort3 = testRPCServer3.getPort(); // Directly use all 3 servers and verify. @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor(); final ControllerImpl controllerClient = new ControllerImpl( ControllerImplConfig.builder() .clientConfig(ClientConfig.builder().controllerURI(URI.create("tcp://localhost:" + serverPort1 + ",localhost:" + serverPort2 + ",localhost:" + serverPort3)).build()) .retryAttempts(1).build(), executor); final Set<PravegaNodeUri> uris = fetchFromServers(controllerClient, 3); Assert.assertEquals(3, uris.size()); }
@Test public void testDiscoverySuccessUsingIPAddress() throws Exception { final int serverPort1 = testRPCServer1.getPort(); final int serverPort2 = testRPCServer2.getPort(); // Use 2 servers to discover all the servers. String localIP = InetAddress.getLoopbackAddress().getHostAddress(); @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor(); final ControllerImpl controllerClient = new ControllerImpl( ControllerImplConfig.builder() .clientConfig(ClientConfig.builder().controllerURI(URI.create("pravega://" + localIP + ":" + serverPort1 + "," + localIP + ":" + serverPort2)).build()) .retryAttempts(1).build(), executor); final Set<PravegaNodeUri> uris = fetchFromServers(controllerClient, 3); // Verify we could reach all 3 controllers. Assert.assertEquals(3, uris.size()); }
@Test public void testDiscoverySuccess() throws Exception { final int serverPort1 = testRPCServer1.getPort(); final int serverPort2 = testRPCServer2.getPort(); // Use 2 servers to discover all the servers. @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor(); final ControllerImpl controllerClient = new ControllerImpl( ControllerImplConfig.builder() .clientConfig(ClientConfig.builder().controllerURI(URI.create("pravega://localhost:" + serverPort1 + ",localhost:" + serverPort2)).build()) .retryAttempts(1).build(), executor); final Set<PravegaNodeUri> uris = fetchFromServers(controllerClient, 3); // Verify we could reach all 3 controllers. Assert.assertEquals(3, uris.size()); }