EurekaClient项目启动报错Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e

时间:2023-03-09 01:28:09
EurekaClient项目启动报错Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e

Disconnected from the target VM, address: '127.0.0.1:51233', transport: 'socket'

Eureka Client的使用

使用IDEA创建一个Spring Initializr项目,在勾选模块的时候需要选择Eureka Discovery,如下:
EurekaClient项目启动报错Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e

项目生成的pom.xml文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  <modelVersion>4.0.0</modelVersion>
  5.  <groupId>org.zero.eureka</groupId>
  6.  <artifactId>client</artifactId>
  7.  <version>0.0.1-SNAPSHOT</version>
  8.  <packaging>jar</packaging>
  9.  <name>client</name>
  10.  <description>Demo project for Spring Boot</description>
  11.  <parent>
  12.  <groupId>org.springframework.boot</groupId>
  13.  <artifactId>spring-boot-starter-parent</artifactId>
  14.  <version>2.0.4.RELEASE</version>
  15.  <relativePath/> <!-- lookup parent from repository -->
  16.  </parent>
  17.  <properties>
  18.  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20.  <java.version>1.8</java.version>
  21.  <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22.  </properties>
  23.  <dependencies>
  24.  <dependency>
  25.  <groupId>org.springframework.cloud</groupId>
  26.  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  27.  </dependency>
  28.  <dependency>
  29.  <groupId>org.springframework.boot</groupId>
  30.  <artifactId>spring-boot-starter-test</artifactId>
  31.  <scope>test</scope>
  32.  </dependency>
  33.  </dependencies>
  34.  <dependencyManagement>
  35.  <dependencies>
  36.  <dependency>
  37.  <groupId>org.springframework.cloud</groupId>
  38.  <artifactId>spring-cloud-dependencies</artifactId>
  39.  <version>${spring-cloud.version}</version> 
  40. <type>pom</type>
  41.  <scope>import</scope>
  42.  </dependency>
  43.  </dependencies>
  44.  </dependencyManagement>
  45.  <build>
  46.  <plugins>
  47.  <plugin>
  48.  <groupId>org.springframework.boot</groupId>
  49.  <artifactId>spring-boot-maven-plugin</artifactId>
  50.  </plugin>
  51.  </plugins>
  52.  </build> 
  53. </project>

项目的依赖都加载完成后,在启动类中加上@EnableDiscoveryClient,声明这是一个eureka client,否则不会进行服务注册:

  1. package org.zero.eureka.client;
  2.  import org.springframework.boot.SpringApplication;
  3.  import org.springframework.boot.autoconfigure.SpringBootApplication;
  4.  import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient
  7.  public class ClientApplication {
  8.  public static void main(String[] args) {
  9.  SpringApplication.run(ClientApplication.class, args);
  10.  }
  11. }

接着就是在application.yml配置文件中,配置注册中心即eureka server的地址,以及项目的名称和启动端口号。如下:

  1.  eureka: 
  2.       client:
  3.    service-url:
  4.            defaultZone: http://localhost:8761/eureka/
  5.  spring:
  6.    application:
  7.      name: eureka-client
  8.  server:
  9.    port: 9088

完成以上配置后,即可启动项目。但是我这里启动项目的时候失败了,控制台输出如下警告信息:

Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

这是因为client里不包含Tomcat的依赖,所以Spring容器无法创建一些实例,从而导致项目无法启动,只需在pom.xml文件中,加上web依赖即可:

  1.  <dependency>
  2.  <groupId>org.springframework.boot</groupId>
  3.  <artifactId>spring-boot-starter-web</artifactId>
  4.  </dependency>

项目启动成功后,可以在eureka server的信息面板中查看到已注册的实例信息,如下:
EurekaClient项目启动报错Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e