SpringCloud之分布式配置中心Config

分布式配置中心Config

配置中心的作用和好处

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

 在微服务体系中,服务的数量以及配置信息的日益增多,比如各种服务器参数配置、各种数据库访问参数配置、各种环境下配置信息的不同、配置信息修改之后实时生效等等,传统的配置文件方式或者将配置信息存放于数据库中的方式已无法满足开发人员对配置管理的要求,如:

安全性:配置跟随源代码保存在代码库中,容易造成配置泄漏
时效性:修改配置,需要重启服务才能生效
局限性:无法支持动态调整:例如日志开关、功能开关

所以,一套集中式的,动态的配置管理设施是必不可少的

Springcloud提供了config组件来解决这种问题

相关产品

百度的disconf     https://github.com/knightliao/disconf
阿里的diamand     https://github.com/takeseem/diamond
携程Apollo       https://github.com/ctripcorp/apollo

配置中心工作流程

是什么
SpringCloud config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

怎么玩
SpringCloud config 分服务端和客户端两部分
服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供配置信息, 加密/解密信息等访问接口

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容, 并在启动时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具类方便管理和访问配置内容。

搭建config-server项目

01.搭建git+config的分布式配置中心
02.新建子项目-cloud-config-center7000
03.配置依赖
 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

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

    </dependencies>
-----------------------------------------------------------
04.application.yml
server:
  port: 7000

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #在gitee中新建的仓库路径,注意是地址栏上的
          uri: https://gitee.com/happy_sun/springcloud-46
          #码云账号
          username: hpsyj88@163.com
          #码云账号秘密
          password: xxxxxxxxx
          label: master

eureka:
  client:
    #是否将自己注册进去eureka,false为不注册,true注册
    registerWithEureka: true
    #是否从eureka抓取注册信息,单点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    serviceUrl:
      defaultZone:  http://localhost:8761/eureka/
-------------------------------------------------------------
05.启动类-ConfigMain7000
@SpringBootApplication
@EnableConfigServer
public class ConfigMain7000 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigMain7000.class, args);
    }
}
-------------------------------------------------------------
01.以cloud-consumer-order8090项目为例子
02.修改pom.xml 添加依赖
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-client</artifactId>
 </dependency>
--------------------------------------------------------
03.修改原先的application.yml为bootstrap.yml //放基本固定内容
spring:
  application:
    name: order-server
  cloud:
    config:
      label: master  #分支名称
      name: order    #配置文件名称
      profile: dev   #读取后缀名称:
      uri: http://localhost:7000
      #上述4个综合: http://localhost:7000/master/order-dev.yml
eureka:
  client:
    #是否将自己注册进去eureka,false为不注册,true注册
    registerWithEureka: true
    #是否从eureka抓取注册信息,单点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    serviceUrl:
      defaultZone:  http://localhost:8761/eureka/
--------------------------------------------------------------
 04.放原本配置下,除了现在已经在bootstrap中配置的内容
 05.启动eureka服务器  启动config服务器  启动order服务器
 观察order端口是不是8090(从码云上加载而来)

//此时若是修改码云上的配置内容,服务器中还不能做到同步修改,还需要借助
//更新码云上配置,服务动态更新配置:Bus数据总线+RocketMQ消息中间件

boostrap与application区别

application.yml 是用户级别的资源配置项
bootstrap.yml 是系统级别,优先级更高

Spring cloud 会创建一个 Bootstrap Context 作为 Spring 应用的Application Context 的父上下文,初始化的时候, BootstrapContext负责从外部加载配置属性并解析,这2个上下文共享一个从外部的Environment

Bootstrap 属性有高优先级, 默认情况下, 它们不会被本地配置覆盖,Bootstrap context 跟 Application Context 有着不同的约定, 所以新增一个bootstrap.yml文件保证Bootstrap Context 跟 Application Context 配置分离

要将Client 模块下的application.yml 改成bootstrap.yml,这是很关键的,因为bootstrap.yml是必application.yml先加载的, bootstrap.yml优先级高与application.yml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值