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,如下:
项目生成的pom.xml文件内容如下:
- 
- 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- 
<modelVersion>4.0.0</modelVersion>
- 
- 
<groupId>org.zero.eureka</groupId>
- 
<artifactId>client</artifactId>
- 
<version>0.0.1-SNAPSHOT</version>
- 
<packaging>jar</packaging>
- 
- 
<name>client</name>
- 
<description>Demo project for Spring Boot</description>
- 
- 
<parent>
- 
<groupId>org.springframework.boot</groupId>
- 
<artifactId>spring-boot-starter-parent</artifactId>
- 
<version>2.0.4.RELEASE</version>
- 
<relativePath/> <!-- lookup parent from repository -->
- 
</parent>
- 
- 
<properties>
- 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- 
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- 
<java.version>1.8</java.version>
- 
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
- 
</properties>
- 
- 
<dependencies>
- 
<dependency>
- 
<groupId>org.springframework.cloud</groupId>
- 
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- 
</dependency>
- 
- 
<dependency>
- 
<groupId>org.springframework.boot</groupId>
- 
<artifactId>spring-boot-starter-test</artifactId>
- 
<scope>test</scope>
- 
</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>
- 
- 
<build>
- 
<plugins>
- 
<plugin>
- 
<groupId>org.springframework.boot</groupId>
- 
<artifactId>spring-boot-maven-plugin</artifactId>
- 
</plugin>
- 
</plugins>
- 
</build>
- 
</project>
项目的依赖都加载完成后,在启动类中加上@EnableDiscoveryClient,声明这是一个eureka client,否则不会进行服务注册:
- 
package org.zero.eureka.client;
- 
- 
import org.springframework.boot.SpringApplication;
- 
import org.springframework.boot.autoconfigure.SpringBootApplication;
- 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- 
- 
- 
- 
public class ClientApplication {
- 
- 
public static void main(String[] args) {
- 
SpringApplication.run(ClientApplication.class, args);
- 
}
- 
}
接着就是在application.yml配置文件中,配置注册中心即eureka server的地址,以及项目的名称和启动端口号。如下:
- 
eureka:
- 
client:
- 
service-url:
- 
defaultZone: http://localhost:8761/eureka/
- 
spring:
- 
application:
- 
name: eureka-client
- 
server:
- 
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依赖即可:
- 
<dependency>
- 
<groupId>org.springframework.boot</groupId>
- 
<artifactId>spring-boot-starter-web</artifactId>
- 
</dependency>
项目启动成功后,可以在eureka server的信息面板中查看到已注册的实例信息,如下:
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号