使用 SpringCloud Config 作为配置中心

使用 SpringCloud Config 作为配置中心 #

2022-01-01 08:57

一开始调研的是阿里的Nacos,花了些时间装上服务端,增加一个配置文件。客户端要调用的时候,启动怎么都是失败,提示某个类未找到。搜索后,在官方仓库的issue上,发现不止笔者一个人遇到这个问题 升级Spring 2.4.1 ConfigurationBeanFactoryMetadata 找不到。好家伙,一年多了,这issue还挂着。SpringBoot 都2.6版本了,2.4版本居然还不支持。好像有人说,阿里的开源项目都是kpi项目?放弃。

后面又稍微看了下Apollo,是携程的。但始终不放心,这要是也用着用着,就流产了,那咋搞?要不,还是用回官方的SpringCloud Config 吧。

老实说,SpringCloud Config 的官方文档也实在是不清晰。好在网上文章不少。结合实践,几个小时下来算是把项目搭建起来了。

Config Server #

创建一个普通的SpringBoot 项目。 如果使用默认的Git 作为Config Server 的存储方式,那只需要一个依赖即可。

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

项目配置,application.yaml

server:
  port: 8001

spring:
  application:
    name: configServer
  cloud:
    config:
      server:
        git:
          ## 此处配置Git 仓库地址;如果是私有仓库,需要配置账号密码
          uri: https://gitee.com/cevin15/config.git
          username: xxx
          password: xxx

项目启动类,ConfigerApplication.java

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

在Git 仓库中上传一个配置文件configClient-test.yaml,configClient 为客户端项目的application name,test 为客户端项目的profile。

server:
  port: 8008

访问 localhost:8001/configClient-test.yaml,响应如下,说明Config Server 启动成功

server:
  port: 8008

Config Client #

创建一个普通的SpringBoot 项目。 添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

这里要注意,SpringBoot 在2.4版本之后,需要添加bootstrap 依赖,项目才能读取bootstrap 启动文件 项目配置,bootstrap.yaml

server:
  port: 8080	##增加一个与配置中心配置不一致的端口,看看最终使用哪个:-)

spring:
  application:
    name: configClient
  cloud:
    config:
      label: main
      profile: test
      uri: http://localhost:8001

启动项目,应用端口号 8008,说明参数已经成功注入。

INFO - 28460 Fetching config from server at : http://localhost:8001
INFO - 28460 Located environment: name=configClient, profiles=[test], label=main, version=e06de6240b98bc9c9fad29692d31e59c8ce2d793, state=null
INFO - 28460 Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/cevin15/config.git/file:C:\Users\xxxx\AppData\Local\Temp\config-repo-8787147078801297440\configClient-test.yaml'}]
INFO - 28460 No active profile set, falling back to default profiles: default
INFO - 28460 BeanFactory id=2c1fe4dd-1f53-3283-bcc7-3096bc8368a3
INFO - 28460 Tomcat initialized with port(s): 8008 (http)
INFO - 28460 Starting service [Tomcat]
INFO - 28460 Starting Servlet engine: [Apache Tomcat/9.0.56]
INFO - 28460 Initializing Spring embedded WebApplicationContext
INFO - 28460 Root WebApplicationContext: initialization completed in 2556 ms
INFO - 28460 Tomcat started on port(s): 8008 (http) with context path ''
INFO - 28460 Started ConfigClientApplication in 10.73 seconds (JVM running for 11.615)

支持JDBC 的Config Server #

SpringCloud Config 除了Git,还支持数据库存储配置信息。我们只需要一个表,5个字段即可。 我们使用数据库作为存储库,在ConfigServer项目中添加如下数据库依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>

修改项目配置,application.yaml

server:
  port: 8001

spring:
  application:
    name: configServer
  profiles:
  	active: jdbc ##此次修改为jdbc,默认为git
  cloud:
    config:
      server:
        jdbc:
          ## Config Server 默认使用此Sql 读取配置
          sql: select c_key, c_value from configer where c_application=? and c_profile=? and c_label=?
  datasource:
  	driver-class-name: com.mysql.cj.jdbc.Driver
  	url: jdbc:mysql://localhost:3306/config
  	username: xxx
  	password: xxx

Config Server 默认使用此Sql 读取配置。现在建一个库config,表configer,然后添加一条数据:

CREATE TABLE `configer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c_key` varchar(255) DEFAULT NULL,
  `c_value` varchar(255) DEFAULT NULL,
  `c_application` varchar(255) DEFAULT NULL,
  `c_profile` varchar(255) DEFAULT NULL,
  `c_label` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
// 端口我们使用8009,跟Git 的配置区分下
INSERT INTO `configer` VALUES ('1', 'server.port', '8009', 'configClient', 'test', 'main');	

启动server项目,不改动client项目,重新启动,响应如下,发现client 使用了8009端口,JDBC 版本的Config Server 启动成功了。

INFO - Fetching config from server at : http://localhost:8001
INFO - Located environment: name=configClient, profiles=[test], label=main, version=null, state=null
INFO - Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-configClient-test'}]
INFO - No active profile set, falling back to default profiles: default
INFO - BeanFactory id=2c1fe4dd-1f53-3283-bcc7-3096bc8368a3
INFO - Tomcat initialized with port(s): 8009 (http)
INFO - Starting service [Tomcat]
INFO - Starting Servlet engine: [Apache Tomcat/9.0.56]
INFO - Initializing Spring embedded WebApplicationContext
INFO - Root WebApplicationContext: initialization completed in 2332 ms
INFO - Tomcat started on port(s): 8009 (http) with context path ''
INFO - Started ConfigClientApplication in 10.394 seconds (JVM running for 11.243)

总结 #

Spring Cloud Config 作为官方出品的配置中心,使用简单,拓展性强,而且升级有保障,很是推荐:-)

文章中的项目已提交到 Gitee,有需要可以参考。