Spring Cloud入门教程-Config Server从github 远程读取配置文件

接上一篇文章,这里记录一下Config Server从github 远程读取配置文件。

Spring cloud Config支持从远程Git仓库读取配置文件,即 Config Server可以不从本地的仓库读取,而是从远程Git仓库读取。这样做的好处就是将配置统一管理,并且可以通过 Spring Cloud Bus在不人工启动程序的情况下对 Config Client的配置进行刷新。这里采用GitHub作为远程Git仓库.

Config Server从github 远程读取配置文件

修改Config Server 的配置文件application.properties  添加git 相关配置。
server.port=8797
spring.application.name=config-server
 
#从本地读取配置文件####################################################
#spring.profiles.active=native
#spring.cloud.config.server.native.search-locations=classpath:/shared
#####################################################################
 
 
 
#从读取远程github配置文件####################################################
spring.cloud.config.server.git.uri=https://github.com/xuweichao/SpringCloud-Demo
spring.cloud.config.server.git.search-paths=SpringcloudConfig
spring.cloud.config.server.git.username=******
spring.cloud.config.server.git.password=******
spring.cloud.config.label=master

其中 spring.cloud.config.server.git.uri 为GitHub 项目名

spring.cloud.config.server.git.search-paths 为存放配置文件的目录

spring.cloud.config.server.git.username 为github 账号
spring.cloud.config.server.git.password 密码
spring.cloud.config.label=master 分支名

需要先将 config-client-dev.properties 上传至github .

重启config-server 和config-client ,请求http://localhost:8798/main 返回 “config-client-v1”.说明从github 远程获取配置文件成功!

构建高可用的Config Server

当服务实例很多时,可以考虑将Config Server 做成一个微服务。并将其集群化,达到高可用。所以需要将config-server 和config-client 向eureka-server 注册。

接下来对config-server 和config-client 进行改造。

两个module 都加入eureka依赖

 <!--构建高可用的Config Server 将config server 作为eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

并在启动类添加注解@EnableEurekaClient 开启 eureka服务。

添加eureka-server 配置

eureka.client.service-url.defaultZone=http://localhost:8791/eureka/

config-client 在增加一下配置:

spring.cloud.config.label=master
spring.cloud.config.profile=dev
 
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.discovery.enabled=true
spring.cloud.config.fail-fast=true

依次启动eureka-server,config-server,config-client .

浏览器访问eureka-server http://localhost:8791/
在这里插入图片描述
config-server,config-client 注册成功。

请求http://localhost:8798/main 返回 “config-client-v1”.说明从github 远程获取配置文件成功!


版权声明:本文为CSDN博主「天青色等烟雨11」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27828675/article/details/83504743

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值