我们一般把配置文件写在项目中直接获取相关参数
Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。
1.配置仓库
使用的是网上找的一个:https://github.com/yjmyzz/spring-cloud-config-repository
2.配置服务端
pom.xml中加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
在应用程序主类中加入:@EnableConfigServer
applicaiton.yml中配置git的地址
spring:
cloud:
client:
ipAddress: 127.0.0.1
config:
server:
git:
uri: https://github.com/yjmyzz/spring-cloud-config-repository
访问url
3.配置客户端
客户端配置文件中配置spring.cloud.config调用服务端文件信息
Spring:
cloud:
config:
uri: http://localhost:1205/
profile: prod
label: master
注:分布式配置中配置文件的概念
@RestController
@RefreshScope //配置文件更新后自动刷新
public class RestfullController {
@Value("${demo.title}")
String title;
@RequestMapping("api")
public String getInfo(){
return "主题是:"+title;
}
}
访问url
这样子我们的配置文件就放在了git上,只需要在git上修改配置文件,程序自动识别
遗留问题:访问有用户名和密码的公司gitlab一直无法实现,网上也没有找到答案,知道的话麻烦告诉下