Java后端技术-Spring Cloud Config学习笔记

Spring Cloud Config简介

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置

image-20210501104202840

Spring Cloud Config分为服务端和客户端两部分。服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务并为客户端提供获取配置信息,加密/解密信息等访问接口。客户端则是通过指定的配置中心来管理应用资源,以及业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

Spring Cloud Config能干什么?

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

Spring Cloud Config服务端配置

Gitee上新建Config统一配置管理仓库

image-20210501111433064

引入依赖

spring-cloud-config-server

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

配置文件

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/czshh0628/spring-cloud-config.git # 仓库地址
          search-paths:
            - Spring-Cloud-Config # 仓库名
      label: master # 哪个分支
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
# 暴露bus刷新配置的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

主启动类

开启@EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

测试访问

image-20210501111743978

配置读取规则

image-20210501112636645

label:分支(branch) name:服务名 profiles:环境(dev/test/prod)

  • /{application}-{profile}.yml http://config-3344.com:3344/application-dev.yml
  • /{application}/{profile}[/{label}] http://config-3344.com:3344/application/dev/master
  • /{label}/{application}-{profile}.yml http://config-3344.com:3344/master/application-dev.yml

Spring Cloud Config客户端配置

application.yml是用户级的资源配置项

bootstrap.yml是系统级的,优先级更高

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

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

要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

引入依赖

spring-cloud-starter-config

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

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名称
      name: config #配置文件名称
      profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
      uri: http://127.0.0.1:3344 #配置中心的地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
# 暴露bus刷新配置的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

主启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

controller 测试

@RestController
@RefreshScope // 动态刷新
public class ConfigClientController {

    @Value("${spring.profiles}")
    private String configInfo;

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

访问http://localhost:3355/configInfo

image-20210501121405662

动态刷新

由于引入了spring-boot-starter-actuator开启了监控功能,并且配置文件配置了暴露bus刷新配置的端点,controller开启了@RefreshScope动态刷新因此实现了手动动态刷新获取最新配置,还需要工作人员手动发送POST请求curl -X "http://localhost:3355/actuator/refresh"即可获取最新的配置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值