没有类型为“org.springframework.cloud.client.discovery.DiscoveryClient'”的合格bean可得到

时间:2017-10-26 07:58:44

标签: java spring client netflix-eureka discovery

DiscoveryClient还需要什么? 我正在尝试使用DiscoveryClient将注册服务提取到eureka。 独立程序运行良好,但以下代码片段将与在jboss上运行的Web应用程序集成。

这是代码 - 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>

1 个答案:

答案 0 :(得分:2)

异常告诉您缺少类型org.springframework.cloud.client.discovery.DiscoveryClient的spring bean的定义。 在这种特殊情况下,您不必自己定义一个,但必须在@EnableDiscoveryClient带注释的类上使用@SpringBootApplication注释启用它。

如果您需要更多详细信息,请花点时间阅读Service Registration and Discovery