spring cloud config笔记

spring cloud config

官网的spring cloud config文档:Spring Cloud Config

server端

server端主要读取远程源的配置信息到本地,然后给后台微服务提供统一的配置中心。

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

主启动类

使用spring cloud config server需要在主启动类中配置@EnableConfigServer注解,以确保开启configServer的配置。

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

application.yml

需要在application.yml配置文件中配置git的远程仓库地址。配置后,spring cloud config server将会和远程仓库的文件进行信息的同步。

server:
  port: 9001

spring:
  application:
    name: cloud-config9001
  cloud:
    config:
      server:
        git:
          uri: xxx.git         #仓库地址
          search-paths: springcloud-config #扫描路径
      label: master

除了github仓库以外,spring cloud config server还支持重其他源读取配置信息,如AWS的S3

spring:
  profiles:
    active: awss3
  cloud:
    config:
      server:
        awss3:
          region: us-east-1
          bucket: bucket1

资源路径

配置好并启动好spring cloud config项目后,我们就可以通过以下资源路径来获取配置信息啦。其中:

label 分支

application 配置名(一般为微服务应用名)

profile 配置概述(如:dev)

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

官方示例:

curl localhost:8888/foo/development
curl localhost:8888/foo/development/master
curl localhost:8888/foo/development,db/master
curl localhost:8888/foo-development.yml
curl localhost:8888/foo-db.properties
curl localhost:8888/master/foo-db.properties

client端

pom.xml

maven依赖导入:

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

加载优先级

加载文件优先级如下:

  • application.yml 用户级的资源配置
  • bootstrap.yml 系统级,优先级高

先加载优先级高的bootstrap.yml配置,然后再加载优先级第的application.yml,并且优先级高的配置会覆盖优先级低的,也就是说bootstrap.yml先于application.yml(bootstrap.yml和application.yml都有的配置,就选择bootstrap.yml的配置)

bootstrap.yml配置

为了确保微服务自身的配置优先级高于公共配置信息(本地仓库的公共配置或远程的公共配置),所以微服务自身的配置文件我们命名为bootstrap.yml。

spring:
  application:
    name: cloud-config-client
  cloud:
    config:
      label: master     #分支
      name: config      #配置名
      profile: dev      #信息后缀(对应上面的profile)
      uri: http://localhost:9001   #配置中心

动态刷新配置

server端的配置来github等其他源,是属于直接获取的,server端的配置能动态刷新,但是其他config client端想要动态刷新配置,必须在bootstrap.yml添加另外一些配置信息,并且需要添加一些maven依赖。

maven依赖

监控器依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

暴露监控端点

management:
  endpoints:
    web:
      exposure:
        include: *

通过网络接口发起post请求刷新

http://localhost:8888/actuator/refresh 发起post请求,以刷新微服务的配置信息。

@RefreshScope注解

有时后你的代码用会用到一些配置文件的信息,如:一些自定义的bean通过@value注入application.yml中的某些值,或者通过@ConfigurationProperties映射到application.yml中的某些自定义配置项。

这些信息可以通过@RefreshScope来解决动态跟新的配置信息,如下代码中,那么我myconfigbeans就可以通过@RefreshScope以刷新注入到自定义bean中的值。

@Component
@RefreshScope  //使得每次获取bean都是得到配置文件中的最新值
public class myconfigbeans {
    @Value("${configbeans.user}")  //配置文件中的configbeans.user=“YAO”
    String user;
}

广播刷新配置

要使用广播以刷新配置,就要配合spring cloud bus使用,spring cloud bus和spring cloud Stream息息相关。

Stream通过对消息中间件进行抽象封装,提供一个统一的接口供我们发送和监听消息,而Bus则是在Stream基础之上再次进行抽象封装,使得我们可以在不用理解消息发送、监听等概念的基础上使用消息来完成业务逻辑的处理。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config是一个分布式配置管理工具,它提供了集中式的外部配置管理,可以帮助我们在微服务架构中管理和维护应用程序的配置。 Spring Cloud Config的核心思想是将应用程序的配置从代码中分离出来,以便在不重新部署应用程序的情况下进行配置的修改和更新。它使用Git或其他版本控制系统来存储和管理配置文件,并通过HTTP或消息总线将配置文件提供给应用程序。 Spring Cloud Config的主要组件包括: 1. Config Server:配置服务器,负责从Git或其他版本控制系统中读取配置文件,并将其提供给客户端应用程序。它可以通过HTTP或消息总线的方式将配置文件推送给客户端。 2. Config Client:配置客户端,是应用程序中的一个模块,负责从Config Server获取配置文件,并将其应用到应用程序中。 3. Spring Cloud Bus:消息总线,用于在微服务架构中传播配置文件的变更。当配置文件发生变化时,Config Server会通过消息总线通知所有的Config Client,从而实现配置的动态更新。 使用Spring Cloud Config可以实现以下功能: 1. 集中式管理和维护应用程序的配置,避免了在每个应用程序中硬编码配置信息的问题。 2. 实现配置的动态更新,当配置文件发生变化时,应用程序可以自动获取最新的配置。 3. 支持多环境的配置管理,可以为不同的环境提供不同的配置文件。 4. 支持配置的版本控制,可以通过Git或其他版本控制系统管理配置文件的历史记录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值