Search in sources :

Example 1 with EchoService

use of services.EchoService in project helidon by oracle.

the class MetricsIT method startGrpcServer.

// ----- helper methods -------------------------------------------------
/**
 * Start the gRPC Server listening on an ephemeral port.
 *
 * @throws Exception in case of an error
 */
private static void startGrpcServer() throws Exception {
    // Add the EchoService and enable GrpcMetrics
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcMetrics.timed()).register(new EchoService(), rules -> rules.intercept(GrpcMetrics.metered()).intercept("Echo", GrpcMetrics.counted())).build();
    // Run the server on port 0 so that it picks a free ephemeral port
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    LOGGER.info("Started gRPC server at: localhost:" + grpcServer.port());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClient(io.helidon.webclient.WebClient) JsonValue(jakarta.json.JsonValue) Channel(io.grpc.Channel) MediaType(io.helidon.common.http.MediaType) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) AfterAll(org.junit.jupiter.api.AfterAll) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonStructure(jakarta.json.JsonStructure) BeforeAll(org.junit.jupiter.api.BeforeAll) MetricsSupport(io.helidon.metrics.MetricsSupport) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LogConfig(io.helidon.common.LogConfig) EchoServiceGrpc(io.helidon.grpc.server.test.EchoServiceGrpc) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Echo(io.helidon.grpc.server.test.Echo) Logger(java.util.logging.Logger) GrpcRouting(io.helidon.grpc.server.GrpcRouting) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) EchoService(services.EchoService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) EchoService(services.EchoService) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 2 with EchoService

use of services.EchoService in project helidon by oracle.

the class SecurityFromConfigIT method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    // load the config containing the gRPC service security settings
    Config config = Config.builder().sources(ConfigSources.classpath("secure-services.conf")).build();
    // Create the gRPC routing configuring the GrpcSecurity interceptor from config
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcSecurity.create(config.get("security"))).register(new EchoService()).register(new StringService()).build();
    // Run the server on port 0 so that it picks a free ephemeral port
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    Channel channel = InProcessChannelBuilder.forName(grpcServer.configuration().name()).build();
    adminEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    adminStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    noCredsEchoStub = StringServiceGrpc.newBlockingStub(channel);
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) EchoService(services.EchoService) Channel(io.grpc.Channel) StringService(services.StringService) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 3 with EchoService

use of services.EchoService in project helidon by oracle.

the class ServiceAndMethodLevelSecurityIT method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    ServiceDescriptor echoService = ServiceDescriptor.builder(new EchoService()).intercept(GrpcSecurity.rolesAllowed("admin")).build();
    ServiceDescriptor stringService = ServiceDescriptor.builder(new StringService()).intercept("Upper", GrpcSecurity.rolesAllowed("admin")).intercept("Split", GrpcSecurity.rolesAllowed("admin")).build();
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(echoService).register(stringService).build();
    // Run the server on port 0 so that it picks a free ephemeral port
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    Channel channel = InProcessChannelBuilder.forName(grpcServer.configuration().name()).build();
    adminEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    adminStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    noCredsEchoStub = StringServiceGrpc.newBlockingStub(channel);
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) LogConfig(io.helidon.common.LogConfig) Config(io.helidon.config.Config) EchoService(services.EchoService) Channel(io.grpc.Channel) StringService(services.StringService) Security(io.helidon.security.Security) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with EchoService

use of services.EchoService in project helidon by oracle.

the class SslIT method startGrpcServer.

/**
 * Start the gRPC Server listening on the specified nPort.
 *
 * @throws Exception in case of an error
 */
private static GrpcServer startGrpcServer(int nPort, boolean mutual, boolean useConfig) throws Exception {
    Resource tlsCert = Resource.create(SERVER_CERT);
    Resource tlsKey = Resource.create(SERVER_KEY);
    Resource tlsCaCert = Resource.create(CA_CERT);
    GrpcTlsDescriptor sslConfig;
    String name = "grpc.server";
    if (useConfig) {
        name = name + 1;
        Config config = Config.builder().sources(ConfigSources.classpath("config-ssl.conf")).build();
        sslConfig = config.get("grpcserver.ssl").as(GrpcTlsDescriptor::create).get();
    } else if (mutual) {
        name = name + 2;
        sslConfig = GrpcTlsDescriptor.builder().jdkSSL(false).tlsCert(tlsCert).tlsKey(tlsKey).tlsCaCert(tlsCaCert).build();
    } else {
        name = name + 3;
        sslConfig = GrpcTlsDescriptor.builder().jdkSSL(false).tlsCert(tlsCert).tlsKey(tlsKey).build();
    }
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().register(new EchoService()).build();
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().name(name).port(nPort).tlsConfig(sslConfig).build();
    GrpcServer grpcServer;
    try {
        grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    LOGGER.info("Started gRPC server at: localhost:" + grpcServer.port());
    return grpcServer;
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) LogConfig(io.helidon.common.LogConfig) Config(io.helidon.config.Config) EchoService(services.EchoService) Resource(io.helidon.common.configurable.Resource) GrpcTlsDescriptor(io.helidon.grpc.core.GrpcTlsDescriptor)

Example 5 with EchoService

use of services.EchoService in project helidon by oracle.

the class TracingIT method startGrpcServer.

// ----- helper methods -------------------------------------------------
/**
 * Start the gRPC Server listening on an ephemeral port.
 *
 * @throws Exception in case of an error
 */
private static void startGrpcServer() throws Exception {
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().register(new EchoService()).intercept(interceptor).build();
    // Enable tracing
    Tracer tracer = TracerBuilder.create("Server").collectorUri(URI.create(zipkin.httpUrl() + "/api/v2/spans")).build();
    GrpcTracingConfig tracingConfig = GrpcTracingConfig.builder().withStreaming().withVerbosity().withTracedAttributes(ServerRequestAttribute.CALL_ATTRIBUTES, ServerRequestAttribute.HEADERS, ServerRequestAttribute.METHOD_NAME).build();
    // Run the server on port 0 so that it picks a free ephemeral port
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).tracer(tracer).tracingConfig(tracingConfig).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    LOGGER.info("Started gRPC server at: localhost:" + grpcServer.port());
}
Also used : EchoService(services.EchoService) Tracer(io.opentracing.Tracer)

Aggregations

EchoService (services.EchoService)5 LogConfig (io.helidon.common.LogConfig)4 Channel (io.grpc.Channel)3 Config (io.helidon.config.Config)3 GrpcRouting (io.helidon.grpc.server.GrpcRouting)3 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 StringService (services.StringService)2 ManagedChannelBuilder (io.grpc.ManagedChannelBuilder)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 Resource (io.helidon.common.configurable.Resource)1 MediaType (io.helidon.common.http.MediaType)1 GrpcTlsDescriptor (io.helidon.grpc.core.GrpcTlsDescriptor)1 GrpcServer (io.helidon.grpc.server.GrpcServer)1 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)1 Echo (io.helidon.grpc.server.test.Echo)1 EchoServiceGrpc (io.helidon.grpc.server.test.EchoServiceGrpc)1 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)1 MetricsSupport (io.helidon.metrics.MetricsSupport)1 Security (io.helidon.security.Security)1