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

2

6

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>
Cryobiology answered 26/10, 2017 at 7:58 Comment(2)
Have you gone through the guide? spring.io/guides/gs/service-registration-and-discovery From girst View I can see that several annotations are missing here, e.g. "@EnableDiscoveryClient" and "@EnableEurekaServer"Katmandu
@Katmandu A standalone program with annotation "@SpringBootApplication" works perfectly. The annotations you mentioned have been used in other classes. Are they required here as well? because when I executed a standalone program, i didn't use any such annotations.Cryobiology
U
4

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

Unconnected answered 26/10, 2017 at 8:42 Comment(0)
D
0

you can use this controller aside your bootstrap config:

@RestController
class ServiceInstanceRestController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }
}
Dwarf answered 3/3, 2022 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.