SpringCloud 10 - SpringCloud config 分布式配置中心

SpringCloud 09 - Gateway 网关


1. 概述

1.1 分布式面临的配置问题

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

1.2 简介

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

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

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

官网:Spring Cloud Config

1.3 作用

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如 dev / test / prod / beta / release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生改变时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以 REST 接口的形式暴露:post / crul 访问刷新即可...

1.4 与 GitGub 整合

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

2. Config 服务端配置与测试

2.1 在 gitHub 上新建一个仓库:

① 仓库名字:springcloud-config

② 仓库地址:GitHub - liyingdan/springcloud-config: cloud2020 项目的配置中心仓库

(git 指南:管理修改 - 廖雪峰的官方网站

2.2 新建 module

① 新建项目 cloud-config-center-3344

② pom

    <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>
        <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>

③ yml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/liyingdan/springcloud-config.git #GitHub上面的git仓库名字 https://github.com/liyingdan/springcloud-config.git
          ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: master

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

④ 主启动

package com.janet.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @Date 2020/5/12
 * @Author Janet
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}

⑤ windows 下修改 hosts 文件,增加映射  127.0.0.1 config-3344.com

⑥ 测试通过 Config 微服务是否可以从 GitHub 是否可以从 GitHub 上获取配置内容

启动服务3344:http://config-3344.com:3344/master/config-dev.yml

2.3 读取配置规则

① /{label}/{application}-{profile}.yml

  • dev分支

② /{application}-{profile}.yml

③ /{application}/{profile}/{/label}  :http://config-3344.com:3344/config/dev/master

④ 重点配置细节总结

到这里就成功实现了 SpringCloudConfig 通过 Github 获取配置信息。

3. Config 客户端配置与测试

① 新建module  cloud-config-client-3355

② POM

    <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.ymI”文件,保证 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://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

④ 主启动

package com.janet.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @Date 2020/5/12
 * @Author Janet
 */

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}

⑤ 业务类

package com.janet.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Date 2020/5/12
 * @Author Janet
 */
@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

⑥ 测试:

  • 启动 Config 配置中心 3344 微服务并自测

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

⑦ 分布式配置的动态刷新问题:

修改 GitHub 上的配置文件内容做调整

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

刷新 3355,发现 ConfigClient 客户端没有任何响应,除非自己重启重新加载才能获取新的配置。

4. Config 客户端之动态刷新

避免每次更新配置都要重启客户端服务 3355

4.1 动态刷新

步骤:

修改3355模块:

① POM 引入 actuator 监控

② 修改YML,暴露监控端口

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
      
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

③ 业务类 Controller 修改:加上 @refreshScope 注解

④ 测试:

修改 github,查看 3344,查看 3355,发现如果不重启,3344 会改变,而 3355 依旧不会变。

⑤ 需要发送 Post 请求刷新3355:curl -X POST "http://localhost:3355/actuator/refresh"

再次刷新,3355 获得最新的配置:

4.2 其他问题

假如有多个微服务客户端3355,3366...每个微服务都要执行一次post请求,手动刷新?

可否广播,一次通知,处处生效。想大范围的自动刷新求方法?


SpringCloud 11 - SpringCloud Bus 消息总线

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值