Spring Cloud Config笔记总结

本文介绍如何使用Spring Cloud Config实现配置中心,包括搭建Git仓库存放配置文件、配置ConfigServer连接Git仓库以及在客户端应用配置文件。通过具体步骤演示整个流程。

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

创建一个git仓库:

1、 搭建一个git服务或者使用github私有仓库

2、 在git服务中创建一个仓库用于存放我们的配置文件

3、 比如创建名为:siki-xhb-config

4、 在仓库中创建目录:siki-xhb-repository

5、 在目录下创建与服务名相同的文件名:siki-xhb-hello-dev.yml

6、 在各个服务名相同的文件夹下创建我们不同环境的配置文件

一、Config Server

1、 引入Spring Cloud Config依赖

	 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

2、 在启动类上加上注解,来加载config Server
@EnableConfigServer
3、 在配置文件中配置我们的Config Server配置

spring:
  application:
    name: siki-xhb-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ccl125/siki-xhb-config.git			  			//配置文件存放地址
          search-paths: siki-xhb-repository/{application},{application}				//仓库的文件目录
          username: ccl125															//git用户
          password: xxxxxx													 		//git密码	
          skipSslValidation: true													//忽略ssl验证
          default-label: main														//指定main分枝

二、Config Client

1、 引入config client依赖

	    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

2、 将配置文件名改为bootstrap.yml

3、 添加config client 配置

spring:
  application:
    name: siki-xhb-hello
  profiles:
    active: dev									//配置环境(开发)
  cloud:
    config:
      uri: http://192.168.101.23:40003			//注册中心的Config Server地址
      name: ${spring.application.name}			//配置文件名称
      profile: ${spring.profiles.active}		//后缀
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值