配置中心SpringCloudConfig

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
    Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
 
将配置文件提交到git上  
  文件命名规则:
{application}-{profile}.yml或{application}-{profile}.properties
  application为应用名称 profile指的开发环境(用于区分开发环境,测试环境、生产环境等)
 
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐config‐server</artifactId>
</dependency>
</dependencies>

启动类  

@EnableConfigServer //开启配置服务
 
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

 

客户端配置 

依赖

<dependency> 
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring‐cloud‐starter‐config</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 可省略
 

 

posted @ 2020-03-26 11:34  MartinEDM  阅读(147)  评论(0编辑  收藏  举报