在spring boot中找不到DiscoveryClient bean错误

时间:2017-03-16 21:27:12

标签: spring-boot netflix-eureka

2017-03-16 16:09:08.821  INFO 9104 --- [           main] com.hello.EurekaClientApplication        : No active profile set, falling back to default profiles: default
2017-03-16 16:09:08.848  INFO 9104 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5dcd8c7a: startup date [Thu Mar 16 16:09:08 CDT 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@441772e
2017-03-16 16:09:09.873  INFO 9104 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'hystrixFeature' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration$HystrixWebConfiguration; factoryMethodName=hystrixFeature; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration$HystrixWebConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration; factoryMethodName=hystrixFeature; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.class]]
2017-03-16 16:09:10.364  WARN 9104 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as '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' annotation found and supported for autowiring
2017-03-16 16:09:11.312  INFO 9104 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$94394ff6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-16 16:09:12.091  INFO 9104 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-03-16 16:09:12.128  INFO 9104 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-03-16 16:09:12.130  INFO 9104 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-03-16 16:09:12.546  INFO 9104 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-03-16 16:09:12.547  INFO 9104 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3699 ms
2017-03-16 16:09:13.191  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'proxyStreamServlet' to [/proxy.stream]
2017-03-16 16:09:13.193  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-03-16 16:09:13.198  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2017-03-16 16:09:13.199  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-03-16 16:09:13.199  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-03-16 16:09:13.200  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-03-16 16:09:13.200  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-03-16 16:09:13.200  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2017-03-16 16:09:13.200  INFO 9104 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2017-03-16 16:09:13.269  WARN 9104 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eurekaClientApplication': Unsatisfied dependency expressed through field 'clientservice'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService' defined in file [C:\Users\Mike\workspace\Eureka_client\target\classes\com\hello\ClientService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.discovery.DiscoveryClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-03-16 16:09:13.306  INFO 9104 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-16 16:09:13.404  INFO 9104 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-16 16:09:13.745 ERROR 9104 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.hello.ClientService required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.


Action:

Consider defining a bean of type 'com.netflix.discovery.DiscoveryClient' in your configuration.
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。但我在代码

上收到此错误

4 个答案:

答案 0 :(得分:24)

您在代码中导入了错误的DiscoveryClient类。来自netflix的DiscoveryClient不是作为Spring Bean提供的。所以你应该使用spring-cloud中的一个。

尝试在org.springframework.cloud.client.discovery.DiscoveryClient课程中导入com.netflix.discovery.DiscoveryClient而不是ClientService

您还需要将this.disc.getInstancesById("a-bootiful-client")更改为this.disc.getInstances(...)

答案 1 :(得分:1)

这将是因为 2 个案例。

1:第一种情况是导入的包是否不匹配

如果您使用了 @EnableDiscoveryClient

import org.springframework.cloud.client.discovery.DiscoveryClient “代替 ” com.netflix.discovery.DiscoveryClient

如果您使用了 @EnableEurekaClient,请使用以下导入

import org.springframework.cloud.netflix.eureka.EnableEurekaClient

2:我在创建模块时没有选择 starer-web 依赖项,在 pom.xml{ 中添加了以下依赖项{1}} 用于获取添加的依赖项的项目。

rebuild

答案 2 :(得分:0)

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

如果要使用Spring Discovery客户端,请按照@ yongsung.yoon的建议使用org.springframework.cloud.client.discovery.DiscoveryClient

答案 3 :(得分:0)

我也遇到了同样的问题,我在添加spring boot-starter-web依赖后解决了