首页 文章

在spring boot中找不到DiscoveryClient bean错误

提问于
浏览
4
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 . 但我在代码上收到此错误

1 回答

  • 15

    您在代码中导入了错误的 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(...) .

相关问题