public class HttpHealthCheckService extends AbstractHttpService
HttpService
that responds with HTTP status "200 OK"
if the server is healthy and can
accept requests and HTTP status "503 Service Not Available"
if the server is unhealthy and cannot
accept requests. The default behavior is to respond healthy after the server is started and unhealthy
after it started to stop.
Subclasses can override newHealthyResponse(ServiceRequestContext)
or
newUnhealthyResponse(ServiceRequestContext)
if they need to customize the response.
Server server = new ServerBuilder()
.defaultVirtualHost(new VirtualHostBuilder()
.service("/rpc", new THttpService(myHandler))
.service("/health", new HttpHealthCheckService())
.build())
.build();
You can also specify additional HealthChecker
s at construction time. It will respond with a
unhealthy response if the HealthChecker.isHealthy()
method returns false
for any of them.
This can be useful when you want to stop receiving requests from a load balancer without stopping a
Server
. e.g. the backend got unhealthy.
SettableHealthChecker healthChecker = new SettableHealthChecker();
Server server = new ServerBuilder()
.defaultVirtualHost(new VirtualHostBuilder()
.service("/rpc", new THttpService(myHandler))
.service("/health", new HttpHealthCheckService(healthChecker))
.build())
.build();
Constructor and Description |
---|
HttpHealthCheckService(HealthChecker... healthCheckers)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected HttpResponse |
doGet(ServiceRequestContext ctx,
HttpRequest req)
Handles a
GET request. |
protected HttpResponse |
doHead(ServiceRequestContext ctx,
HttpRequest req)
Handles a
HEAD request. |
protected AggregatedHttpMessage |
newHealthyResponse(ServiceRequestContext ctx)
Creates a new response which is sent when the
Server is healthy. |
protected AggregatedHttpMessage |
newUnhealthyResponse(ServiceRequestContext ctx)
Creates a new response which is sent when the
Server is unhealthy. |
void |
serviceAdded(ServiceConfig cfg)
|
doDelete, doDelete, doGet, doHead, doOptions, doOptions, doPatch, doPatch, doPost, doPost, doPut, doPut, doTrace, doTrace, serve
public HttpHealthCheckService(HealthChecker... healthCheckers)
healthCheckers
- the additional HealthChecker
sprotected AggregatedHttpMessage newHealthyResponse(ServiceRequestContext ctx)
Server
is healthy.protected AggregatedHttpMessage newUnhealthyResponse(ServiceRequestContext ctx)
Server
is unhealthy.public void serviceAdded(ServiceConfig cfg) throws Exception
Service
Service
has been added to a Server
with the specified configuration.
Please note that this method can be invoked more than once if this Service
has been added more
than once.Exception
protected HttpResponse doHead(ServiceRequestContext ctx, HttpRequest req)
AbstractHttpService
HEAD
request.
This method sends a 405 Method Not Allowed
response by default.doHead
in class AbstractHttpService
protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req)
AbstractHttpService
GET
request.
This method sends a 405 Method Not Allowed
response by default.doGet
in class AbstractHttpService
© Copyright 2015–2018 LINE Corporation. All rights reserved.