SpringCloud —— Config 配置中心

前文

SpringCloud 简介

SpringCloud 版本选型

SpringCloud 工程构建

SpringCloud —— Eureka 注册中心

SpringCloud —— Eureka 集群

SpringCloud —— 服务注册进 Eureka 集群

SpringCloud —— Eureka 自我保护

SpringCloud —— SpringCloud Consul 实现服务注册中心

SpringCloud —— 三个注册中心的异同点

SpringCloud —— Ribbon

SpringCloud —— Ribbon 负载均衡算法

扫描二维码关注公众号,回复: 11127821 查看本文章

SpringCloud —— OpenFeign

SpringCloud —— Hystrix 简介

SpringCloud —— Hystrix 断路器

SpringCloud —— HystrixDashboard 服务监控

SpringCloud —— Gateway 网关

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

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

SpringCloud Config 是什么?

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

在这里插入图片描述

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

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

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

SpringCloud Config 能干嘛

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

SpringCloud Config 部署测试

新建 Repository

用自己的 Github 账号在 Github 上新建一个名为 springcloud-config 的新 Repository
在这里插入图片描述
在电脑新建一个文件夹
在这里插入图片描述
将 Github 新建的仓库 clone 到该文件夹下
在这里插入图片描述
在这里插入图片描述

新建 Module(8033)

在这里插入图片描述

编写 application.yml 文件

server:
  port: 8033

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

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

编写启动类

package com.java.springcloud;

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

/**
 * @author Woo_home
 * @create 2020/3/30 12:27
 */

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

修改 hosts 文件

修改 C:\Windows\System32\drivers\etc 下的 hosts 文件,在底部增加以下内容
在这里插入图片描述

测试访问

在启动服务之前先上传一个文件到 Github 上(格式名称如:{application} - {name}.yml),待会让 8033 读取
在这里插入图片描述
    先启动 EurekaServer 7001 再启动 8033,访问 http://config-8033.com:8033/master/config-dev.yml,如下:(注意:在 Github 上新建仓库的时候要设置为 public,不然会报错,具体是不是因为这个我也不知道,知道的朋友可以在评论区评论下,相互学习,谢谢)
在这里插入图片描述
成功从 Github 上获取信息

新建 Module(8055)

上面已经基本实现了配置中心,那么要新建客户端从 server 端读取内容了
在这里插入图片描述

编写 bootstrap.yml 文件

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

    SpringCloud 会创建一个 “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: 8055

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

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

编写启动类

package com.java.springcloud;

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

/**
 * @author Woo_home
 * @create 2020/3/30 13:26
 */

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

编写 Controller

package com.java.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Woo_home
 * @create 2020/3/30 13:27
 */

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

测试访问

先访问 http://eureka7001.com:7001/,可以发现 配置中心 和 客户端 都已成功注册到 EurekaServer
在这里插入图片描述
访问 http://config-8033.com:8033/master/config-dev.yml 配置中心也是 OK 的
在这里插入图片描述
访问 http://localhost:8055/configInfo 客户端也成功了
在这里插入图片描述

Config 客户端动态刷新

为了避免每次更新配置都要重启客户端微服务 8055

动态刷新

修改 8055 模块

在 8055 模块的 POM 文件中引入 actuator
在这里插入图片描述

修改 8055 模块的 bootstrap.yml 文件

在 bootstrap.yml 文件中新增以下配置

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

修改 ConfigClientController

在 ConfigClientController 中增加一个 @RefreshScope 注解,实现自动刷新的功能
在这里插入图片描述

测试访问

修改 Github 上的配置文件
在这里插入图片描述
访问 http://config-8033.com:8033/master/config-dev.yml OK
在这里插入图片描述
使用 curl 工具去刷新一下 8055,这样就不必重启 8055 也可以获取修改之后的内容

curl -X POST "http://localhost:8055/actuator/refresh"

在这里插入图片描述
访问 http://localhost:8055/configInfo 发现获取的也是修改后的内容
在这里插入图片描述


完整代码已上传至码云, 代码地址

发布了227 篇原创文章 · 获赞 1032 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Woo_home/article/details/105194242