Config解读

Config 简介

springCloud Config 项目是一个解决分布式系统的配置管理方案。它包含了 ClientServer 两个部分,Server 提供配置文件的存储、以接口的形式将配置文件的内容提供出去,Client 通过接口获取数据、并依据此数据初始化自己的应用

目前 springCloud Config 的使用主要是通过 Git/SVN 方式做一个配置中心,然后每个服务从其中获取自身配置所需的参数

springCloud Config 也支持本地参数配置的获取。如果使用本地存储的方式,在 application.propertiesapplication.yml 文件添加 spring.profiles.active=native 配置即可,它会从项目的 resources 路径下读取配置文件。如果是读取指定的配置文件,那么可以使用 spring.cloud.config.server.native.searchLocations = file:D:/properties/ 来读取

Config 作用

springCloud Config 是一个基于 http 协议的远程配置实现方式。通过统一的配置管理服务器进行配置管理,客户端通过 https 协议主动的拉取服务的的配置信息,完成配置获取

简单一点的说就是: 在分布式环境中,很多的服务都是集群部署,那就意味着这些集群部署的服务都需要相同的配置文件。所以,这时候就引入了 springCloud config 这个组件,使用该组件来进行众多的配置文件的统一管理。例如,我们在修改某一配置文件时,只需要在远程的 gitHub 等工具上面修改即可,不用多次的在众多的配置文件中来回繁琐的修改

Config 架构

在这里插入图片描述
springCloud Config 分为服务端和客户端,服务端负责将本地 git 或者 svn 中存储的配置文件发布成 REST 风格的接口,客户端可以从服务端 REST 接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过 POST 方法触发各自的 /refresh 接口。而这时的 springCloud Bus 就发挥了其作用了

springCloud Bus 通过一个轻量级消息代理连接分布式系统的节点(有点像消息队列那种)。这可以用于广播状态更改(如配置更改)或其他管理指令。springCloud Bus 提供了通过 post 方法访问的 endpoint/bus/refreshspringboot 有很多监控的 endpoint,比如/health),这个接口通常由 git 的钩子功能(监听触发)调用,用以通知各个 springCloud Config 的客户端去服务端更新配置

在这里插入图片描述
注意:这是工作的流程图,实际的部署中 springCloud Bus 并不是一个独立存在的服务,这里单列出来是为了能清晰的显示出工作流程

Config 使用示例

服务端

新建项目 spring-cloud-config-server
添加依赖

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

主启动类

在程序的启动类 ConfigServerApplication 通过 @EnableConfigServer 开启 springCloud Config 服务端

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

添加配置

spring.application.name=config-server
server.port=8888
spring.cloud.config.label=master
spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=spring-cloud-config

spring.cloud.config.server.git.username=your username
spring.cloud.config.server.git.password=your password

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
  • spring.cloud.config.server.git.uri:配置 git 仓库地址
  • spring.cloud.config.server.git.searchPaths:配置仓库路径
  • spring.cloud.config.label:配置仓库的分支
  • spring.cloud.config.server.git.username:访问 git 仓库的用户名
  • spring.cloud.config.server.git.password:访问 git 仓库的用户密码
  • eureka.client.serviceUrl.defaultZoneeureka 注册中心地址

远程 Git 仓库

spring-cloud-config 文件夹下有 application-dev.properties,application-test.properties 三个文件,内容依次是:content=hello dev,content=hello test,content=hello pre

在这里插入图片描述

服务测试

启动程序 ConfigApplication 类,访问 springCloud Config Server 服务。浏览器访问:http://localhost:8888/springCloudConfig/dev/master,结果如下

{
    
    
    "name": "springCloudConfig",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16",
    "state": null,
    "propertySources": [
        {
    
    
            "name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",
            "source": {
    
    
                "content": "hello dev"
            }
        }
    ]
}

证明配置服务中心可以从远程程序获取配置信息

springcloud configURL 与配置文件的映射关系

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

客户端

新建项目 spring-cloud-config-client

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

主启动类开启 refresh

通过 @RefreshScope 开启 springCloud Config 客户端的 refresh 刷新范围,来获取服务端的最新配置,@RefreshScope 要加在声明 @Controller 声明的类上,否则 refresh 之后 Conroller 拿不到最新的值,会默认调用缓存

@RefreshScope
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {
    
    

    @Value("${content}")
    String content;

    @Value("${server.port}")
    String port;

    @RequestMapping("/")
    public String home() {
    
    
        return "Hello world ,port:" + port+",content="+content;
    }

    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

添加配置

修改配置文件 application.properties 添加 Eureka 注册中心,配置从 springCloud Config 配置中心读取配置,指定 springCloud Config Service 服务名称

spring.application.name=eureka-provider
server.port=8081

spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
  • spring.cloud.config.label:指明远程仓库的分支
  • spring.cloud.config.profiledev 开发环境,test 测试环境,pro 正式环境
  • spring.cloud.config.uri:指定 config 配置中心的地址
  • spring.cloud.config.discovery.enabled:是否是从配置中心读取文件
  • spring.cloud.config.discovery.serviceId:配置中心的 servieId,服务名称,通过服务名称去 Eureka 注册中心找服务

修改 Git 仓库

按照顺序依次启动各个项目,访问服务注册中心,查看服务是否都已注册成功。在各个服务都已注册成功的情况下,修改 Git 仓库配置

修改 Git 仓库配置,在 content=hello dev 后面加个 123456

在这里插入图片描述
访问服务

命令窗口,通过 curl http://127.0.0.1:9000/hello 访问服务,或者在浏览器访问http://127.0.0.1:9000/hello F5 刷新

发现没有得到最新的值

在这里插入图片描述
再次刷新配置

通过 Postman 发送 POST请求到:http://localhost:8081/refresh,http://localhost:8083/refresh,我们可以看到以下内容:

在这里插入图片描述
再次访问服务

命令窗口,通过 curl http://127.0.0.1:9000/hello 访问服务,或者在浏览器访问http://127.0.0.1:9000/hello F5 刷新

发现:服务8082 没有刷新到最新配置,因为没有手动触发更新。上面使用 Postman 只是手动刷新了端口为 8081,8083 的两个服务

在这里插入图片描述

Config 的缺点,不足

Config Client 实现配置的实时更新,我们可以使用 /refresh 接口触发,如果所有客户端的配置的更改,都需要手动触发客户端 /refresh ,当服务越来越多的时候,那岂不是维护成本很高,显然不太合适

为了解决上述问题,我们可以使用 springCloud Bus 消息总线实现方案,可以优雅的解决以上问题,那就是通过消息代理中间件 RabbitMQGitWebhooks 来触发配置的更新。那具体是怎么实现的,可以参考:https://souyunku.blog.csdn.net/article/details/78891620

文章未完,待更新
参考文章:https://souyunku.blog.csdn.net/article/details/78808976

猜你喜欢

转载自blog.csdn.net/weixin_38192427/article/details/113889125