7. Spring Cloud 分布式配置中心,配置文件子目录存储以及高可用 ,Spring Cloud Config(Finchley版本)

本文详细介绍了如何在Spring Cloud Config中实现配置文件的多子目录存储,以及配置中心的高可用。首先,通过改造配置文件和配置服务端,实现了不同类型的配置文件分散在不同子目录下,并在客户端成功加载。接着,通过配置多个Config Server实例并注册到Eureka,以及在客户端使用服务名访问,确保了Config Server的高可用性。
摘要由CSDN通过智能技术生成

一.Spring Cloud Config配置文件多子目录存储

  • 在上篇Spring Cloud 分布式配置中心入门的基础上进行配置文件多子目录存储的拓展
  • 在实际项目中,我们有不同类型的配置文件,例如:数据库连接,redis连接,spring cloud不同模块配置等,我们希望将这些不同类型的配置文件分布在不同的文件夹下,这时就需要多子目录存储

1.配置文件改造

  • 在config中添加2个子目录,如下图,SubDirectoryConfigClient子文件夹模拟一种类型的配置文件,具体内容

    SubDirectoryConfigClient.yml 中内容 config: I’m SubDirectoryConfigClient
    SubDirectoryConfigClient-dev.yml 中内容 config: I’m SubDirectoryConfigClient-Dev
    SubDirectoryConfigClient-test.yml 中内容 config: I’m SubDirectoryConfigClient-Test

  • SubDirectoryConfigCommon下模拟另一种类型配置文件

    SubDirectoryConfigCommon.yml 中内容 config-comm: I’m SubDirectoryConfigCommon

  • 我们现在要做的就是:在Spring Cloud Config的服务端拉取到所有子目录下的配置文件,并且在客户端项目中使用多个配置文件

2.Spring Cloud Config的服务端

  • pom.xml文件和上篇相同,不需要改动,如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.friends</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Cloud</description>
    <parent>
        <groupId>org.friends</groupId>
        <artifactId>centralpark</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>
  • 启动类中不需要改动,如下
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
   
    public static void main(String[] args) {
   
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  • application.yml中,写入搜寻子目录的配置
server:
  port: 8202
spring:
  application:
    name: ConfigServer
  profiles:
    active: subdirectory
---
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/SpanishSoap/spring-cloud-example.git
          search-paths: config
  profiles: configServer
---
<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值