0

There are two microservices deployed with docker compose. A dependecy between services is defined in docker compose file by depends_on property. Is it possible to achieve the same effect implicitly, inside the spring boot application?

Let's say the microservice 1 depends on microservice 2. Which means, microsearvice 1 doesn't boot up before microservice 2 is healthy or registered on Eureka server.

1

1 Answer 1

1

By doing some research, I found a solution to the problem.

Spring Retry resolves dependency on Spring Cloud Config Server. Maven dependency spring-retry should be added into the pom.xml, and the properties below into the .properties file:

spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-interval=2000
spring.cloud.config.retry.max-attempts=10

The following configuration class is used to resolve dependency on other microservices.

@Configuration
@ConfigurationProperties(prefix = "depends-on")
@Data
@Log
public class DependsOnConfig {

    private List<String> services;
    private Integer periodMs = 2000;
    private Integer maxAttempts = 20;

    @Autowired
    private EurekaClient eurekaClient;

    @Bean
    public void dependentServicesRegisteredToEureka() throws Exception {
        if (services == null || services.isEmpty()) {
            log.info("No dependent services defined.");
            return;
        }
        log.info("Checking if dependent services are registered to eureka.");
        int attempts = 0;
        while (!services.isEmpty()) {
            services.removeIf(this::checkIfServiceIsRegistered);
            TimeUnit.MILLISECONDS.sleep(periodMs);
            if (maxAttempts.intValue() == ++attempts)
                throw new Exception("Max attempts exceeded.");
        }
    }

    private boolean checkIfServiceIsRegistered(String service) {
        try {
            eurekaClient.getNextServerFromEureka(service, false);
            log.info(service + " - registered.");
            return true;
        } catch (Exception e) {
            log.info(service + " - not registered yet.");
            return false;
        }
    }

}

A list of services that the current microservice depends on are defined in .properties file:

depends-on.services[0]=service-id-1 
depends-on.services[1]=service-id-2  

A bean dependentServicesRegisteredToEureka is not being initialized until all services from the list register to Eureka. If needed, annotation @DependsOn("dependentServicesRegisteredToEureka") can be added to beans or components to prevent attempting an initialization before dependentServicesRegisteredToEureka initialize.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, I found it useful to reduce eureka.client.registry-fetch-interval-seconds property to 10s because the default value is the 90s and it's too long to wait for registry refresh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.