Native 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 paramter 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.
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.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.ArrayList;
import java.util.List;
@Configuration
@ComponentScan("com.example")
// start - this is code fragment responsible for microservice availability in this example
@EnableScheduling
// end
public class SpringConfiguration {
private final Logger logger = LoggerFactory.getLogger(SpringConfiguration.class);
@Bean(name = "jLupinRegularExpressionToRemotelyEnabled")
public List getRemotelyBeanList() {
List<String> list = new ArrayList<>();
list.add("exampleService");
return list;
}
// 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
}