【问题标题】:Spring boot application not able to find bean for feign clientSpring Boot 应用程序无法为 feign 客户端找到 bean
【发布时间】:2020-04-17 22:55:12
【问题描述】:

在测试 feign 功能时遇到异常。
************************
应用程序启动失败
************************

说明:

com.in28minutes.microservices.currencyconversionservice.CurrencyConversionController 中的字段 currencyConversionServiceProxy 需要找不到类型为“com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy”的 bean。

注入点有以下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

POM.xml

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
....
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</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>

CurrencyConversionServiceApplication.java

@SpringBootApplication
@EnableFeignClients
public class CurrencyConversionServiceApplication {

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

CurrencyConversionController.java

@RestController
public class CurrencyConversionController {

  @Autowired
  private CurrencyConversionServiceProxy currencyConversionServiceProxy;

  @GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
  public CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to,
        @PathVariable BigDecimal quantity) {

      CurrencyConversionBean response = currencyConversionServiceProxy.retrieveExchangeValue(from, to);
      return new CurrencyConversionBean(response.getId(), response.getFrom(), response.getTo(),
            response.getConversionMultiple(), quantity, quantity.multiply(response.getConversionMultiple()),
            response.getPort());
  }
}

CurrencyConversionServiceProxy.java

@FeignClient(name="currency-exchange-service", url="localhost:8000")
public interface CurrencyConversionServiceProxy {

  @GetMapping("/currency-exchange/from/{from}/to/{to}")
  public CurrencyConversionBean retrieveExchangeValue(@PathVariable("from") String from, @PathVariable("to") String to);

}

日志

      .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2019-12-28 14:19:27.788  INFO 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Fetching config from server at : http://localhost:8888
2019-12-28 14:19:28.805  INFO 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2019-12-28 14:19:28.805  WARN 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/currency- 
conversion-service/default": Connection refused: connect; nested exception is 
java.net.ConnectException: Connection refused: connect
2019-12-28 14:19:28.805  INFO 11444 --- [  restartedMain] m.c.CurrencyConversionServiceApplication : 
No active profile set, falling back to default profiles: default
2019-12-28 14:19:28.921  INFO 11444 --- [  restartedMain] o.s.cloud.context.scope.GenericScope     : 
BeanFactory id=6fe0f2bd-0b38-367c-af3b-d79d3b2d9d52
2019-12-28 14:19:28.937  WARN 11444 --- [  restartedMain] org.apache.tomcat.util.modeler.Registry  : 
The MBean registry cannot be disabled because it has already been initialised
2019-12-28 14:19:28.968  INFO 11444 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : 
Tomcat initialized with port(s): 8100 (http)
2019-12-28 14:19:28.968  INFO 11444 --- [  restartedMain] o.apache.catalina.core.StandardService   : 
Starting service [Tomcat]
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : 
Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] o.a.c.c.C.[Tomcat-16].[localhost].[/]    : 
Initializing Spring embedded WebApplicationContext
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] o.s.web.context.ContextLoader            : 
Root WebApplicationContext: initialization completed in 178 ms
2019-12-28 14:19:28.999  WARN 11444 --- [  restartedMain] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'currencyConversionController': Unsatisfied dependency expressed through field 
'currencyConversionServiceProxy'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-12-28 14:19:29.015  INFO 11444 --- [  restartedMain] o.apache.catalina.core.StandardService   : 
Stopping service [Tomcat]
2019-12-28 14:19:29.015  INFO 11444 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 
'debug' enabled.
2019-12-28 14:19:29.093 ERROR 11444 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field currencyConversionServiceProxy in 
com.in28minutes.microservices.currencyconversionservice.CurrencyConversionController required a bean 
of type 'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' 
that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 
'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' in your 
configuration.

我已测试在端口 8000 上运行的服务仅在名称“currency-exchange-service”下运行良好。

