Quarkus Design Patterns
Quarkus is the latest addition to the Java world. This open-source framework initiated by Redhat has been talked about for some time. It improves start-up times, execution costs, increases productivity. IT industry slowly adapted Quarkus for Java projects, due to its high performance. This article focuses on implementing a microservice with Quarkus design patterns.
When you using a microservice in your project, its important to use it in a right way in a approach to the microservice architecture by using best practices as possible, Even if your breaking down your monolithic or to starting new project using microservices starting from scratch. So that’s why its important to apply some design patterns that will help you not only by approach microservices into right way, also having less effort when doing that. And offcourse a lot of Design patterns available, something around 40 to 50 . Here I’ll cover 5 most important design patterns. Microservices with some specific design patterns that can be a life saving for any project, and this article will show you the best of them in a live coding approach.
Below are some of the best design practices that will take you microservices project to the next level.
Externalized Configurations
Circuit Breaker
Health Check
Distributed Tracing
API Metrics
To build below quarkus project code, use the below command:
$ mvn compile quarkus:dev -Dconfig=quarkus
Externalized Configurations: It can be your external container, your server, or a cluster or whatever your application, it can get the configuration from the environment. Below is the sample class:
@Path(“/config”)
public class ConfigResource {
@ConfigProperty(name = “config”)
private Optional<String> config;@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getConfig(){
return Response.ok(“Hello, “ + config.orElse(“Config”)).build() ;
}
}
Circuit Breaker:
@Path(“/circuit”)
public class CircuitResource {
@Timeout(unit = ChronoUnit.MILLIS, value = 500)
@Fallback(fallbackMethod = “fallback”)
@CircuitBreaker(requestVolumeThreshold = 4, failureRatio = 0.5, delay = 2000, delayUnit = ChronoUnit.MILLIS, successThreshold = 2)
@GET
public String getCircuit() throws InterruptedException {
Thread.sleep(600);
return “Circuit \n”;
}public String fallback(){
return “I’m a fallback “;
}
}
Health Check: It has two part LivenessCheck & ReadinessCheck.
@Liveness
public class LivenessCheck implements HealthCheck {@Override
public HealthCheckResponse call() {
return HealthCheckResponse.up(“I’m live!”);
}
}@Readiness
public class ReadinessCheck implements HealthCheck {@Override
public HealthCheckResponse call() {
Client client = ClientBuilder.newClient();
Response response = client.target(“https://myredhar.com").request().get();if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)){
return HealthCheckResponse.up(“I’m ready!”);
} else{
return HealthCheckResponse.down(“I’m not ready yet…”);
}
}
}
Distributed Tracing:
@Path(“/trace”)
public class TraceResource {
@GET
@Traced
public String getTrace(){
return “Trace”;
}
}
API Metrics:
@Path(“/mymetrics”)
public class MetricsResource {
@GET
@Path(“counted”)
@Counted
public String getCounted(){
return “Counted”;
}@GET
@Path(“timed”)
@Timed
public String getTimed(){
return “Timed”;
}
}
Conclusion:
I hope this is useful for you. Remember when you are using standards we have a lot of hard lifting of our code being reduced by standards. That’s why we are using Quarkus with various design patterns.