配置中心 Spring Cloud config

配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。

1.服务端  

   创建spring boot 项目 

   主要依赖 

   <dependency>
        <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

  新版本好像不包括 web 需加入web依赖 

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在启动类上声明 

@EnableConfigServer

配置文件 application.yml
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: #git 地址
          search-paths: #git的路径
          username: #账号
          password: #密码
          basedir: #本地存放路径
      label: master #分支

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8888
2.客户端
引入依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

配置文件 要用bootstrap.yml  (用来程序引导时执行,应用于更加早期配置信息读取)

用于有eureka的配置 通过id寻找配置中心
#spring:
# cloud:
# config:
# discovery:
# service-id: config
# enabled: true
# profile: dev
# name: product
# label: master

通过url 寻找配置中心
spring:
cloud:
config:
label: master #分支
profile: dev #配置描述
uri: http://localhost:8888
name: product #名称 设置了application.name 可省略
 
配置访问规则 
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application}应用名称

{profile} 配置文件的版本 如application-dev.yml、application-test.yml、application-prod.yml。

{label} 表示 git 分支,默认是 master 分支

 applicattion-dev.yml  applicattion-prod.yml  有共同配置  则可放在 applicattion.yml 中  ,访问时得到的文件 合并了application.yml中的内容

 



原文地址:https://www.cnblogs.com/qin1993/p/11549260.html