springcloud:一篇学会springcloud config

SpringCloud Config简介

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

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

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

SpringCloud Config介绍

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

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

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

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

说简单点,ConfigServer也是一个maven项目作为微服务,需要到注册中心上注册。他的功能就是读取github或者svn上面的配置信息。ConfigClient就是存在冗余配置信息的多个微服务,需要共同读取ConfigServer配置中心的内容。箭头读取指向可为:ConfigClient -> ConfigServer -> GitHub

SpringCloud ConfigServer基本使用

首先在github或者gitee创建一个仓库,假设仓库名为springcloud-config,切记为开源项目。然后在仓库下新建两个配置文件,如application-dev.yml和application-test.yml,内容如夏:

applicaiton-dev.yml

config:
  info: dev 1.0

applicaiton-test.yml

config:
  info: test 1.0

环境和注解

  1. configServer要加上下面依赖:
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
  1. yml配置
server:
  port: 3344

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

这里所谓说下:bootstrap.yml和application.yml的区别?

bootstrap.yml是系统级的配置文件,加载顺序高于application.yml,通常是配置一些系统级别的配置信息和共有配置信息;通常是用在configClient
application.yml是用户级的配置文件,通常是配置本微服务的私有配置信息
激活configServer注解:

  1. 激活configServer注解
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}

测试

启动configServer后,想要测试configServer到底有没有读取到,可按如下地址进行读取:

http://config-3344.com:3344/master/application-test.yml
config-3344.com:3344:是configServer的域名和端口
master是github上配置文件所处的分支
application-test.yml是哪个配置文件

SpringCloud Config配置文件读取规则

其实我们在github上面创建的配置文件格式是有要求的,这影响到configServer如何读取,默认规则有如下3种:

  1. /{label}/{application}-{profile}.yml
  2. /{application}-{profile}.yml
  3. /{application}/{profile}/{label}
    label表示分支,application是配置文件名前缀;profile是配置文件名后缀;第1种是更加常用的一种方式,用来读取configServer的配置信息;第2种默认分支是master;第3种和第1种差不多,顺序不一样而已

SpringCloud ConfigClient基本使用

  1. 环境
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  1. bootstrap.yml
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: application #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k
  1. 编写controller测试,如下:
@RestController
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}
  1. 访问测试:http://localhost:3355/configInfo

SpringCloud Config动态刷新配置之手动版

既然配置由github上面进行管理,当修改了上面的文件,我们肯定是希望configclient能够动态地更新最新的配置文件,但不好意思。在默认情况下修改github上面的配置,configServer能够实时更新,但是configClient就不行,要实现configClient也能更新,有如下办法:

  1. configclient引入下面依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 修改configclient的yml配置,添加下面配置:
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. configclient,在业务类controller上加上@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController
{
  1. 发送请求手动刷新configclient:curl -X POST "http://localhost:3355/actuator/refresh"

总结

存在的问题:

  1. 需要运维人员手动去发送post请求通知configclient,并且如果configClient的数量一多,每台机器上的configClient都得发送post请求很是麻烦
  2. 得手动发送post请求,程序不能自动处理

目前springcloud config自己还处理不了1和2,不过搭配springcloud bus可以解决1的问题。具体请看链接:https://www.cnblogs.com/ibcdwx/p/16117137.html

posted @ 2022-04-08 11:03  爱编程DE文兄  阅读(59)  评论(0编辑  收藏  举报