1、项目环境
项目中使用的是SpringBoot 2.1.7.RELEASE、SpringCloud Greenwich.SR2版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.Config-Server 首先确保正常启动了,并检查github上的配置文件是否正确;
如:如果配置内容格式不对,此处访问会报错;
3.问题:
config-client一直无法获取config-server在github上的配置文件中的属性值;
(1).bootstrap配置读取优先于application
将application.yml /application.properties 改成 bootstrap.yml /bootstrap.properties
(2)github上配置文件格式:(注意:很容易遇到坑的)
/{application}/{profile}[/{label}]:
1: /{application}-{profile}.yml
2: /{label}/{application}-{profile}.yml
3: /{application}-{profile}.properties
4: /{label}/{application}-{profile}.properties
(3)我刚开始的错误配置文件:
错误一:
1:config-client中的bootstrap.yml:
spring:
cloud:
config:
uri: http://localhost:8765
profile: dev
2:github:
ps:刚开始以为只要 -后面的dev与spring.cloud.config.profile=dev一致就行,结果发现不可以;
错误二:
1:config-client中的bootstrap.yml:
spring:
cloud:
config:
uri: http://localhost:8765
profile: dev
刚开始以为下面这段服务名配置放在github上,然后用config-client读取下来就行,所以只是将github上的配置文件名格式改成如下,结果发现还是不行;
spring:
application:
name: morning-service-order
github: 配置文件修改如下:(附:yml在线转properties )
正确:
我的做法是在bootstrap.yml中配置服务名,并在github中将配置文件命名为:
或者yml格式: