4.14. Health Check¶
Table of Contents
4.14.1. Overview¶
4.14.1.1. Load sharing and fallback of load balancer¶
4.14.1.2. Types of health checks¶
Sr. No. | Types of health checks | Details |
---|---|---|
(1)
|
Health check in PING
|
Verify operational status at the network layer level of OSI reference model. PING is sent to a server (OS) and determined as “server running” if appropriate response is received from the server.
|
(2)
|
Health check in TCP/UDP
|
Verify operational status in transport layer level of OSI reference model. A request is sent to TCP port (or UDP port) of Web/AP server and determined as “server running” if the appropriate response is received from the server.
|
(3)
|
Health check in application
|
Verify operational status at the application layer level of OSI reference model. A HTTP request is sent to application running on Web/AP server and determined as “running” if the appropriate response is received from the server.
|
4.14.1.3. Structure of health check shown in the guideline¶
Sr. No. | Description |
---|---|
(1)
|
Run Controller, Service and Repository by receiving a request from LB.
A method to perform a simple health check also exists when only operational status is to be verified. However, this guideline also verifies whether the structure and framework used by the application is running correctly, using the health check and implements Controller, Service and Repository to achieve technological configuration of targeted application as much as possible.
|
(2)
|
Issue SQL from Repository and verify that the database is running.
This is because, in an application with database access, an operation cannot be performed normally if an abnormality occurs in database, even if the application itself is running.
|
(3)
|
Use JSP as a View which returns a response.
Although this guideline explains about JSP, communication method and response format should be changed appropriately in accordance with the characteristics of application, while using REST or SOAP. For details, refer RESTful Web Serviceor SOAP Web Service (Server/Client).
|
Health check process results | Status code | Response details |
---|---|---|
Success
|
200 (Normal)
|
3 characters of
OK. |
Error occurrence
|
Status code set by exception handling function
|
Response set by exception handling function
|
4.14.2. How to use¶
4.14.2.1. Repository interface¶
HealthCheckRepository
is created. HealthCheckRepository
executes SQL for health check and verifies database operation.HealthCheckRepository.java
package com.example.domain.repository.healthcheck;
public interface HealthCheckRepository {
void healthcheck();
}
- It should be a reference system
- It should not require parameters
HealthCheckRepository.xml(when PostgreSQL is used)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.domain.repository.healthcheck.HealthCheckRepository">
<select id="healthcheck" resultType="String">
SELECT '1'
</select>
</mapper>
HealthCheckRepository.xml(When Oracle is used)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.domain.repository.healthcheck.HealthCheckRepository">
<select id="healthcheck" resultType="String">
SELECT '1' FROM DUAL
</select>
</mapper>
4.14.2.2. Service class¶
HealthCheckService
interface and a HealthCheckServiceImpl
class which implements HealthCheckService
interface are created.HealthCheckServiceImpl
calls healthcheck
method of healthcheckRepository
and performs health check for the database.HealthCheckService.java
package com.example.domain.service.healthcheck;
public interface HealthCheckService {
void healthcheck();
}
HealthCheckServiceImpl.java
package com.example.domain.service.healthcheck;
import healthcheck.domain.repository.healthcheck.HealthCheckRepository;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class HealthCheckServiceImpl implements HealthCheckService {
@Inject
HealthCheckRepository healthcheckRepository;
@Override
public void healthcheck() {
healthcheckRepository.healthcheck();
}
}
4.14.2.3. Controller class¶
HealthCheckController
is created.healthcheck
method of HealthcheckService
is called and transition is done to the specified path based on execution results. When it is verified that “database is running”, a view to display OK.
is returned.HealthCheckController.java
package com.example.app.healthcheck;
import healthcheck.domain.service.healthcheck.HealthCheckService;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HealthCheckController {
@Inject
HealthCheckService healthcheckService;
@RequestMapping(value = "healthcheck") // (1)
public String healthcheck(){
healthcheckService.healthcheck();
return "common/healthcheck/ok"; // (2)
}
}
Sr. No. Description (1)value
attribute acts as a URL for health check in order to verify operational status. (2) The directory where JSP file is placed, is kept one layer deep so as not to receive configuration of Apache Tiles. For details, refer Should not receive settings of Apache Tiles.
4.14.2.4. JSP file¶
<%@page>
directive and OK.
as shown below.ok.jsp
<%@page contentType="text/plain; charset=utf-8" language="java" pageEncoding="utf-8" %>OK.
4.14.2.5. Configuring access rights¶
<sec:intercept-url>
of spring-security.xml is configured for enabling the access for any role./common/healthcheck
is shown below.spring-security.xml
<sec:http>
<sec:intercept-url pattern="/healthcheck/**" access="permitAll"/>
<!-- omitted -->
</sec:http>
Note
If authorization control is removed, URL for health check can be accessed by anyone. Hence, countermeasures like using LB to prevent external access are required.
4.14.3. Appendix¶
4.14.3.1. Configuration for minimising the data volume for response¶
- Apache Tiles settings should not be received
- Header file and footer file should not be read
- Additional line breaks are to be removed from the response
Respective implementation examples for above are shown below.
4.14.3.1.1. Should not receive settings of Apache Tiles¶
<put-attribute>
tag of tiles-definitions.xml so as not to receive Apache Tiles configuration./WEB-INF/views/{1}/{2}.jsp
, a directory which is a level deep in the hierarchy is created and JSP file is placed under /WEB-INF/views/common/healthcheck/
.4.14.3.1.3. Remove additional line breaks from response¶
<%@taglib>
directive is set at the beginning of JSP, in order to use tag library, additional line breaks are output at the beginning of response.trimDirectiveWhitespaces
attribute is set in <%@page>
directive and output of additional line breaks in ok.jsp is prevented.ok.jsp (while setting trimDirectiveWhitespaces attribute)
<%@page contentType="text/plain; charset=utf-8" language="java" pageEncoding="utf-8" trimDirectiveWhitespaces="true" %>OK.
Note
/WEB-INF/views/common/include.jsp
is configured in <include-prelude>
of web.xml, in the default configuration of Blank project.
Hence, it is necessary to configure as shown above or modify <url-pattern>
so as not to include ok.jsp.
If these actions are not taken, include.jsp is read by entire JSP file and line breaks are output in ok.jsp.
Warning
When WebLogic is used, line breaks are not removed when an additional character exists before <%@page>
directive, even when trimDirectiveWhitespaces
attribute is configured as shown above.
Hence, a different method should be used.
As an example, how to configure a <trim-directive-whitespaces>
tag in <jsp-property-group>
tag of web.xml is shown below.
web.xml(while setting <trim-directive-whitespaces> tag)
<jsp-config> <jsp-property-group> <url-pattern>/WEB-INF/views/common/healthcheck/ok.jsp</url-pattern> // (1) <el-ignored>false</el-ignored> <page-encoding>UTF-8</page-encoding> <scripting-invalid>false</scripting-invalid> <trim-directive-whitespaces>true</trim-directive-whitespaces> // (2) </jsp-property-group> </jsp-config>
Sr. No. Description (1) Specify only ok.jsp in<url-pattern>
tag in order to apply<trim-directive-whitespaces>
tag only in ok.jsp. (2) Remove additional line breaks from targeted jsp file (ok.jsp) by settingtrue
in<trim-directive-whitespaces>
tag.