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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27828675/article/details/83504743

 项目源码及相关说明请查看此文:Spring Cloud入门教程-简介

      接上一篇文章,这里记录一下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 远程获取配置文件成功!

猜你喜欢

转载自blog.csdn.net/qq_27828675/article/details/83504743