5

I have an application that is a eureka discovery client.

It registers with my Eureka server just fine.

Sadly when I try to autowire the EurekaClient in any @Component or @Service class to fetch registry instance details, I'm told that I can't autowire as there are 2 bean definitions:

enter image description here

I don't know what is causing this. I have the following dependencies in my pom.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

...

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>`

The exclusion in the first dependency is due to conflicts with the second.

My app is a discovery enabled app through the following configuration class:

@Configuration
@EnableDiscoveryClient
public class EurekaDiscoveryConfiguration {
}

I really don't understand why it thinks there are 2 beans matching that description.

FWIW, strangely enough I can autowire it in classes that are not annotated with @Component or @Service... and they work - i've managed to get them returning instance info correctly.

I have a feeling, and i've read a bit around it, that using jersey 2.0 for my services interferes with eureka. I might just swap to consul......

Many thanks in advance for any advice.

EDIT: (added test class)

This doesn't tell you much - it's just the base application test

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyAppApplicationTest {

    @Test
    public void contextLoads() {
    }
}

... lots of tests extend that class but it fails all by itself. It breaks trying to build the resource class that I've currently got the EurekaClient autowired in.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.discovery.EurekaClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Daisy Day
  • 652
  • 7
  • 19
  • Is it just Intellij that gives you this error or the exception is thrown when you run the application? – Maciej Walkowiak Oct 31 '17 at 23:19
  • Good point. It will compile fine and it will build fine without tests. However if I build with full tests, the tests which run with @RunWith(SpringRunner.class) @SpringBootTest break during context startup with the following: "Unsatisfied dependency expressed through field 'discoveryClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.discovery.EurekaClient' available..." - it will also run fine as a spring boot application. Maybe it's just a test context problem and I can switch off the warning when I've fixed? – Daisy Day Nov 01 '17 at 06:57
  • Update the question with a test class. I think we are getting close to the root cause. – Maciej Walkowiak Nov 01 '17 at 07:29
  • Sorry for the delay - just added test + some of the error. – Daisy Day Nov 02 '17 at 16:55
  • I know it has been a while but did you manage to resolve this, any tips? THanks – gaukhar Jan 10 '19 at 18:28
  • Honestly, I started finding building from the command line solved this issue. – Daisy Day Jan 11 '19 at 22:58
  • That's actually a terrible answer sorry. I had significant problems here but I might have been mistaken - I think it was idea complaining. It was quite some time ago. You can disable those warnings or at least stop them from.affecting tour build. – Daisy Day Jan 11 '19 at 23:01

2 Answers2

0

The bean is not available if you disable the Eureka client by setting

eureka.client.enabled=false

Depending on the using class, you could annotate it with @ConditionalOnProperty("eureka.client.enabled") so that it is only loaded when the Eureka client is enabled.

Another solution is setting

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

as explained in issue #1511 of spring-cloud-netflix.

0

Years on, this still happens.

I'm answering my own question now because it's clearly an IDEA issue. There are no build or run issues surrounding it and if you disable the inspection, the program is fine. I don't understand why IDEA takes exception to Eureka Client specifically.

Daisy Day
  • 652
  • 7
  • 19