Servlet microservice - controlling availability

You can control your microservice availability by yourself from inside of your microservice. This way you can decide if requests will be redirected or not to your microservice instance and better control microservice overhead. To tell JLupin that your microservice is available or not just set system property called jlupin.user.available. If your microservice is available use "1" as value, otherwise use "-1". Value of property jlupin.user.available is cast to boolean value and passed to script defined under key checkAvailableScript in microservice configuration file (configuration.yml or servlet_configuration.yml). Remember that this script must take value of this flag into consideration when availability is calculated. By default if userAvailableFlag is false then returned is false value (which means microservice is unavailable).

Default implementation of availability script:

function isAvailable(checkResponseTimeInMillis, jrmcActiveThreads, jrmcMaxThreads,
                     queueActiveThreads, queueMaxThreads, servletActiveThreads, servletMaxThreads,
                     jvmMaxMemoryInBytes, jvmTotalMemoryInBytes, jvmFreeMemoryInBytes,
                     jvmProcessCpuLoadInPercentage, userAvailableFlag) {
  var isAvailableByUser = Boolean(userAvailableFlag);
  if(checkResponseTimeInMillis > 20000 || !isAvailableByUser) {
    return false;
  }
  return true;
}

Of course you can use any parameter you want. For example you can check if microservice's JVM process CPU load is too high and set it as unavailable. This way traffic will go to other microservices for better balancing resources.

Configuration of how often this script is executed can be found in main server configuration file main.yml. Parameter responsible for this is named howOftenCheckingMicroservicesInMillis. You can read more about load balancer configuration here.

There is also second option to set microservice availability. If you microservice provides URL which can be requested to determine its availability (200 status response means available, otherwise unavailable) you can configure JLupin Edge balancer to use this URL instead of getting this information from main server. To do so change microservice configuration file, and set:

[...]
PROPERTIES:
  [...]
  isExternalHealthcheck: true
  externalHealthcheckURI: '/[your-url]'
[...]

Where [your-url] is URL defined for healthchecking. Here you can read more about JLupin Edge Balancer configuration.

Configuring microservice to set property jlupin.user.available

The simplest way is to use Spring built in scheduling system to periodically check your microservice status and update property jlupin.user.available. Below example sets microservice availability every 30 seconds but to hard coded value true. Remember to implement your own logic to determine if microservice is available (like checking if other microservices are available).

Just change your microservice Spring configuration file:

package com.example.springboot;

import com.jlupin.servlet.monitor.annotation.EnableJLupinSpringBootServletMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
@EnableJLupinSpringBootServletMonitor
// start - this is code fragment responsible for microservice availability in this example
@EnableScheduling
// end
public class ExampleController {
    private final static Logger logger = LoggerFactory.getLogger(ExampleController.class);

    @RequestMapping("/")
    @ResponseBody
    String home() {
        logger.info("called home() method");
        return "Hello JLupin";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleController.class, args);
    }

    // start - this is code fragment responsible for microservice availability in this example
    @Scheduled(fixedRate = 30000)
    public void setAvailable() {
        logger.info("Setting available flag...");

        // This is a place for your logic to determine if your microservice is available.
        // In this example it is set to true without computing.
        final boolean isAvailable = true;
        // This is end of logic.

        if (isAvailable) {
            System.setProperty("jlupin.user.available", "1");
        } else {
            System.setProperty("jlupin.user.available", "-1");
        }

        logger.info("Current value of jlupin.user.available is '" + System.getProperty("jlupin.user.available") + "'.");
    }
    // end
}