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,如下:
Spring Cloud Eureka-服务注册与发现

项目生成的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.   
  6.  <groupId>org.zero.eureka</groupId>
  7.  <artifactId>client</artifactId>
  8.  <version>0.0.1-SNAPSHOT</version>
  9.  <packaging>jar</packaging>
  10.  
  11.  <name>client</name>
  12.  <description>Demo project for Spring Boot</description>
  13.  
  14.  <parent>
  15.  <groupId>org.springframework.boot</groupId>
  16.  <artifactId>spring-boot-starter-parent</artifactId>
  17.  <version>2.0.4.RELEASE</version>
  18.  <relativePath/> <!-- lookup parent from repository -->
  19.  </parent>
  20.   
  21.  <properties>
  22.  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23.  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24.  <java.version>1.8</java.version>
  25.  <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  26.  </properties>
  27.  
  28.  <dependencies>
  29.  <dependency>
  30.  <groupId>org.springframework.cloud</groupId>
  31.  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32.  </dependency>
  33.   
  34.  <dependency>
  35.  <groupId>org.springframework.boot</groupId>
  36.  <artifactId>spring-boot-starter-test</artifactId>
  37.  <scope>test</scope>
  38.  </dependency>
  39.  </dependencies>
  40.   
  41.  <dependencyManagement>
  42.  <dependencies>
  43.  <dependency>
  44.  <groupId>org.springframework.cloud</groupId>
  45.  <artifactId>spring-cloud-dependencies</artifactId>
  46.  <version>${spring-cloud.version}</version> 
  47. <type>pom</type>
  48.  <scope>import</scope>
  49.  </dependency>
  50.  </dependencies>
  51.  </dependencyManagement>
  52.   
  53.  <build>
  54.  <plugins>
  55.  <plugin>
  56.  <groupId>org.springframework.boot</groupId>
  57.  <artifactId>spring-boot-maven-plugin</artifactId>
  58.  </plugin>
  59.  </plugins>
  60.  </build> 
  61. </project>

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

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

接着就是在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的信息面板中查看到已注册的实例信息,如下:
Spring Cloud Eureka-服务注册与发现

posted @ 2018-09-09 07:56  云端观云  阅读(17111)  评论(0编辑  收藏  举报