zoukankan      html  css  js  c++  java
  • 十、分布式配置中心 SpringCloud config

    官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

    一、概述

    1.1、分布式系统面临的配置问题

      微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理是必不可少的

      SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理,难以管理

    1.2、是什么

      

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

      SpringCloud Config分为服务端和客户端两部分:

      • 服务端也称为配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户提供获取配置信息,加密/解密信息等访问接口。

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

    1.3、功能作用

    • 集中管理配置文件

    • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

    • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

    • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

    • 将配置信息以REST接口的形式暴露 post、curl访问刷新均可....

    1.4、与Github整合配置

      由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)。

    二、Config服务端配置与测试

    1、环境准备

    • 用你自己的账号在Github上新建一个名为sprincloud-config的新Repository,获得刚新建的git地址
    • 本地硬盘上新建git仓库并clone

    • 表示多个环境的配置文件

    • 保存格式必须为UTF-8

    • 如果需要修改,此处模拟运维人员操作git和github

    2、案例

    • 新建Module模块cloud-config-center-3344它既为Cloud的配置中心模块cloudConfig Center
    • pom文件添加如下依赖
    <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>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    • 编写application.yml配置文件
    server:
      port: 3344
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri:  填写你自己的github路径
              search-paths:
                - springcloud-config
          label: master
    eureka:
      client:
        service-url:
          defaultZone:  http://localhost:7001/eureka
    • 主启动类ConfigCenterMain3344
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigCenterMain3344 {
        public static void main(String[] args) {
                SpringApplication.run(ConfigCenterMain3344 .class,args);
            }
    }

    1.3、配置读取规则

      label:分支

      name:服务

      profile:环境

    三、Config客户端配置与测试

    • 新建Spring Boot 的Module名称:cloud-config-client-3355

    • pom.xml添加如下依赖

    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</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>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
    • 编写bootstrap.yml
      • application.yml:使用户级别的资源配置项,bootstrap.yml是系统界别的,优先级更高。

      • Spring Cloud会闯将一个Bootstrap Context,作为Spring应用的“Application Context”的父上下文。初始化的时候,Bootstrap Context服务从外部源加载配置属性并解析配置,这两个上下文共享一个外部获取的“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。

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        #Config客户端配置
        config:
          label: master #分支名称
          name: config #配置文件名称
          profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.yml
          uri: http://localhost:3344 #配置中心地址k 
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    • 修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
    • 主启动
    @SpringBootApplication
    public class ConfigClientMain3355 {
        public static void main(String[] args) {
                SpringApplication.run( ConfigClientMain3355.class,args);
            }
    }
    • 业务类
    @RestController
    public class ConfigClientController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo(){
            return configInfo;
        }
    }
    • 测试

    • 成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

    • 问题随时而来,分布式配置的动态刷新

      • Linux运维修改GitHub上的配置文件内容做调整

      • 刷新3344,发现ConfigServer配置中心立刻响应

      • 刷新3355,发现ConfigServer客户端没有任何响应

      • 3355没有变化除非自己重启或者重新加载

      • 难道每次运维修改配置文件,客户端都需要重启??噩梦

    四、Config客户端之动态刷新

      避免每次更新配置都要重启客户端微服务3355;动态刷新修改3355

      修改3355

    • pom.xml添加如下依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    • 修改bootstap.yml
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    目前还有什么问题?

    • 假如有多个微服务客户端3355/3366/3377。。。。

    • 每个微服务都要执行一次post请求,手动刷新?

    • 可否广播,一次通知,处处生效?

    • 我们想大范围的自动刷新,求方法

  • 相关阅读:
    【SSH网上商城项目实战15】线程、定时器同步首页数据(类似于CSDN博客定期更新排名)
    【SSH网上商城项目实战14】商城首页UI的设计
    Spring工具类:WebApplicationContextUtils
    多线程技术: 两个线程交替打印奇数和偶数
    常见的几种异常类型Exception
    【SSH网上商城项目实战13】Struts2实现文件上传功能
    【SSH网上商城项目实战12】添加和更新商品功能的实现
    【SSH网上商城项目实战11】查询和删除商品功能的实现
    【SSH网上商城项目实战10】商品类基本模块的搭建
    如何防止通过IP地址访问Tomcat管理页面
  • 原文地址:https://www.cnblogs.com/jdy1022/p/14686755.html
Copyright © 2011-2022 走看看