Microservice 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 cntrol microservice overhead. To tell JLupin that your microservice is available or not just set system property called jlupin.user.available. If your miroservice 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 tak 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).

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).

Native microservice

Just change your microservice Spring configuration file:

package com.example.configuration;

import com.jlupin.impl.client.util.JLupinClientUtil;
import com.jlupin.interfaces.client.delegator.JLupinDelegator;
import com.jlupin.interfaces.common.enums.PortType;
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 avaiability in this example
@EnableScheduling
// end
public class AvailableNativeSpringConfiguration {
    private final Logger logger = LoggerFactory.getLogger(AvailableNativeSpringConfiguration.class);

    @Bean
    public JLupinDelegator getJLupinDelegator() {
        return JLupinClientUtil.generateInnerMicroserviceLoadBalancerDelegator(PortType.JLRMC);
    }

    @Bean(name = "jLupinRegularExpressionToRemotelyEnabled")
    public List getRemotelyBeanList() {
        List<String> list = new ArrayList<>();
        // list.add("<REMOTE_SERVICE_NAME>");
        return list;
    }

    // start - this is code fragment responsible for microservice avaiability 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
}

Servlet microservice

Just change your microservice Spring configuration file:

package com.example.configuration;

import com.jlupin.impl.client.util.JLupinClientUtil;
import com.jlupin.interfaces.client.delegator.JLupinDelegator;
import com.jlupin.interfaces.common.enums.PortType;
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;

@Configuration
@ComponentScan({
        "com.example",
        "com.jlupin.servlet.monitor.configuration"
})
// start - this is code fragment responsible for microservice avaiability in this example
@EnableScheduling
// end
public class AvailableServletSpringConfiguration {
    private final Logger logger = LoggerFactory.getLogger(AvailableServletSpringConfiguration.class);

    private boolean available = true;

    @Bean
    public JLupinDelegator getJLupinDelegator() {
        return JLupinClientUtil.generateInnerMicroserviceLoadBalancerDelegator(PortType.JLRMC);
    }

    // start - this is code fragment responsible for microservice avaiability 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
}