Spring Boot 不兼容 Eureka 服务器

Posted

技术标签:

【中文标题】Spring Boot 不兼容 Eureka 服务器【英文标题】:Spring boot does not compatible with Eureka server 【发布时间】:2018-06-06 03:39:59 【问题描述】:

我已经构建了一个应用程序,它是一个 Eureka 服务器,服务注册表。 pom.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>Microservice</groupId>
    <artifactId>Micro1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <!-- This is a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Tomcat embedded container-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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


</project>

运行应用程序的主类如下:

package org.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication 

    public static void main(String[] args) 
        //ApplicationContext ctx = new SpringApplicationBuilder().bannerMode(Banner.Mode.CONSOLE).run(args);
        SpringApplication.run(EurekaServiceApplication.class, args);
    

而 application.yml 就是这样的:

server:
  port: 8761

当通过clean spring-boot:run命令运行时,会引发如下异常:

引起:java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.showBanner(Z)Lorg/springframework/boot/builder/SpringApplicationBuilder; 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:110) 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:75) 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:1) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) 在 org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) 在 org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) 在 org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:296) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) 在 org.eureka.server.EurekaServiceApplication.main(EurekaServiceApplication.java:16)

... 6 更多 [INFO]

当我删除 pom.xml 中的 eureka 依赖时,应用程序运行成功。 哪里错了?

【问题讨论】:

可以省略eureka版本,使用依赖管理添加spring cloud吗? 请问我该怎么做? 一定要加Spring cloud,去掉eureka的版本吗? 这就是它通常的使用方式,请看这里:projects.spring.io/spring-cloud,例如 dalston 版本。 【参考方案1】:

你会像这样改变你的 pom

<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.roshan</groupId>
    <artifactId>registry</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>registry</name>
    <description></description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>registry</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

以上是关于Spring Boot 不兼容 Eureka 服务器的主要内容,如果未能解决你的问题,请参考以下文章

使用 Eureka 服务集成测试 Spring Boot 服务

.net core + eureka + spring boot 服务注册与调用

使用 Spring boot、Eureka、Zuul、Spring Oauth 创建 OAuth 安全微服务的问题

如何在没有spring-boot的情况下使用eureka+feign?

spring cloud 与spring boot的版本对应总结

spring cloud 与spring boot的版本对应总结