说说如何使用 Spring Cloud Config 搭建配置中心服务

525 阅读2分钟

(1)新建 Spring Boot  项目   

新建一个基于 Spring Boot 的 Maven项目模块,名为 app-config。

(2)配置 pom.xml

pom 文件配置为:


<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

 <!--author: Deniro Lee-->

 <modelVersion>4.0.0</modelVersion>

 <!--模块基础信息-->

 <description>项目配置中心</description>

 <groupId>com.xxx.cc</groupId>

 <artifactId>app-config</artifactId>

 <version>1.0.0</version>

 <name>app-config</name>

 <!--包类型-->

 <packaging>jar</packaging>

 <!--引入项目父级依赖-->

 <parent>

 <groupId>com.xxx.cc</groupId>

 <artifactId>ms-app</artifactId>

 <version>1.0-SNAPSHOT</version>

 </parent>

 <!--当前模块依赖-->

 <dependencies>

 <!--spring-cloud-config-->

 <dependency>

 <groupId>org.springframework.cloud</groupId>

 <artifactId>spring-cloud-config-server</artifactId>

 </dependency>

 </dependencies>

</project>

其中 spring-cloud-config-server 是配置中心服务端。

Spring Cloud 的版本号以伦敦地铁站命名1。它与 Spring Boot 版本之间存在如下对应关系2

Release TrainBoot Version
2020.0.x aka Ilford2.4.x
Hoxton2.2.x, 2.3.x (Starting with SR5)
Greenwich2.1.x
Finchley2.0.x
Edgware1.5.x
Dalston1.5.x

目前 Spring Cloud的 Dalston、Edgware 与 Finchley 版本,官方已经不再维护。

(3)配置注解


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication

@EnableConfigServer

public class AppConfigApplication {

 public static void main(String[] args) {

 SpringApplication.run(AppConfigApplication.class, args);

 }

}

这里在 Spring Boot 的 Application 启动类中加入了一行注解 @EnableConfigServer。

(4)配置 applicaton.yml

# 端口
server:
  port: 8881

spring:
  cloud:
    config:
      server:
        native:
          # 配置文件库所在路径 
          searchLocations: classpath:/shared
          
  # 使用文件系统加载配置信息
  profiles:
     active: native
  • server.port 指定了服务配置中心的端口号;

  • spring.profiles.active=native 表示采用文件系统来加载配置信息。它需要配合 spring.cloud.config.server.native.searchLocations 属性才能生效。searchLocations 属性表示配置文件所在路径,可以是 classpath 路径或者文件系统路径3

  • searchLocations 属性支持多路径,形如:[classpath:/, classpath:/config, file:./, file:./config] classpath: 前缀表示classpath 路径, file: 前缀表示文件系统路径。

注意:基于文件系统的配置模式可以很方便地开始,而且易于测试。但如果在生产环境,我们必须要确保文件系统可靠并且能够被所有的配置中心实例所共享。

(5)环境配置映射流程

配置文件命令规则为:应用名-环境名.yml。请求的 URL 地址会被配置中心转化为相应的配置文件名称。比如 app-admin/dev 会被转化为 app-admin-dev.yml。默认的配置文件就是 应用名.yml

假设 app-admin应用默认情况下使用 Oracle 数据库,开发环境下使用 MySQL 数据库。

这时 app-admin.yml 定义的内容为:

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.Oracle10gDialect

app-admin-dev.yml 定义的内容为:

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect

配置中心启动后,浏览器访问 http://localhost:8881/app-admin/dev,就可以得到 JSON 形式的 app-admin-dev.yml 中的内容。

可以看到响应内容中的 profiles 为 dev,而且还附带上了app-admin.yml 中的内容。

当 Spring 框架执行属性解析时,它会先查找默认配置中的属性值,然后再用特定环境的值(如果存在)去覆盖默认属性值1


1 约翰·卡内尔. Spring微服务实战(异步图书) (Kindle位置1570-1666). 人民邮电出版社. Kindle 版本. 2 Spring Cloud OVERVIEW. 3 File System Backend.