No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available

Divyanshi picture Divyanshi · Oct 26, 2017 · Viewed 7.4k times · Source

What else is required for DiscoveryClient? I am trying to fetch registered services to eureka using DiscoveryClient. The standalone program works well, but the following code snippet is to be integrated with a web application which runs on JBoss.

Here is the code

DiscoveryClientController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;

@Controller
public class DiscoveryClientController {
    
    @Autowired
    private DiscoveryClient discoveryClient;
        
    public DiscoveryClient getClient() {
        return discoveryClient;
    }
}

DiscoveryClientBean.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
    
@Configuration
public class DiscoveryClientBean {      
        
    @Bean
    public  DiscoveryClientController  discoveryClientController() {
        return  new DiscoveryClientController();
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>Discovery</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <!-- <sourceDirectory>src</sourceDirectory> -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <properties>
        <spring.version>4.3.8.RELEASE</spring.version>
        
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR3</spring-cloud.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <exclusions>
            <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            </exclusions>
            <!-- <version>1.3.4.RELEASE</version> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.6.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
            
        </dependency>
        

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

Answer

araknoid picture araknoid · Oct 26, 2017

The exception is telling you that you are missing the definition of the spring bean of type org.springframework.cloud.client.discovery.DiscoveryClient. In this particular case, you don't have to define one yourself, but you have to enable it with the @EnableDiscoveryClient annotation on your @SpringBootApplication annotated class.

If you need extra details, please take a moment to read Service Registration and Discovery