API Health check

Micro service API’s need a way of providing liveness events to Clients and Gateway applications. Application frameworks such as Spring Boot provide mechanisms for enabling Health Check implementation automatically through the Actuator.

In Mulesoft a simple mechanism like the one below can be added to a common layer and reused in your API’s.

    <flow name="health-check">
          <http:listener config-ref="HTTP_API_Listener" path="/health" doc:name="HTTP">
            <http:response-builder statusCode="200" reasonPhrase="Health check success!"/>
          </http:listener>

          <set-payload value="Micro Service on #[InetAddress.getLocalHost().getHostName()] reply: ... Success." doc:name="Set Payload"/>
    </flow>

The Health check request invoked from the Browser, CURL or other tools like Httpie will timeout if the micro service is unavailable, or respond with the HTTP Header and content similar to below.

$ curl https://myhost.com/health

HTTP/1.1 200 Health check success!
Content-Length: 74

reply: ... Success.

Health checks are an integral component to API Gateways in determining service availability.

Another handy feature to consider, is to bake into your micro service the ability of responding with a static example if for example, an HTTP query term like test is added to the inbound request.

The example below shows a qualifier you might add to a Choice statement ans well as a static responder that might return a JSON response.

<!-- sample query term to direct the production of a static sample response -->
message.inboundProperties.'Mule-Route' == 'test'

<!-- static page responder -->
<set-payload value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('POST-Page02-200.json')]" doc:name="Post Document"/>
<set-variable variableName="RESPONSE_STATUS" value="#[201]" doc:name="Status"/> 

The response you return in the file POST-Page02-200.json above would be a static JSON response which client applications can use as a Mock implementation of the interface contract.

{
  "Item": "Neural Compute Stick 2",
  "Quantity": "42",
  "Color": "CyberBlue",
  "Accelerators": [ "CPU", "GPU", "VPU", "FGPA"]
}

We hope you found this short tutorial interesting and wish you the best in your endeavors.

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Published by Mitch Dresdner

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Leave a comment

You can comment using your social media account

%d bloggers like this: