When deploying a Service Registry client app that also contains a OSS Spring Cloud Gateway dependency, you will see the following error messages:
2019-12-20T10:24:22.21+0100 [APP/PROC/WEB/0] OUT 2019-12-20 09:24:22.216 WARN 34 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'eurekaInstanceConfigBean' defined in class path resource [io/pivotal/spring/cloud/service/eureka/EurekaInstanceAutoConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=io.pivotal.spring.cloud.service.eureka.EurekaInstanceAutoConfiguration; factoryMethodName=eurekaInstanceConfigBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [io/pivotal/spring/cloud/service/eureka/EurekaInstanceAutoConfiguration.class]] for bean 'eurekaInstanceConfigBean': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; factoryMethodName=eurekaInstanceConfigBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]] bound.
2019-12-20T10:24:22.23+0100 [APP/PROC/WEB/0] OUT The bean 'eurekaInstanceConfigBean', defined in class path resource [io/pivotal/spring/cloud/service/eureka/EurekaInstanceAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class] and overriding is disabled. 2019-12-20T10:24:22.23+0100 [APP/PROC/WEB/0] OUT Action: 2019-12-20T10:24:22.23+0100 [APP/PROC/WEB/0] OUT Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
This is due an incompatibility between OSS Spring Cloud Gateway and Spring MVC, that is included in Service Registry dependency. They are incompatible because Spring Cloud Gateway is using Spring's Reactive Web support (Spring WebFlux) which conflicts with Spring's Servlet based support (Spring Web MVC).
This can be solved in three different ways as long as the
org.springframework.boot:spring-boot-starter-web
dependency is not included in the project:
1. Exclude the OSS
EurekaClientAutoConfiguration.class
, which can be done adding the exclude clause to your configuration class as follows:
@EnableAutoConfiguration(exclude={EurekaClientAutoConfiguration.class})
.
2. Exclude the
spring-cloud-netflix-eureka-client
artifact by changing the following:
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-service-registry</artifactId>
</dependency>
With:
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-service-registry</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</exclusion>
</exclusions>
</dependency>
3. Enabling bean overriding by setting:
spring.main.allow-bean-definition-overriding=true
Note:
If the
org.springframework.boot:spring-boot-starter-web
dependency is included in the project there will be no way to by pass the incompatibility and you would need to either split the app into two different ones or use
reactive web
instead of Spring MVC.