Search in sources :

Example 6 with GrpcServer

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);
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) MpConfig(io.helidon.config.mp.MpConfig) Config(io.helidon.config.Config) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 7 with GrpcServer

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);
}
Also used : GrpcServerCdiExtension(io.helidon.microprofile.grpc.server.GrpcServerCdiExtension) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) GrpcServer(io.helidon.grpc.server.GrpcServer) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with GrpcServer

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()));
}
Also used : ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) GrpcServer(io.helidon.grpc.server.GrpcServer) Test(org.junit.jupiter.api.Test)

Example 9 with GrpcServer

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;
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Context(io.helidon.common.context.Context) ResponseHelper.complete(io.helidon.grpc.core.ResponseHelper.complete) Channel(io.grpc.Channel) StringService(io.helidon.grpc.examples.common.StringService) StreamObserver(io.grpc.stub.StreamObserver) GrpcHelper(io.helidon.grpc.core.GrpcHelper) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Service(io.helidon.webserver.Service) Status(io.grpc.Status) LogConfig(io.helidon.common.LogConfig) Http(io.helidon.common.http.Http) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) Greet(io.helidon.grpc.examples.common.Greet) ServerRequest(io.helidon.webserver.ServerRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Strings(io.helidon.grpc.examples.common.Strings) GrpcService(io.helidon.grpc.server.GrpcService) StringServiceGrpc(io.helidon.grpc.examples.common.StringServiceGrpc) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 10 with 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;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Security(io.helidon.security.Security) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) GreetService(io.helidon.grpc.examples.common.GreetService) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) Security(io.helidon.security.Security) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Aggregations

GrpcServer (io.helidon.grpc.server.GrpcServer)12 GrpcRouting (io.helidon.grpc.server.GrpcRouting)10 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)10 LogConfig (io.helidon.common.LogConfig)8 Config (io.helidon.config.Config)7 StringService (io.helidon.grpc.examples.common.StringService)7 GreetService (io.helidon.grpc.examples.common.GreetService)4 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)4 Security (io.helidon.security.Security)4 GrpcSecurity (io.helidon.security.integration.grpc.GrpcSecurity)4 Routing (io.helidon.webserver.Routing)4 WebServer (io.helidon.webserver.WebServer)4 Channel (io.grpc.Channel)2 MetricsSupport (io.helidon.metrics.MetricsSupport)2 HttpBasicAuthProvider (io.helidon.security.providers.httpauth.HttpBasicAuthProvider)2 WebClient (io.helidon.webclient.WebClient)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Test (org.junit.jupiter.api.Test)2 ManagedChannelBuilder (io.grpc.ManagedChannelBuilder)1 Status (io.grpc.Status)1