Microservices start order

Sometimes one microservice requires another one to work properly. Furthermore it is required that one microservice will start before the other one. In JLupin it is possible to order your microservices start. But how it works?

Start order

While server is starting and is configured to start microservices one by one (not all at once) it must determine order in which microservices are started. How it is done? Every microservice inside it's configuration file (configuration.yml) contains parameter named priorityStartOnMainServerInitialize. Main server reads all microservices' configuration files and group them by this parameter. Then in ascending order groups are taken and microservices are started one by one again, but they are taken from proper groups. Inside group order is not determined so not depend on it. But it is certain that if one microservice is lower value of priorityStartOnMainServerInitialize it will be started earlier.

Remember that this order works only during start of whole server!

Example

So let's assume we have four microservices: A, B, C, D. With standard configuration but with priorityStartOnMainServerInitialize set as shown below:

Microservice name value of priorityStartOnMainServerInitialize
A 5
B 15
C 5
D 10

While starting server will group them by priority so we will get:

5:  A, C
15: B
10: D

It will be sorted by priority ascending:

5:  A, C
10: D
15: B

And example start order could be:

1. A
2. C
3. D
4. B

It is also possible that order would be:

1. C
2. A
3. D
4. B

It is due to fact that A and C have same priority set. But it is not possible that microservice A would start after B or D. It is also not possible that B would start after D.

Load balancers

Load balancers are started after whole initialization phase is done - which means that all microservices are started. But some operation are taken during microservice start. It is also possible that microservice need to call other microservice during initialization. If so load balancers are required to be started manually during initialization phase. It is very simple - just call start() method on load balancer object:

@Configuration
@ComponentScan("com.example")
public class ExampleSpringConfiguration {
    @Bean
    public JLupinDelegator getJLupinDelegator() throws JLupinDelegatorException {
        JLupinDelegator jlupinDelegator = JLupinClientUtil.generateInnerMicroserviceLoadBalancerDelegator(PortType.JLRMC);
        jlupinDelegator.start();
        return jlupinDelegator;
    }
    ...

This way its repository will be populated with already started microservices. Just remember to make sure that required microservice was started earlier!