Adding a custom health module

Adding a new custom module to the Spring Boot application is not so complex. To demonstrate this feature, assume that if a service gets more than two transactions in a minute, then the server status will be set as Out of Service.

In order to customize this, we have to implement the HealthIndicator interface and override the health method. The following is a quick and dirty implementation to do the job:

class TPSCounter { LongAdder count; int threshold = 2; Calendar expiry = null; TPSCounter(){ this.count = new LongAdder(); this.expiry = Calendar.getInstance(); this.expiry.add(Calendar.MINUTE, 1); } boolean isExpired(){ return Calendar.getInstance().after(expiry); } boolean isWeak(){ return (count.intValue() > threshold); ...

Get Spring Microservices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.