use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.
the class GrpcServerCdiExtension method startServer.
private void startServer(@Observes @Initialized(ApplicationScoped.class) Object event, BeanManager beanManager) {
GrpcRouting.Builder routingBuilder = discoverGrpcRouting(beanManager);
Config config = MpConfig.toHelidonConfig(ConfigProvider.getConfig());
GrpcServerConfiguration.Builder serverConfiguration = GrpcServerConfiguration.builder(config.get("grpc"));
CompletableFuture<GrpcServer> startedFuture = new CompletableFuture<>();
CompletableFuture<GrpcServer> shutdownFuture = new CompletableFuture<>();
loadExtensions(beanManager, config, routingBuilder, serverConfiguration, startedFuture, shutdownFuture);
server = GrpcServer.create(serverConfiguration.build(), routingBuilder.build());
long beforeT = System.nanoTime();
server.start().whenComplete((grpcServer, throwable) -> {
if (null != throwable) {
STARTUP_LOGGER.log(Level.SEVERE, throwable, () -> "gRPC server startup failed");
startedFuture.completeExceptionally(throwable);
} else {
long t = TimeUnit.MILLISECONDS.convert(System.nanoTime() - beforeT, TimeUnit.NANOSECONDS);
int port = grpcServer.port();
STARTUP_LOGGER.finest("gRPC server started up");
LOGGER.info(() -> "gRPC server started on localhost:" + port + " (and all other host addresses) " + "in " + t + " milliseconds.");
grpcServer.whenShutdown().whenComplete((server, error) -> {
if (error == null) {
shutdownFuture.complete(server);
} else {
shutdownFuture.completeExceptionally(error);
}
});
startedFuture.complete(grpcServer);
}
});
// inject the server into the producer so that it can be discovered later
ServerProducer serverProducer = beanManager.createInstance().select(ServerProducer.class).get();
serverProducer.server(server);
}
use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.
the class MetricsTest method startServer.
@BeforeAll
public static void startServer() throws Exception {
LogConfig.configureRuntime();
server = Server.create().start();
beanManager = CDI.current().getBeanManager();
client = ClientBuilder.newBuilder().register(new LoggingFeature(LOGGER, Level.WARNING, LoggingFeature.Verbosity.PAYLOAD_ANY, 500)).property(ClientProperties.FOLLOW_REDIRECTS, true).build();
instance = beanManager.createInstance();
GrpcServer grpcServer = instance.select(GrpcServerCdiExtension.ServerProducer.class).get().server();
channel = ManagedChannelBuilder.forAddress("localhost", grpcServer.port()).usePlaintext().build();
stringStub = StringServiceGrpc.newBlockingStub(channel);
}
use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.
the class GrpcServerCdiExtensionTest method shouldStartServerAndLoadServicesAndExtensions.
@Test
public void shouldStartServerAndLoadServicesAndExtensions() {
Instance<GrpcServerCdiExtension.ServerProducer> instance = beanManager.createInstance().select(GrpcServerCdiExtension.ServerProducer.class);
// verify that the GrpcServerCdiExtension.ServerProducer producer bean is registered
assertThat(instance.isResolvable(), is(true));
// obtain the started server from the producer
GrpcServer grpcServer = instance.get().server();
assertThat(grpcServer, is(CoreMatchers.notNullValue()));
assertThat(grpcServer.isRunning(), is(true));
// verify that the services are deployed
Map<String, ServiceDescriptor> services = grpcServer.services();
// UnaryService should have been discovered by CDI
assertThat(services.get("UnaryService"), is(CoreMatchers.notNullValue()));
// ServerStreamingService loaded by ExtensionOne discovered by CDI
assertThat(services.get("ServerStreamingService"), is(CoreMatchers.notNullValue()));
// TestService loaded by ExtensionTwo loaded by the ServiceLoader
assertThat(services.get("TestService"), is(CoreMatchers.notNullValue()));
}
use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.
the class SecureServer method createGrpcServer.
/**
* Create the gRPC server.
*/
private static GrpcServer createGrpcServer(Config config, Security security) {
GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(new StringService(), GrpcSecurity.rolesAllowed("admin")).register(new GreetService()).build();
GrpcServer grpcServer = GrpcServer.create(GrpcServerConfiguration.create(config), grpcRouting);
grpcServer.start().thenAccept(s -> {
System.out.println("gRPC server is UP! http://localhost:" + s.port());
s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
}).exceptionally(t -> {
System.err.println("gRPC server startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
return grpcServer;
}
use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.
the class SecureServer method main.
/**
* Main entry point.
*
* @param args the program arguments
*/
public static void main(String[] args) {
LogConfig.configureRuntime();
Config config = Config.create();
Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
ServiceDescriptor greetService1 = ServiceDescriptor.builder(new GreetService(config)).name("GreetService").intercept(GrpcSecurity.rolesAllowed("user")).intercept("SetGreeting", GrpcSecurity.rolesAllowed("admin")).build();
GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(greetService1).register(new StringService()).build();
GrpcServerConfiguration serverConfig = GrpcServerConfiguration.create(config.get("grpc"));
GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
grpcServer.start().thenAccept(s -> {
System.out.println("gRPC server is UP! http://localhost:" + s.port());
s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
}).exceptionally(t -> {
System.err.println("Startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
}
Aggregations