SpringCloud的配置中心

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务中心采用Git的方式存储配置文件,所以咱们很容易部署修改,有助于对环境配置进行版本管理。git

1 、配置中心服务端启动

Spring Cloud Config支持在Git, SVN和本地存放配置文件,使用Git或者SVN存储库能够很好地支持版本管理,Spring默认配置是使用Git存储库。在本案例中将使用OSChina提供的Git服务存储配置文件。为了能让Config客户端准确的找到配置文件,在管理配置文件项目my-sample-config中放置配置文件时,须要了解application, profile和label的概念:spring

  • {application}映射到Config客户端的spring.application.name属性
  • {profile}映射到Config客户端的spring.profiles.active属性,能够用来区分环境
  • {label}映射到Git服务器的commit id, 分支名称或者tag,默认值为master

例如在本案例中,计划将客户端my-config-client的spring.application.name设置为my-client,则上传一个文件名为my-client.yml配置文件;另外计划在uat环境上作不一样的配置,则再上传一个文件名为my-client-uat.yml配置文件tomcat

info:
  app:
    name: 配置中心服务器
  version: 1.0.0-SNAPSHOT

server:
  port: 3333
  tomcat:
    max-threads: 1000
    uri-encoding: UTF-8

spring:
  profiles:
    active: dev
  application:
    name: config-service
  http:
    encoding:
      charset: UTF-8
      force: true
  zipkin:
    base-url: "http://192.168.3.3:2233/"
  rabbitmq:
    host: 192.168.0.1
    port: 1111
    username: 1
    password: 2
  cloud:
    config:
      server:
       git:
          searchPaths: dev
          #设置服务器端口号和配置文件Git仓库的连接
          uri: "http://192.168.3.3:3000/root/config.git"
          username: root
          password: 111111

management:
  security:
    enabled: false
 
security:
  basic:
    enabled: false

eureka:
  client:
    serviceUrl:
      defaultZone: "http://192.168.3.1:1111/eureka/,http://192.168.3.2:1111/eureka/,http://192.168.3.3:1111/eureka/"
  instance:
    # 续约更新时间间隔(默认30秒)
    lease-renewal-interval-in-seconds: 20
    # 续约到期时间(默认90秒)
    lease-expiration-duration-in-seconds: 30

若是配置文件放置在Git存储库的根目录下,则无需使用searchPaths参数,本例中的配置文件在my-sample-config目录中,所以使用searchPaths参数提示Config服务器搜索my-sample-config子目录。服务器

二、客户端链接

spring:
  profiles:
    active: dev
  application:
    name: DDDD-xs
  http:
    encoding:
      charset: UTF-8
      force: true
  cloud:
    bus:
      trace:
        enabled: true
    config:
      name: spes
      profile: serv
      label: master
      discovery:
        enabled: true
        service-id: AAAAAA
      fail-fast: true

三、动态刷新配置

无需从新启动客户端,便可更新Spring Cloud Config管理的配置 
1)在Git中更新 my-client-uat.yml 文件中的配置:app

my-config:
  appName: my-app-uat-new
  • 1
  • 2

2) 访问http://localhost:8888/my-client/uat/master,可见属性my-config.appName已经更新。但此时访问http://localhost:8080/app-name,客户端读到属性值还没有更新。 
3) 对Conf客户端发一个POST请求http://localhost:8080/refresh,返回200 OK。再次访问http://localhost:8080/app-name,可见在并未重启客户端服务的状况下,读到的属性值已经动态更新分布式

相关文章
相关标签/搜索