前言
配置中心存放文件在github是读取过程,可能你会出现读取不到配置信息。本次笔者将这一过程进行详细介绍。
准备
父工程
由于笔者是使用聚合工程,所以这次也是把相关的工程创建说明写上。当然你也可以完全创建两个独立的工程来引用。
创建父工程时直接只有一个pom文件,以下是这个文件的依赖信息
<? xml version = "1.0" encoding = "UTF-8" ?> <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://HdhCmsTestw3.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> <packaging> pom </packaging> <modules> <module> ch2-2-eureka-client </module> <module> ch2-3-config-server </module> <module> ch4-1-feign </module> <module> ch5-1-zuul </module> <module> config-client </module> </modules> <parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 2.0.2.RELEASE </version> </parent> <groupId> com.example </groupId> <artifactId> ch2-1 </artifactId> <version> 0.0.1-SNAPSHOT </version> <name> ch2-1 </name> <description> eureka </description> <properties> <java.version> 1.8 </java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId> org.springframework.cloud </groupId> <artifactId> spring-cloud-dependencies </artifactId> <version> Finchley.RELEASE </version> <type> pom </type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <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> </dependencies> </project>子工程-config-server
这个工程内容目录如下图
依赖
由于在父工程已经引入了WEB,所以这里只引入spring-cloud-config-server,另外一个spring-boot-starter-actuator主要是用来查看端点信息的,可以不引用,后续手机刷新时需要这个开启相关的访问端点(好像是,具体后续可能有相关文章再细说)
<? xml version = "1.0" encoding = "UTF-8" ?> <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId> ch2-1 </artifactId> <groupId> com.example </groupId> <version> 0.0.1-SNAPSHOT </version> </parent> <modelVersion> 4.0.0 </modelVersion> <artifactId> ch2-3-config-server </artifactId> <dependencies> <dependency> <groupId> org.springframework.cloud </groupId> <artifactId> spring-cloud-config-server </artifactId> </dependency> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-actuator </artifactId> </dependency> </dependencies> </project>启动主类
package springcloud ; import org . springframework . boot . SpringApplication ; import org . springframework . boot . autoconfigure . SpringBootApplication ; import org . springframework . cloud . config . server . EnableConfigServer ; @SpringBootApplication //只加这个即可,表示这个启动的是config的服务中心 @EnableConfigServer public class Ch21ConfigServerApplication { public static void main ( String [] args ) { SpringApplication . run ( Ch21ConfigServerApplication . class , args ); } }配置文件
服务端的配置不需要注意什么的,按下面的配置即可,因为默认是git的,所以不需要写profiles 指向git了
server : port : 9090 spring : application : name : config - server cloud : config : server : git : uri : https : //github测试数据/xxx1/xxx2.git #xxx2 是指存放当前配置文件的仓库名 username : 写自己的 github 账号 password : 写自己的 github 密码启动项目后,访问http://localhost:9090/config/dev/main 即可以看到配置信息 这个地址要注意的是 http://localhost:9090/{配置文件名前缀}/{环境类型}/{仓库分支标签}
如在仓库创建的文件名为config-dev.yml,那么配置文件名前缀就是config,环境就是指文件名后缀 dev,仓库标签就是当前存放文件的仓库分支名
看到以下信息说明启动项目且配置获取成功
子工程-config-client
子工程就是个问题了,当时创建时一直无法读取配置信息就是子工程这里出现的问题,有两个问题觉得要说明的,先看下子工程创建过程。
pom文件
```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://HdhCmsTestw3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ch2-1</artifactId> <groupId>com.example</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>ch2.springcloud</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies> </project>启动类
package cn . springcloud ; import org . springframework . boot . SpringApplication ; import org . springframework . boot . autoconfigure . SpringBootApplication ; @SpringBootApplication public class ClientApplication { public static void main ( String [] args ) { SpringApplication . run ( ClientApplication . class , args ); } }配置类或者通过@Value获取配置信息说明
package cn . springcloud . config ; import org . springframework . boot . context . properties . ConfigurationProperties ; import org . springframework . stereotype . Component ; @Component @ConfigurationProperties ( prefix = "cn.springcloud.book" ) public class ConfigInfoProperties { private String config ; public String getConfig () { return config ; } public void setConfig ( String config ) { this . config = config ; } }测试类Controller
这个类写了两个获取配置信息的方式,一个是通过@Autowired注入配置类,一个是通过@Value来获取
package cn . springcloud . controller ; import cn . springcloud . config . ConfigInfoProperties ; import org . springframework . beans . factory . annotation . Autowired ; import org . springframework . beans . factory . annotation . Value ; import org . springframework . web . bind . annotation . GetMapping ; import org . springframework . web . bind . annotation . RestController ; @RestController public class ConfigController { @Autowired private ConfigInfoProperties configInfoProperties ; @Value ( "${demo.value}" ) private String value ; @GetMapping ( "/getmsg" ) public String getMsg (){ System . out . println ( "value: " + value ); System . out . println ( "this config is: " + configInfoProperties . getConfig ()); return configInfoProperties . getConfig (); } }配置文件
这里的配置文件就是一个问题点了,配置如下启动时会先摘取配置信息再启动,所以把配置中心的配置放到bootstrap.yml中
问题:label: main 这个是指配置指向当前创建的分支,如果没有则默认是master,网上就是这样说的,后来发现,现在的直接在github创建仓库后,显示的是main,所以当时我没有配置或者配置成master时一直获取不了配置信息,所以重新查看了仓库信息,如下图:
bootstrap.yml说明:
spring : cloud : config : label : main uri : http : //localhost:9090 name : config profile : devapplication.yml说明
server : port : 9000 spring : application : name : config - client另一个问题
配置了配置中心的URL,即上面bootstrap.yml中的 uri: http://localhost:9090,但是项目一直启动的是访问 uri: http://localhost:8888,当时就纳闷,找了很久都没有找到在哪里配置了8888,后来又是清缓存,还是不行,最后在client添加server依赖包,再重启,结果发现正常了,正常后又把它删除,也正常了。
org . springframework . cloud spring - cloud - config - server从github拉取的文件在本地存放的目录
如果你不知道文件在哪里,在启动server时又显示了以下信息,则说明文件是拉取到本地了:
当前你也可以直接在电脑上查找文件名,看到以下类似目录即可以找到
不知道能不能指定目录的,这个读者可以试下
测试结果
启动server client,访问client提供的接口localhost:9000/getmsg
结果如图,说明正常获取信息了
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/esunlang/article/details/113791467
查看更多关于解决SpringCloud Config结合github无法读取配置的问题的详细内容...