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 is a Thought Leader and an Architect at Steampunk where he helps to deliver human-centered, secure digital, platforms. His interests span the gamut of application integration, scalable secure clusters, embedded systems, user interface and into the hobby space where he dabbles with drones, home wine making and other ferments.

Published by Mitch Dresdner

Mitch is a Thought Leader and an Architect at Steampunk where he helps to deliver human-centered, secure digital, platforms. His interests span the gamut of application integration, scalable secure clusters, embedded systems, user interface and into the hobby space where he dabbles with drones, home wine making and other ferments.

Leave a comment

Your email address will not be published. Required fields are marked *