Search in sources :

Example 1 with HealthSupport

use of io.helidon.health.HealthSupport in project helidon by oracle.

the class Main method main.

/**
 * Start the example. Prints endpoints to standard output.
 *
 * @param args not used
 */
public static void main(String[] args) {
    serverStartTime = System.currentTimeMillis();
    HealthSupport health = HealthSupport.builder().addLiveness(HealthChecks.healthChecks()).addReadiness(() -> HealthCheckResponse.named("exampleHealthCheck").up().withData("time", System.currentTimeMillis()).build()).addStartup(() -> HealthCheckResponse.named("exampleStartCheck").status(isStarted()).withData("time", System.currentTimeMillis()).build()).build();
    Routing routing = Routing.builder().register(health).get("/hello", (req, res) -> res.send("Hello World!")).build();
    WebServer ws = WebServer.create(routing);
    ws.start().thenApply(webServer -> {
        String endpoint = "http://localhost:" + webServer.port();
        System.out.println("Hello World started on " + endpoint + "/hello");
        System.out.println("Health checks available on " + endpoint + "/health");
        return null;
    });
}
Also used : HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) Duration(java.time.Duration) WebServer(io.helidon.webserver.WebServer) Routing(io.helidon.webserver.Routing) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer) HealthSupport(io.helidon.health.HealthSupport) Routing(io.helidon.webserver.Routing)

Example 2 with HealthSupport

use of io.helidon.health.HealthSupport in project helidon by oracle.

the class ConfigTest method runWithConfig.

private JsonObject runWithConfig(String configKey, int expectedStatus) throws InterruptedException, ExecutionException, TimeoutException {
    HealthSupport healthSupport = HealthSupport.builder().addLiveness(HealthChecks.healthChecks(testConfig.get(configKey + ".helidon.health"))).build();
    WebServer webServer = null;
    try {
        webServer = startServer(healthSupport);
        WebClientResponse response = webClientBuilder(webServer).build().get().accept(MediaType.APPLICATION_JSON).path("health/live").submit().await();
        assertThat("Normal health URL HTTP response", response.status().code(), is(expectedStatus));
        return response.content().as(JsonObject.class).await();
    } finally {
        if (webServer != null) {
            shutdownServer(webServer);
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) HealthSupport(io.helidon.health.HealthSupport) JsonObject(jakarta.json.JsonObject)

Example 3 with HealthSupport

use of io.helidon.health.HealthSupport in project helidon by oracle.

the class Server method main.

/**
 * The main program entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    // load logging configuration
    LogConfig.configureRuntime();
    // Get gRPC server config from the "grpc" section of application.yaml
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder(config.get("grpc")).build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, createRouting(config));
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    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;
    });
    // add support for standard and gRPC health checks
    HealthSupport health = HealthSupport.builder().addLiveness(HealthChecks.healthChecks()).addLiveness(grpcServer.healthChecks()).build();
    // start web server with health endpoint
    Routing routing = Routing.builder().register(health).build();
    WebServer.create(routing, config.get("webserver")).start().thenAccept(s -> {
        System.out.println("HTTP server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("HTTP 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) GreetServiceJava(io.helidon.grpc.examples.common.GreetServiceJava) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcServer(io.helidon.grpc.server.GrpcServer)

Example 4 with HealthSupport

use of io.helidon.health.HealthSupport in project helidon by oracle.

the class PokemonMain method createRouting.

/**
 * Creates new {@link io.helidon.webserver.Routing}.
 *
 * @param config configuration of this server
 * @return routing configured with JSON support, a health check, and a service
 */
private static Routing createRouting(Config config) {
    Config dbConfig = config.get("db");
    // Client services are added through a service loader - see mongoDB example for explicit services
    DbClient dbClient = DbClient.builder(dbConfig).build();
    // Some relational databases do not support DML statement as ping so using query which works for all of them
    HealthSupport health = HealthSupport.builder().addLiveness(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check"))).build();
    // Initialize database schema
    InitializeDb.init(dbClient);
    return Routing.builder().register(// Health at "/health"
    health).register(// Metrics at "/metrics"
    MetricsSupport.create()).register("/db", new PokemonService(dbClient)).build();
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport)

Example 5 with HealthSupport

use of io.helidon.health.HealthSupport in project helidon by oracle.

the class OciObjectStorageMain method main.

/**
 * Main method.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    // as I cannot share my configuration of OCI, let's combine the configuration
    // from my home directory with the one compiled into the jar
    // when running this example, you can either update the application.yaml in resources directory
    // or use the same approach
    Config config = buildConfig();
    Config ociConfig = config.get("oci");
    // this requires OCI configuration in the usual place
    // ~/.oci/config
    OciObjectStorageRx ociObjectStorage = OciObjectStorageRx.create(ociConfig);
    // the following parameters are required
    String bucketName = ociConfig.get("objectstorage").get("bucket").asString().get();
    String namespace = ociConfig.get("objectstorage").get("namespace").asString().get();
    // setup ObjectStorage health check
    HealthSupport health = HealthSupport.builder().addLiveness(OciObjectStorageHealthCheck.builder().ociObjectStorage(ociObjectStorage).addBucket(bucketName).namespace(namespace).build()).build();
    WebServer.builder().config(config.get("server")).routing(Routing.builder().register(health).register("/files", new ObjectStorageService(ociObjectStorage, bucketName))).build().start().await();
}
Also used : Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport) OciObjectStorageRx(io.helidon.integrations.oci.objectstorage.OciObjectStorageRx)

Aggregations

HealthSupport (io.helidon.health.HealthSupport)20 MetricsSupport (io.helidon.metrics.MetricsSupport)10 LogConfig (io.helidon.common.LogConfig)7 Config (io.helidon.config.Config)7 DbClient (io.helidon.dbclient.DbClient)4 WebServer (io.helidon.webserver.WebServer)4 Routing (io.helidon.webserver.Routing)3 HealthChecks (io.helidon.health.checks.HealthChecks)2 DbClientService (io.helidon.dbclient.DbClientService)1 DbClientHealthCheck (io.helidon.dbclient.health.DbClientHealthCheck)1 MovieRepository (io.helidon.examples.integrations.neo4j.se.domain.MovieRepository)1 GreetService (io.helidon.grpc.examples.common.GreetService)1 GreetServiceJava (io.helidon.grpc.examples.common.GreetServiceJava)1 StringService (io.helidon.grpc.examples.common.StringService)1 GrpcRouting (io.helidon.grpc.server.GrpcRouting)1 GrpcServer (io.helidon.grpc.server.GrpcServer)1 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)1 Neo4j (io.helidon.integrations.neo4j.Neo4j)1 Neo4jHealthCheck (io.helidon.integrations.neo4j.health.Neo4jHealthCheck)1 Neo4jMetricsSupport (io.helidon.integrations.neo4j.metrics.Neo4jMetricsSupport)1