最近搭建SpringCloud架构,踩了不少坑,主要问题有以下这些:
一、SpringBoot无法启动,找不属性文件
DiscoveryClient_UNKNOWN/windows10.microdone.cn - was unable to refresh its cache! status = Cannot execute request on any known server
错误原因:
application.yml 文件找不到,见application.properties文件改成application.yml 即可
关于application.yml 与 application.properties区别,可以看我的另外一篇文章
[https://blog.csdn.net/weixin_41003771/article/details/103086603](https://blog.csdn.net/weixin_41003771/article/details/103086603)
二、Hoxton 、Greenwich等版本
新建一个eureka模块后,发现怎么都启动不起来,springboot一直报错,经过多次查找,发现是SpringCloud和SpringBoot不匹配问题
SpringCloud有很多版本,不同版本,需要的SpringBoot版本也不一样,具体可参照以下:
spring-cloud:
"Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
"Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
"Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
"Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
"Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
"Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
"Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
"Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
"Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
"Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
"Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
"Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
"Greenwich.SR3": "Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT",
"Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4",
"Hoxton.M2": "Spring Boot >=2.2.0.M4 and <2.2.0.BUILD-SNAPSHOT",
"Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.2.0.BUILD-SNAPSHOT"
三、找不到端口
启动时,提示Request execution error. endpoint=DefaultEndpoint,找不到启动端口,仔细一查,发现是application.yml 中信息没配置对
错误:
server:
port: 8761
正确:
server:
port: 8761
application.yml 必须是树形结构,而且 : 号后面必须有个空格
四、找不到Hoxton.RC2
错误:Failure to find org.springframework.cloud:spring-cloud-dependencies:pom:Hoxton.RC2
解决方法:
在父pom.xml中指定仓库(project 节点内)
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
五、引入其他模块报错
在新建生产服务时,引入common通用模块,但pom一直报红,找了很久,才找到原因:repositories 引入重复了
repositories 只需要在父工程pom中引入就好,其他模块全部删掉
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
六、编译失败
打包编译时,出现错误: ‘packaging’ with value ‘jar’
解决方法,修改父工程pom.xml,增加打包方式为pom
<packaging>pom</packaging>
七、打包失败
打包时,子模块common一直打包失败,经过排查,发现子模块pom中引入了pring-boot-maven-plugin,这个只需要配置到打包的模块即可
<build>
<plugins>
<!--不需要配置 只需配置到打包的模块-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
</plugins>
</build>