快速搭建 Spring Cloud Config

本文介绍了如何快速搭建Spring Cloud Config服务端和客户端。Config Server使用Git存储配置信息,支持配置版本管理。搭建过程包括创建服务端和客户端项目,配置bootstrap.yml,启用相关注解。客户端通过Actuator的/refresh端点实现实时刷新配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Cloud Config 分布式配置中心

  • 是Spring Cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。
  • 其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是为服务架构中的各个微服务应用或基础设施,他们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
  • Spring Cloud Config实现的配置中心默认采用Git来存储配置信息。所以使用Spring Cloud Config构建的配置服务器,支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。

    搭建 Spring Cloud Config Server

    1、新建config-server 的model

2、注意这里不是application.yml而新建bootstrap.yml(bootstrap.yml可以获取一些外部配置信息,这些信息优先级高于application.yml,就此实现了外部化配置)

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
#          uri: https://github.com/forezp/SpringcloudConfig/
          uri: https://github.com/25312/ConfigServer/
#          配置Git仓库位置
          searchPaths: respo
#          spring_cloud_in_action/config-repo 配置仓库路径下的相对搜索位置,可以配置多个
          username: 
#          访问Git仓库的用户名
          password: 
#          访问Git仓库的用户名密码
      label: master




server:
  port: 9200

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1122/eureka/

3、在应用程序启动类添加 @EnableConfigServer 注解

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

4、启动测试(先启动高可用配置中心,peer1和peer2):
这里写图片描述
输入:http://localhost:9200/cc/dev/master(意为访问 master 节点下 cc-dev.properties)
这里写图片描述
这里写图片描述
如此便测试成功!

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

/{application}/{profile}[/{label}]:

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

搭建Spring Cloud Client

1、新建config-client 的model

2、配置文件一样是bootstrap.yml

eureka:
  client:
    serviceUrl:
        defaultZone: http://peer1:1122/eureka/
spring:
  application:
    name: config-client
  cloud:
    config:
      uri:  http://localhost:9200/
      label: master
      profile: dev
server:
  port: 9300

3、在应用程序启动类加 @EnableDiscoveryClient 即可

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

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

4、新建一个 TestController 类作为测试获取配置中心 github上的属性值

@RestController
public class TestController {

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

    @RequestMapping("/from")
    public String from(){

        return "hello  :  "+from;
    }

}

5、启动(cpnfig-client应用程序):
这里写图片描述

6、如果改变github中配置文件的 from = cc-test.1.2 改成 from=cc-test.2.3,再次访问 http://localhost:9300/from 发现 from 还是 cc-test.1.2
那么就要在 config-client 的pom.xml文件中新增 spring-boot-starter-actuator监控模块来实现配置信息的动态刷新(其中包含了 /refresh 端点的实现,用于客户端应用配置信息的重新获取与刷新)

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 重新启动config-client,访问 http://localhost:9300/from 可以看到修改后的值
  2. 修改 from 的值:from = cc-test.3.4
  3. 再次访问http://localhost:9300/from还是没有变 还是 cc-test.2.3
  4. POST(Postman)请求 http://localhost:9300/refresh
  5. 再次 http://localhost:9300/from 可以看到更新后的值了

++++++++++++++++++++++++++++++OVER+++++++++++++++++++++++++++++++++++++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值