- SpringCloud系列之Config2-创建配置中心服务
- SpringCloud技术指南系列(十)配置管理之自建配置中心(
- 「 从0到1学习微服务SpringCloud 」10 服务网关Z
- 「 从0到1学习微服务SpringCloud 」09 补充篇-m
- SpringCloud技术指南系列(三)服务注册发现之Eurek
- SpringCloud技术指南系列(四)服务注册发现之Consu
- SpringCloud Alibaba - Nacos 作为配置
- SpringBoot -- 配置中心服务/webhook
- SpringCloud组件:Eureka高可用集群部署
- Springcloud使用Bus配合Config实现配置更新
config配置中心跟Eureka比较类似,服务中心搭建比较简单,如果是使用IDEA创建的方式可以提前勾选选择好config-server的服务即可
首先如果没有使用IDEA自动创建组件的方式,可以手动的添加config的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
然后需要在主程序类上添加一个开启配置中心的注解:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
最后就需要在配置中心配置一些信息了,主要就是服务名就不用说了,还有拉取配置的uri,账号密码,如果是自己玩的话就配置成防配置的github的公开仓库地址即可,如果是公司的那就得配置账号密码了。
注意:如果github访问很慢就换成gitee也行的
我们可以新建一个仓库,然后地址就复制出来放在uri那里,然后在仓库那里新建两个文件:
config-consumer-dev.yml
info:
profile: dev
name: Saul
words: 'God bless me
config-consumer-prod.yml
info:
profile: prod
name: Paul
words: 'God bless you '

search-paths:abc,def*
这个配置的是文件目录,可以配置多个,以逗号隔开,当然也可以配置通配符,比如def*就是前缀是def的所有的目录。

拉取资源文件我们设置为true
下面就是配置中心服务启动之后,我们可以通过接口调用的方式获取各个配置文件的内容,其中有两种接口方式:

上图中第一种接口方式的最后一个后缀,比如.json并不是指哪个确定的文件后缀,而是后取到的配置文件信息以什么样的文件类型来展示。
其中label是仓库的分支名称,下面是测试接口截图:




网友评论