SpringCloud笔记八:分布式配置中心Config

SpringCloud笔记八:分布式配置中心Config

文章目录

      • SpringCloud笔记八:分布式配置中心Config
        • 什么是配置中心
        • SpringCloud的配置中心组件config-server
        • 使用git服务器结合config搭建分布式配置中心
        • 分布式配置中心客户端使用

什么是配置中心

  1. 统一管理配置,快速切换各个环境的配置

  2. 相关产品

1、百度的disconf

百度disconf

2、阿里的diamand

阿里diamand

3、springCloud的configs-server

springCloud的config-server

  1. 推荐文章

配置中心推荐文章

  1. 配置中心的架构

SpringCloud笔记八:分布式配置中心Config_第1张图片

SpringCloud的配置中心组件config-server

  1. 新建项目config-server
  2. 启动类增加注解

@EnableConfigServer

使用git服务器结合config搭建分布式配置中心

  1. 默认使用git存储配置中心

使用git服务器可以搭建gitlab服务器或者使用github,开源中国gti,阿里云git

  1. 配置文件添加配置
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/destroy_god_zc/config_cloud
          username: xxxxx.com
          password: xxxxx
          timeout: 5
          default-label: master
server:
  port: 9100

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  1. 访问方式(一定要注意语法,如果有问题会报错)

多种访问路径,可以通过启动日志去查看

例子:http://localhost:9100/product-service.yml

/{name(服务名称)}-{profiles}.properties

/{name}-{profiles}.yml

/{name}-{profile}.json

/{label}/{name}-{profiles}.yml

name:服务器名称

profile:环境名称,开发,测试,生产

lable:仓库分支,默认master分支

即使你上传的是yml格式,当你访问properties,会自动帮我们转换成properties格式,json同理。

分布式配置中心客户端使用

  1. 官方文档

官方文档

  1. 服务中添加依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-config-clientartifactId>
dependency>
  1. 修改对应服务的配置文件
spring:
  cloud:
    config:
      discovery:
      	#配置中心服务名称,这个和Eureka中的一致
        service-id: CONFIG-SERVER
        enabled: true
       #profile是选择分支里面的那个
      profile: test
      #label是分支,默认是master
      label: test
spring:
  application:
    name: product-service
  output:
    ansi:
      enabled: ALWAYS
  zipkin:
    base-url: http://localhost:9411/
  sleuth:
    sampler:
      probability: 1
  cloud:
    config:
      discovery:
        service-id: CONFIG-SERVER
        enabled: true
      profile: test
      label: test

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  1. 将application.yml修改为bootstrap.yml
  2. 默认读取的文件名是服务名称
  3. 项目中的yml配置的都是一般不会修改的yml
  4. 没有实现动态刷新yml。

你可能感兴趣的:(springCloud笔记)