本文介绍了没有类型为'org.springframework.cloud.client.discovery.DiscoveryClient'的合格Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是代码

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>

推荐答案

该异常告诉您,您缺少类型为 org.springframework.cloud.client.discovery.DiscoveryClient 的spring bean的定义.代码>.在这种情况下,您不必自己定义,但必须在带有 @SpringBootApplication 注释的类上使用 @EnableDiscoveryClient 批注启用它.

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

这篇关于没有类型为'org.springframework.cloud.client.discovery.DiscoveryClient'的合格Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 11:43