最近在学习spring boot和spring cloud,而在编写注册服务组件Eureka server入门示例时,遇到了一些问题,在此总结一下,以便日后查询,也希望可以帮到遇到和我一样问题的人。
Talk is cheap. Show me the code!
1、pom.xml中spring-cloud-starter-eureka-server无法引入或启动类注释@EnableEurekaServer不生效
这个问题是在pom.xml中配置spring-cloud-starter-eureka-server时,需要添加版本号,我在引入时没有加上version,导致默认使用了父类spring-boot-starter-parent的版本号,后来查阅jar发现,spring-cloud-starter-eureka-server目前最高版本为1.4.6.RELEASE,而我的父类使用了1.5.9.RELEASE,导致获取不到包,正确配置如下:
<dependency>
<!-- Eureka server 包引入 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2、启动报错:java.lang.NoSuchMethodError:org.springframework.boot.builder.SpringApplicationBuilder.
此问题是pom.xml中没有引入spring-cloud-starter-config配置导致,使用eureka启动需要这个包,否则在项目启动时就报上边这个错误,引入正确配置如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
3、启动报错:Reason: Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Map’ for property ‘serviceUrl’;
此问题是因为,我在使用yml配置文件,配置service-url的value值时,设置了一个地址字符串,
然而源码中的参数却是一个map类型,如图所示:
所以在启动时,出现不能转换为Map类型的异常。这个问题需要在配置service-url时,增加一个子节点defaultZone,正确配置如下:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
4、启动报错:Exception in thread “main” java.lang.NoClassDefFoundError:org/springframework/cloud/context/named/NamedContextFactory
此错误是因为我手动配置的pom.xml,其中缺失spring-cloud-starter引入导致,引入包依赖如下:
<dependency>
<!-- Spring Cloud starter -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
以下是整个pom.xml和application.yml配置:
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jia</groupId>
<artifactId>microservice-consumer-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>movie</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<!-- Setup Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- Spring Cloud starter -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<!-- Eureka for service registration -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<!-- Eureka for service registration -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/