十三 SpringCloud Config分布式配置中心

clipboard

一、概述

1、分布式服务过程中面临的一些问题?

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

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

的管理 o(╥﹏╥)o

2、SpringCloud-config是什么?

中心化的外部配置

clipboard

clipboard

3、能干嘛

1)集中管理配置文件

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

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

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

5)将配置信息以rest接口的形式暴露

4、与 github整合配置

SpringCloud Config默认使用Git来存储配置文件

二、config服务端的配置和测试

项目结构

clipboard

1) pom文件

<dependencies>
    <!--添加消息总线RabbitMQ支持-->
    <!--<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>-->
    <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>

2)配置文件

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zzyybs/springcloud-config.git  #GitHub上面的git仓库名字
          ####搜索目录
          search-paths:
            - springcloud-config
           #设置跳过ssl校验
          skip-ssl-validation: true 
      ####读取分支
      label: master
#服务注册到eureka 上
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

3)主启动类

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

4)启动成功后,访问路径 http://localhost:3344/master/config-dev.yml

clipboard

【config-server读取配置规则】

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

http://localhost:3344/master/config-dev.yml
http://localhost:3344/master/config-dev.yml
http://localhost:3344/master/config-dev.yml

http://localhost:3344/dev/config-dev.yml
http://localhost:3344/dev/config-dev.yml
http://localhost:3344/dev/config-dev.yml

②、/{application}-{profile}.yml

http://http://localhost:3344/config-dev.yml

为什么没有label,这是因为application.yml中设置了默认的label

clipboard

③ /{application}/{profile}/label

http://localhost:3344/config/test/master

三、config客户端的配置与测试

application.yml和 bootstrap.yml的区别?

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

② SpringCloud会创建一个“BootStrap Context"。作为Spring应用的 ”Application Context“的父上下文。初始化的时候Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment

③ Bootstrap 属性有着高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap Context和 Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和 Application Context的分离

项目路径

clipboard

1)添加pom文件

<dependencies>
    <dependency> <!-- config的客户端依赖 -->
        <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>

2) 编写配置文件 bootstrap.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

【注意】这一段配置和git仓库上的文件路径名称是对应起来的

 

 

3) 主启动类

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

4)controller类:

这个接口是为了直观的查看有没有获取到git上的配置,直接获取配置文件中的 config.info

@RestController
public class ConfigClientController {

    @Value("${config.info}") 
    private String configInfo;  //相当于将git上的配置加载到本地的bootstrap.yml中

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

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return "server port:"+ serverPort + " ---> " +configInfo;
    }
}

5)测试,访问http://localhost:3355/configInfo,获取github中相应文件的内容

clipboard

由上述可见:成功实现了客户端3355访问服务端3344,从而拿到git上的配置

6) 存在的问题

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

【问题详情】

Linux运维修改GitHub上的配置文件内容

clipboard

改为

clipboard

刷新上面的config-server http://localhost:3344/master/config-dev.yml

clipboard

刷新上面的config-client http://localhost:3355/configInfo

clipboard

可见config-client端没有任何变化,除非自己重启,难道每次运维修改配置文件,客户端都要自己重启

四、Config客户端 动态刷新之手动版

1、配置手动动态刷新步骤

修改config-client模块

1)pom文件引入 actuator监控依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2)yml文件暴露监控端口

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

3)在业务类controller上添加 @refreshScope注解

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo; //相当于将git上的配置加载到本地的bootstrap.yml中

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

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return "server port:"+ serverPort + " ---> " +configInfo;
    }
}

4)此时修改github上的配置

clipboard

查看config-server 3344

clipboard

5)运维人员手动发送post请求刷新config-client 3355

clipboard

查看config-client 3355,发现成功刷新配置

clipboard

2、存在的问题  ==》 (有问题才能使技术得到进步,啊哈哈哈哈!!!)

image

到此SpringCoud Config的基本用法就介绍完了,另外还可以配合 SpringCloud Bus实现自动刷新,下节再进行介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Cloud Config配置中心有以下功能: 1. 集中管理应用程序的配置信息 2. 支持多环境配置 3. 支持加密/解密配置信息 4. 支持客户端自动更新配置信息 5. 支持版本管理 6. 支持配置信息的动态刷新 ### 回答2: Spring Cloud Config配置中心是一个可以集中管理、动态更新分布式系统中的配置的工具。它具有以下功能: 1. 集中管理:Spring Cloud Config可以将分布式系统的配置集中存储在一个地方,使得配置信息更加可控和可维护。开发人员可以通过配置服务器统一管理各个环境(如开发、测试、生产)的配置文件,减少配置文件的传统散落问题。 2. 动态更新:在分布式系统中,如果某些配置信息发生变化,传统方式需要重新部署应用程序才能使变化生效。而使用Spring Cloud Config可以实现动态更新,即可以在不重新部署应用的情况下,及时将新的配置信息更新到应用程序中,减少了重新部署对系统的影响和停机时间。 3. 版本管理:Spring Cloud Config允许配置文件进行版本管理,每次更新配置文件都会生成一个新的版本,方便进行配置的历史版本查看和回滚操作。这样可以有效地避免因为配置错误而导致的系统故障,并提高系统的可靠性和稳定性。 4. 分布式支持:Spring Cloud Config支持将配置文件分布式地存储在多个配置服务器中,可以根据需求进行水平扩展和负载均衡,确保系统可扩展性和高可用性。 5. 安全性:Spring Cloud Config提供了对配置文件的安全保护机制,开发人员可以通过对配置文件进行加密和解密操作,确保配置文件的安全性,防止敏感信息泄露。 总的来说,Spring Cloud Config配置中心提供了集中管理、动态更新、版本管理、分布式支持和安全性等功能,帮助开发人员更加方便地管理和维护分布式系统的配置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值