How to do it...

Your starter project will have some pre-built classes. We will create two other classes to provide the health check endpoints for your newly-created microservice:

  1. First, we'll create a class named LivenessHealthCheck. It's pretty simple, so its full source code is here:
@Liveness
@ApplicationScoped
public class LivenessHealthCheck implements HealthCheck{
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.up("I'm up!");
}
}
  1. Then, let's build and run the project. Start by building and packaging it:
mvn package
  1. Then, execute this command to run it:
java -jar target/ch08-mphealthcheck.jar

If everything went fine, you should see something like this in your Terminal:

  1. Open a browser and navigate to this URL:
http://localhost:8080/health/live

If it's working, you will see something close to this:

If this is the case, your liveness health check is working! Now, let's create the readiness one:

  1. Create a class named ReadinessHealthCheck and code it like this:
@Readiness
@ApplicationScoped
public class ReadinessHealthCheck implements HealthCheck {
@Override
public HealthCheckResponse call() {
if (isAcessible()){
return HealthCheckResponse.up("I'm up and ready!");
} else{
return HealthCheckResponse.down("I'm up, but not ready...");
}
}

private boolean isAcessible(){
return new Random().nextBoolean();
}
}
  1. Again, let's build and run it:
mvn package
java -jar target/ch08-mphealthcheck.jar
  1. Now, you have a new URL to check another kind of health state:
http://localhost:8080/health/ready

If it's working, you will see something like this:

Note that as it's just a simulation, your service will randomly return UP or DOWN each time that you call the URL:

  1. You can also just call the root health check endpoint:
http://localhost:8080/health

It will show you both endpoints (/ready and /live) in the same result:

If you got here successfully, congratulations! You are now able to manage the state of this microservice through the Eclipse MicroProfile Health Check API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221