- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 384字
- 2021-07-02 19:45:06
Adding a custom health module
Adding a new custom module to the Spring Boot application is not so complex. For demonstrating 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 code 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);
}
void increment(){
count.increment();
}
}
The preceding code is a simple Plain Old Java Object (POJO) class, which maintains the transaction counts window. The isWeak method checks whether the transaction in a particular window has reached its threshold. isExpired checks whether the current window is expired or not. The increment method simply increases the counter value.
As the next step, implement our custom health indicator class, TPSHealth. This is done by extending HealthIndicatoras follows:
@Component
class TPSHealth implements HealthIndicator {
TPSCounter counter;
@Override
public Health health() {
boolean health = counter.isWeak();
if (health) {
return Health.outOfService()
.withDetail("Too many requests", "OutofService")
.build();
}
return Health.up().build();
}
void updateTx(){
if(counter == null || counter.isExpired()){
counter = new TPSCounter();
}
counter.increment();
}
}
The health method checks whether the counter isWeak or not. If it is weak, it marks the instance as out of service.
Finally, we autowire TPSHealth into the GreetingController class, and then call health.updateTx() in the greet method, like this:
Greet greet(){
logger.info("Serving Request....!!!");
health.updateTx();
return new Greet("Hello World!");
}
Go to the /application/health endpoint in the HAL browser, and see the status of the server.
Now open another browser, point to http://localhost:8080, and fire the service twice or thrice. Go back to the /application/health endpoint, and refresh to see the status. It would have been changed to out of service.
In this example, since there is no action taken other than collecting the health status, new service calls will still go through even though the status is out of service. But in the real world, a program should read the /application/health endpoint, and block further requests going to that instance.
- Instant Node Package Manager
- Git Version Control Cookbook
- Docker進階與實戰
- C/C++算法從菜鳥到達人
- 實戰低代碼
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- 重學Java設計模式
- 正則表達式經典實例(第2版)
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Node.js區塊鏈開發
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- Getting Started with hapi.js
- Distributed Computing with Python
- PHP高性能開發:基礎、框架與項目實戰