How to Register a Spring Cloud App to Multiple Service Registries Without Startup Errors
This article explains why a Spring Cloud application cannot register with multiple service registries by default, analyzes the startup failure caused by duplicate AutoServiceRegistration beans, and provides a step‑by‑step solution using configuration and auto‑configuration exclusion to enable successful multi‑registry registration.
Introduction
When using Spring Cloud to develop microservices, service registration is usually as simple as adding the registration dependency.
Why Multiple Registrations Fail
Adding two service‑registry dependencies (e.g., Nacos and Eureka) causes the application to fail at startup because the AutoServiceRegistrationAutoConfiguration class requires a single AutoServiceRegistration bean, but finds two (nacosAutoServiceRegistration and eurekaAutoServiceRegistration).
Root Cause
The failure is logged as an APPLICATION FAILED TO START error, indicating that the field autoServiceRegistration in AutoServiceRegistrationAutoConfiguration requires a single bean but two were found.
Solution: Exclude the Auto‑Configuration
Exclude
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfigurationvia spring.autoconfigure.exclude or
@SpringBootApplication(exclude = AutoServiceRegistrationAutoConfiguration.class). Add the following property to application.properties:
spring.autoconfigure.exclude=org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfigurationAfter exclusion, both Nacos and Eureka register successfully, as shown by the log entries confirming service registration on ports 18082.
Analysis of the init Method
The init method checks if autoServiceRegistration is null and if AutoServiceRegistrationProperties.failFast is true; if so, it throws an IllegalStateException. Since failFast defaults to false, the method normally does not abort.
Role of @Import
@Import(AutoServiceRegistrationConfiguration.class)creates the AutoServiceRegistrationProperties bean. The configuration is conditionally loaded by @ConditionalOnProperty (property spring.cloud.service-registry.auto-registration.enabled).
Discovery Client Import Selector
The EnableDiscoveryClientImportSelector adds AutoServiceRegistrationConfiguration to the import list unless autoRegister is set to false. Therefore, keeping @EnableDiscoveryClient without disabling autoRegister ensures the registration beans are created.
Additional Considerations
If spring-boot-starter-actuator is present, you may also need to exclude ServiceRegistryAutoConfiguration to avoid another startup error.
Practical Ways to Exclude Auto‑Configurations
Use
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)for direct exclusion.
Implement an AutoConfigurationImportFilter (e.g., RegistryExcludeFilter) and register it in META-INF/spring.factories to filter out specific auto‑configurations.
Use Cases
The main scenario for multi‑registry registration is migrating services from one registry to another without downtime. The provided edas-sc-migration-starter dependency bundles the necessary configuration for easy adoption.
Conclusion
Excluding AutoServiceRegistrationAutoConfiguration (and optionally ServiceRegistryAutoConfiguration) enables a Spring Cloud application to register with multiple service registries safely, with minimal side effects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
