Spring Boot 中找不到 DiscoveryClient bean 错误

12
2017-03-16 16:09:08.821 INFO 9104 --- [main] com.hello.EurekaClientApplication : 没有设置活动配置文件,回退到默认配置:default 2017-03-16 16:09:08.848 INFO 9104 --- [main] ationConfigEmbeddedWebApplicationContext : 正在刷新 org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5dcd8c7a:启动日期[Thu Mar 16 16:09:08 CDT 2017];父级:org.springframework.context.annotation.AnnotationConfigApplicationContext@441772e 2017-03-16 16:09:09.873 INFO 9104 --- [main] o.s.b.f.s.DefaultListableBeanFactory : 用不同的定义替换bean 'hystrixFeature'的定义:替换[Root bean:class [null];范围=;抽象= false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration$HystrixWebConfiguration;factoryMethodName=hystrixFeature;initMethodName=null;destroyMethodName=(已推断);定义在类路径资源[org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration$HystrixWebConfiguration.class]]与[Root bean:class [null];范围=;抽象= false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration;factoryMethodName=hystrixFeature;initMethodName=null;destroyMethodName=(已推断);定义在类路径资源[org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.class]] 2017-03-16 16:09:10.364 WARN 9104 --- [main] o.s.c.a.ConfigurationClassPostProcessor : 无法增强@Configuration bean定义'refreshScope',因为它的单例实例创建得太早。典型原因是一个非静态@Bean方法,带有BeanDefinitionRegistryPostProcessor返回类型:考虑将这些方法声明为'static'。 2017-03-16 16:09:10.701 INFO 9104 --- [main] o.s.cloud.context.scope.GenericScope : BeanFactory id=d0eb8cfd-bd5b-3565-9f63-f671e896f6be 2017-03-16 16:09:10.736 INFO 9104 --- [main] f.a.AutowiredAnnotationBeanPostProcessor : 发现并支持JSR-330 'javax.inject.Inject'注释进行自动装配 2017-03-16 16:09:11.312 INFO 9104 --- [main] trationDelegate$BeanPostProcessorChecker : 类型为[org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration $ $ EnhancerBySpringCGLIB $ $ 94394ff6]的Bean'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration'不适合被所有BeanPostProcessors处理(例如:不适合自动代理) 2017-03-16 16:09:12.091 INFO 9104 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat初始化端口:8080(http) 2017-03-16 16:09:12.128 INFO 9104 --- [main] o.apache.catalina.core.StandardService : 正在启动Tomcat 2017-03-16 16:09:12.130 INFO 9104 --- [main] org.apache.catalina.core.StandardEngine : 正在启动Servlet引擎:Apache Tomcat/8.5.11 2017-03-16 16:09:12.546 INFO 9104 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : 初始化Spring嵌入式WebApplicationContext 2017-03-16 16:09:12.547 INFO 9104 --- [ost-startStop-1] o.s.web.context.ContextLoader : 根WebApplicationContext:初始化完成,用时3699毫秒 2017-03-16 16:09:13.191 INFO 9104 --- [ost-start
package com.hello;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.stereotype.Service;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@EnableDiscoveryClient
@Service
public class ClientService {

private final DiscoveryClient disc;

    public ClientService(DiscoveryClient disc){
        this.disc=disc;
    }



    @HystrixCommand(fallbackMethod="disp")
    public String serviceInstancesByApplicationName(){
        return this.disc.getInstancesById("a-bootiful-client").toString();
    }

    public String disp(){
        return "This is fall back method";
    }
}




package com.hello;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


@EnableHystrix
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@RestController
public class EurekaClientApplication {


    @Autowired
    private ClientService clientservice;




    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

    @RequestMapping("/serv")
    public String serviceInstancesByApplicationName(){
        return clientservice.serviceInstancesByApplicationName();
    }
}

我试图创建一个简单的Eureka服务和客户端程序,并在其中启用Hystrix。但是,在代码中我遇到了这个错误。


SO不是一个调试服务。请添加文本,解释您尝试了什么来调查问题。说明您的代码的哪个部分似乎按照您想要的方式工作,哪个部分不起作用。展示一些努力,否则看起来像您只是倾倒了一个大错误消息,并期望其他人为您调试代码。 - Stefan
5个回答

39

你在代码中导入了错误的 DiscoveryClient 类。Netflix 的 DiscoveryClient 并没有作为 Spring Bean 提供。因此,你应该使用来自 spring-cloud 的一个。

尝试在你的 ClientService 类中导入 org.springframework.cloud.client.discovery.DiscoveryClient,而不是 com.netflix.discovery.DiscoveryClient

另外你需要将 this.disc.getInstancesById("a-bootiful-client") 更改为 this.disc.getInstances(...)


9
这可能是由两种情况引起的。
1:第一种情况是导入的包不匹配。
如果您使用了@EnableDiscoveryClient,请使用以下导入: import org.springframework.cloud.client.discovery.DiscoveryClient 而不是 com.netflix.discovery.DiscoveryClient 如果您使用了@EnableEurekaClient,请使用以下导入: import org.springframework.cloud.netflix.eureka.EnableEurekaClient 2:当我创建模块时没有选择starter-web依赖项,在pom.xml中添加了以下依赖项,并进行重建项目,以获取添加的依赖项。
  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

4

请使用 com.netflix.discovery.EurekaClient 替代 com.netflix.discovery.DiscoveryClient。这是由Netflix提供的发现客户端。

如果您想要使用Spring发现客户端,请使用 @yongsung.yoon 建议的 org.springframework.cloud.client.discovery.DiscoveryClient


3
我也遇到了同样的问题,我在添加Spring Boot Starter Web依赖后解决了它。

0
你可以在项目中检查两件事情。
  1. 检查你是否在pom.xml中添加了spring-web依赖。

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

检查您是否已在主要的Spring Boot应用程序类上添加了以下2个注解:

1. @EnableEurekaClient
   from import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

2. @EnableDiscoveryClient
   from import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接