【问题讨论】:

  • 我已经检查了相关查询,但它不起作用。 stackoverflow.com/questions/51485179/…
  • CurrencyConversionServiceProxy 是一个接口 - 它的实现在哪里?是否使用了正确的注释,例如 Qualifier?
  • @FeignClient 自己创建了这个接口的实现。下面的定义是我从它的文档中找到的。 “接口注释声明应创建具有该接口的 REST 客户端(例如,用于自动装配到另一个组件中)。”javadoc.io/doc/org.springframework.cloud/…
  • 将 Spring 云版本更改为“Finchley.RELEASE”为我工作。但我无法理解为什么它在“Hoxton.SR1”版本中不起作用
  • Hoxton.SR1 是一个非常新的版本,因此可能会解释您遇到的问题。很高兴听到你把它整理好了!

标签: spring-boot spring-cloud-netflix spring-cloud-feign feign


【解决方案1】:

问题在于服务器运行时添加了以下依赖项。 因此,在 pom.xml 中进行任何更改后手动重新启动服务器应该可以解决问题。

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

【讨论】:

    【解决方案2】:

    对我来说,Spring Cloud Dependency 的更新有所帮助。一般来说,保持 Spring Boot Dependency 和 Spring Cloud Dependency 为最新是很重要的。

    https://spring.io/projects/spring-cloud

    【讨论】:

      【解决方案3】:

      您可以使用此依赖项而不是仅使用 open-config。

      <!-- add by WanChengHe 20190507 Increase eureka dependencies -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
      
      <!-- Add a feign dependency for inter-service calls add by WanChengHe 20190508 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
      </dependency>
      

      【讨论】:

        【解决方案4】:

        尝试在@EnableFeignClients注解中指定一个Feign客户端的类名。例如,在您的情况下,它应该如下所示:

        @SpringBootApplication
        @EnableFeignClients(clients = {CurrencyConversionServiceProxy.class})
        public class CurrencyConversionServiceApplication
        

        【讨论】:

        • 如果您的客户端与您的应用程序不在同一个包结构中,那么您需要按照 Elizaveta 的建议指定clients,否则将找不到客户端。另一种选择是在@EnableFeignClients 注释的basePackages 属性中提供要扫描的包。
        【解决方案5】:

        对于 Hoxton.SR1

        在注解中添加FeignClient的实现类FeignClientsConfiguration.class。

        @FeignClient(name="currency-exchange-service", url="localhost:8000",configuration=FeignClientsConfiguration.class)
        

        【讨论】:

          【解决方案6】:

          我刚刚进行了 maven 更新和全新安装,之后它开始正常运行。

          【讨论】:

            【解决方案7】:

            遇到了类似的问题,这是由于找不到自动配置而导致的。客户端配置是在它们自己的模块中定义的,该模块由 Spring Boot 应用程序导入。解决方案是添加 resources/META-INF/spring.factories 文件,该文件具有:org.springframework.boot.autoconfigure.EnableAutoConfiguration=&lt;path-to-@Configuration @EnableFeignClients&gt;

            【讨论】:

              【解决方案8】:

              如果您使用的是父项目,请务必从父 POM 中删除

              <plugins>
                 <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>  
              

              【讨论】:

                【解决方案9】:

                您需要为 FeignClients 指定路径,因为 @EnableFeignClients 不会在内部使用组件扫描来识别应用程序中的 FeignClients

                soln->

                @EnableFeignClients("com.in28minutes.microservices.currencyconversionservice")
                

                试试下面提到的更新代码

                @SpringBootApplication
                @EnableFeignClients("com.in28minutes.microservices.currencyconversionservice")
                public class CurrencyConversionServiceApplication {
                
                  public static void main(String[] args) {
                     SpringApplication.run(CurrencyConversionServiceApplication.class, args);
                  }
                }
                

                【讨论】:

                • 为我工作:)
                猜你喜欢
                • 2018-12-31
                • 2017-01-27
                • 1970-01-01
                • 2020-01-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-01-21
                • 2020-07-25
                相关资源
                最近更新 更多