springcloud config踩坑
学习springcloud config,创建springboot 项目config-server。
config-server:pom.xml文件
注意,这里cofig依赖包用得是spring-cloud-config-server
配置文件
此时你需要在github上创建repository并添加配置文件。如下是为了测试创建得,较简单。
此时启动项目。用http://localhost:8001/config-server.yml可以访问
Config支持使用的请求的参数规则为:
- / { 应用名 } / { 环境名 } [ / { 分支名 } ]
- / { 应用名 } - { 环境名 }.yml
- / { 应用名 } - { 环境名 }.properties
- / { 分支名 } / { 应用名 } - { 环境名 }.yml
- / { 分支名 } / { 应用名 } - { 环境名 }.properties
正常出现github上配置文件得内容则表示正常。
接下来创建config-client项目,pom.xml文件依赖包基本和config-server一致,除了config包
这里用得是spring-cloud-starter-config。坑就在这里,导致我client一直启动不成功.
配置文件也比较简单
client文件需要配置为bootstrap.yml文件,在别人得博客中找到详解。
https://www.cnblogs.com/BlogNetSpace/p/8469033.html
接下来做个简单测试,将启动类加入@RestController注解,其中@Value("${abc}"),abc是我在client.preperties中设置得值
项目正常启动。http://localhost:8002/str访问就会出现success,至此结束。
其中容易踩得坑:
1.client 项目中的配置文件要改为bootstarp.yml/preperties;
2.server项目的依赖包为spring-cloud-config-server,而client依赖包用的是spring-cloud-starter-config;
3.github路径。
给自己留个记录,避免再次犯错。也给跟我一样踩坑的朋友提示,网上有很多更详细的配置,共同学习。