1. About
Micronaut is an open-source JVM-based software framework for building lightweight, modular applications and microservices. It helps to create microservices with small memory footprints and quick startup time. We will create a Micronaut application using Java. You will learn how to use the Micronaut Management feature to enable your application’s “health” endpoint.
2. Pre-requisite
- Micronaut
- Gradle
- JDK 1.8 or greater installed with JAVA_HOME
- Enable annotation Processing.
3. Development
To create a Micronaut project just navigate to https://micronaut.io/launch/ and create your starter project.
Add micronaut dependency in build.gradle file, So it will expose a management/health endpoint.
// Add micronaut management dependency in build.gradle file
implementation("io.micronaut:micronaut-management")
The project structure will be as follows-
The Application.java will be as follows-
package com.example;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
The HelloController.java will be as follows-
package example.micronaut;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
@Controller("/hello") // <1>
public class HelloController {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello Controller called";
}
}
Configuration of application.yml file will be as follows-
micronaut:
application:
name: micronautguide
netty:
default:
allocator:
max-order: 3
The HelloControllerTest will be as follows-
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import jakarta.inject.Inject;
@MicronautTest // <1>
public class HelloControllerTest {
@Inject
@Client("/") // <2>
HttpClient client;
@Test
public void testHello() {
HttpRequest<String> request = HttpRequest.GET("/hello"); // <3>
String body = client.toBlocking().retrieve(request);
assertNotNull(body);
assertEquals("Hello World", body);
}
}
The HelloControllerTest will be as follows-
package com.example;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import jakarta.inject.Inject;
@MicronautTest
class MicronautguideTest {
@Inject
EmbeddedApplication<?> application;
@Test
void testItWorks() {
Assertions.assertTrue(application.isRunning());
}
}
Create HealthPathTest test file
- Sets the base path for all management endpoints to /endpoints/. It is normally specified in application configuration (e.g. application.yml).
- The “health” endpoint is now rooted to /endpoints/health.
- The “health” endpoint is no longer accessible on the default path and results in a “Not Found” status (HTTP 404).
Create HealthTest test file
- Annotate HealthTest class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server.
- Inject the HttpClient bean via @Inject and point it to the embedded server.
- Health endpoint returns information about the “health” of the application, as determined by any number of “health indicators”.
Create PoorHealthTest test file
- Sets the endpoints.health.disk-space.threshold property to an impossibly high value to force a service down status. It is normally specified in the application configuration (e.g. application.yml).
- Failed health check result in service unavailable status (HTTP 503).
- The error response contains json string {“status”:”DOWN”}
4. Work Flow
Run Application.java class
Open the terminal and execute the health endpoint via curl localhost:8080/health
Testing the Application
Run the tests via cmd ./gradlew test Then goto build/reports/tests/test/index.html and open in a browser to see the results.
If any test get failed
5. Conclusion
In this blog, we have covered Exposing a Health point for the Micronaut application. Now you are ready with the understanding of Exposing a Health point for the Micronaut application. For more, you can refer to the documentation: https://staging-e733-knoldus.wpcomstaging.com/micornaut-data-jpa-hibernate/