6. Spring Cloud 分布式配置中心入门 ,Spring Cloud Config(Finchley版本)

本文介绍了Spring Cloud Config的使用,包括服务端配置、客户端配置和配置文件的管理。通过创建config-server,设置git仓库地址,然后启动服务,客户端通过bootstrap.yml连接配置中心,拉取对应的环境配置。通过示例展示了配置文件内容,并提供了访问接口验证配置拉取成功。

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

1.Spring Cloud Config介绍

  • Spring Cloud Config不是Netflix公司开源的项目,其实spring团队针对于微服务打造的一个全新的服务,目的是为了治理实际业务中服务过多导致的配置文件繁杂难以维护的问题。
  • Spring Cloud Config可以对微服务配置进行统一的外部管理,并且默认采用Git来管理配置信息。将每个微服务的配置文件抽离出来,放置到统一的文件夹下管理或者针对于不同项目仓库,统一拉取管理。
  • Spring Cloud Config包含了服务端Server和客户端Client。服务端用于从Git仓库中加载配置,并且缓存到本地,客户端用于从服务端获取配置信息。

2.创建Spring Cloud Config的服务端

  • 在centralpark父项目下创建子项目config-server
  • pom.xml文件中,引入依赖spring-cloud-config-server
<?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>
  • 启动类中ConfigServerApplication.java,添加注解@EnableConfigServer,声明为配置服务类
@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: configServer
---
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/SpanishSoap/spring-cloud-example.git
          search-paths: config
          #username: xxx   若是私有仓库添加账号密码凭证
          #password: xxx
  profiles: configServer

spring.application.name:ConfigServer服务名称
spring.profiles.active:configServer代表需要激活的配置文件子部分,现在只有一个子配置
spring.cloud.config.server.git.uri: https://xx.gitgit地址
spring.cloud.config.server.git.search-paths:config代表搜索的子文件夹
spring.profiles:configServer子配置名称

3.config-server中conf文件夹下配置文件


ConfigClient.yml

config: I’m ConfigClient
config-comm: I’m ConfigClientComm

ConfigClient-dev.yml

config: I’m ConfigClient-Dev
config-comm: I’m ConfigClientComm-Dev

ConfigClient-test.yml

config: I’m ConfigClient-Test
config-comm: I’m ConfigClientComm-Test

4.启动config-server服务

  • 访问http://localhost:8202/ConfigClient/dev
    可见:
{
    "name": "ConfigClient",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": "e2ac48f72403073325241f56a255ee062a106f68",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/SpanishSoap/spring-cloud-example.git/config/ConfigClient-dev.yml",
            "source": {
                "config": "I'm ConfigClient-Dev",
                "config-comm": "I'm ConfigClientComm-Dev"
            }
        },
        {
            "name": "https://github.com/SpanishSoap/spring-cloud-example.git/config/ConfigClient.yml",
            "source": {
                "config": "I'm ConfigClient",
                "config-comm": "I'm ConfigClientComm"
            }
        }
    ]
}
  • 若访问默认的ConfigClient.yml配置文件,可以使用:http://localhost:8202/ConfigClient/default

  • 启动服务后访问配置规则为:

    1. /{application}/{profile}[/{label}]
    2. /{application}-{profile}.yml
    3. /{label}/{application}-{profile}.yml
    4. /{application}-{profile}.properties
    5. /{label}/{application}-{profile}.properties
  • {application}spring.application.name
    {profile}spring.profiles.active
    {label}为git分支,若不指定默认为master

5.搭建客户端config-client

  • 有了config-server服务端,可以从git端拉取配置文件到本地缓存,现在创建config-client从config-server中拉取配置到自己的配置文件中
  • 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-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</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-starter-config</artifactId>
        </dependency>
    </dependencies>
</project>
  • 启动类中,不用任何特殊配置
  • 创建ConfigClientApplicationController.java读取配置文件,验证是否拉取配置成功
@RestController
public class ConfigClientApplicationController {
  @Value("${config}")
  private String configString;

  @GetMapping("configString")
  public String configString(){
      return configString;
  }

  @Value("${config-comm}")
  private String configCommString;

  @GetMapping("configCommString")
  public String configCommString(){
      return configCommString;
  }
}
  • 项目主配置文件中bootstrap.yml,注意这里一定要使用bootstrap配置文件引导,不能使用application
server:
  port: 8301
spring:
  profiles:
    active: profilesA
---
spring:
  profiles: profilesA
  application:
    name: ConfigClient
  cloud:
    config:
      uri: http://localhost:8201/
      label: master
      profile: dev
  • spring.cloud.config.uri: config-server地址
    spring.cloud.config.label: 拉取git分支,对应{label}
    spring.cloud.config.profile: 拉取环境,对应{profile}
    spring.profiles.name:拉取主体,对应{application}

6.启动config-client服务

  • 访问接口http://localhost:8301/configString返回,获取配置成功

    I’m ConfigClient-Dev

上一篇:Spring Cloud声明式服务调用,Feign(Finchley版本)
下一篇:Spring Cloud 分布式配置中心,配置文件子目录存储以及高可用 ,Spring Cloud Config(Finchley版本)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值