简体   繁体   English

Spring 启动微服务 - 依赖

[英]Spring Boot microservices - dependency

There are two microservices deployed with docker compose.使用 docker compose 部署了两个微服务。 A dependecy between services is defined in docker compose file by depends_on property.服务之间的依赖关系在 docker 组合文件中由depends_on属性定义。 Is it possible to achieve the same effect implicitly, inside the spring boot application?是否可以在 spring 引导应用程序中隐式实现相同的效果?

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 依赖于微服务 2。这意味着,在微服务 2 健康或在 Eureka 服务器上注册之前,微服务 1 不会启动。

By doing some research, I found a solution to the problem.通过一些研究,我找到了解决问题的方法。

Spring Retry resolves dependency on Spring Cloud Config Server. Spring 重试解决了对 Spring 云配置服务器的依赖。 Maven dependency spring-retry should be added into the pom.xml, and the properties below into the.properties file: Maven 依赖 spring-retry 应添加到 pom.xml 中,并将以下属性添加到 .properties 文件中:

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.以下配置 class 用于解决对其他微服务的依赖。

@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:当前微服务所依赖的服务列表定义在 .properties 文件中:

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.在列表中的所有服务都注册到 Eureka 之前,不会初始化dependentServicesRegisteredToEureka If needed, annotation @DependsOn("dependentServicesRegisteredToEureka") can be added to beans or components to prevent attempting an initialization before dependentServicesRegisteredToEureka initialize.如果需要,可以将注解@DependsOn("dependentServicesRegisteredToEureka")添加到 bean 或组件中,以防止在dependentServicesRegisteredToEureka初始化之前尝试初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM