config client没有进行fetch操作bug
***@TOC
你好!在学习和使用ConfigServer进行配置管理时,在ConfigServer一切正常的情况下,发现ConfigClient没有进行fetch操作,端口号依旧是默认的8080,于是查阅了一些资料,解决方法如下。
##关于bootstrap.yml和application.yml
在网上查阅资料,大部分会说优先加载bootstarp.yml,其实不然,springboot项目根本不会识别bootstarp.yml,还有网上说加入cloud-context依赖就可以识别,尝试无果。
其实,springCloud项目才会优先加载bootstrap.yml。所以,当你的项目是springboot项目,然后将application修改为bootstrap,会以默认端口进行启动。如下所示:
图中显示没有找到配置文件,使用默认配置。
加上cloud-context依赖之后,依然如此,无解。
其实,springCloud项目优先加载bootstrap.yml是毋庸置疑的,而关键点是,你的项目是否编译为springCloud。如果你的项目是通过idea的搭建的,就要看看你的pom文件的parent,是springBoot项目,还是springCloud项目。pom如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--这里标识着你的项目,是springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.css</groupId>
<artifactId>productor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>productor</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
于是,原因找到了,于是将其修改springCloud项目,如下:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR5</version>
<relativePath/>
</parent>
于是,正常启动项目即可。
##如果此时依然无法正常启动
那就要注意看你的Cloud版本和boot版本,是否兼容。例如,上述Dalston,只兼容boot版本1.5.X,而现在的springboot的版本已经上升至2.6.x,所以注意看兼容性。兼容性对比如下:
Release Train | Boot Version |
---|---|
Hoxton | 2.2.x |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
Camden | 1.4.x |
Bixton | 1.3.x |
Angle | 1.2.x |
具体情况还是以官网为准:
https://spring.io/projects/spring-cloud#overview.
##如果此时依然没有走到你的配置文件
那至少,你会看到
图中显示,你的项目已经从ConfigServer进行了拉取配置文件,如果依然没有用你的配置文件,那就说明,你的配置是有问题的。
具体的配置如下(记得用bootstrap.yml)
#注册中心
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8760/eureka
spring:
application:
name: productor-service
#配置中心
cloud:
config:
#后缀
profile: dev
label: master
#通过ip直接访问配置中心
# uri: http://localhost:9001/
#通过eurka访问配置中心
discovery:
#配置中心服务
service-id: config-server
enabled: true
至此,便解决了问题,项目会加载Server上的对应配置文件,端口成为远程配置文件的端口。
如果本次文章对您有帮助,希望您点个赞!