• 为Spring Cloud Config Server配置远程git仓库


    简介

    虽然在开发过程,在本地创建git仓库操作起来非常方便,但是在实际项目应用中,多个项目组需要通过一个中心服务器来共享配置,所以Spring Cloud配置中心支持远程git仓库,以使分散的项目组更方便的进行协作。

    基础环境

    • JDK 1.8
    • Maven 3.3.9
    • IntelliJ 2018.1
    • Git

    项目源码

    Gitee码云

    配置远程git仓库

    首先我在gitee上创建了一个远程仓库https://gitee.com/zxuqian/spring-cloud-config-remote专门用来存放配置文件,然后我们会通过配置文件来访问此仓库。然后我们把以前本地的配置文件迁移到此库中。为了测试效果,我们把web-client.ymlmessage的值修改为:此条消息来自于远程配置仓库

    配置configserver

    现在在我们之前的configserver中作一些配置上的改动。首先为了保留之前的本地仓库的配置,我们把application.yml重命名为application-local.yml

    这个-local是一个profile,它的值是-后面的,即local,我们可以在bootstrap.yml中指定使用哪个profile。比如实际项目中开发阶段和生产阶段的配置有所不同,所以会有application-development.ymlapplication-production.yml等两种或以上的配置。

    然后新建一个application-remote.yml文件,添加如下配置内容:

    server:
      port: 8888
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/zxuqian/spring-cloud-config-remote
              username: 您的gitee用户名
              password: 您的gitee密码
    

    因为是自用账号的仓库,所以就不提供账号密码了,改成自己对应的。这里uri配置了远程git仓库的地址。

    最后在bootstrap.yml中启用我们的remote profile:

    spring:
      application:
        name: config-server
      profiles:
        active: remote
    

    spring.profiles.active即指定了我们的remote Profile,使用application-remote.yml配置文件。

    测试

    使用spring-boot:run启动我们的config server,然后访问http://localhost:8888/web-client/default,看到如下结果:

    {"name":"web-client","profiles":["default"],"label":null,"version":"cbef7d379ef01d68810c3fdc2105b2226ea6c611","state":null,"propertySources":[{"name":"https://gitee.com/zxuqian/spring-cloud-config-remote/web-client.yml","source":{"message":"此条消息来自于远程配置仓库","management.endpoints.web.exposure.include":"*"}}]}
    

    message的值取自于远程仓库。这里的web-client/default配置文件名/profile,因为我们的web客户端项目没有其他Profile,则默认值为default,只有这样写全,才可以访问到web-client.yml的配置。

  • 相关阅读:
    正则表达式
    javascript if(条件)------------条件中可以使用的值
    maven配置
    获取中文日期
    Oracle无安装客户端安装方法
    Eclipse org.eclipse.compare plug-in
    Differences or similarities between Java and C++
    Java Programming Guidelines
    Native Method
    Object in Java same as pointer
  • 原文地址:https://www.cnblogs.com/zxuqian/p/9013307.html
Copyright © 2020-2023  润新知