一、概述
本文将介绍如何使用spring-cloud-config与mysql数据库搭建配置中心(服务端搭建、client端使用),并提供完整的源码。本文使用的技术框架与版本如下:jdk1.8 (java8) 、spring-boot 2.7、spring-cloud-config-3.1.5、spring actuator、mysql。
如果想了解spring-cloud-config如何与git搭建配置中心,可参考本人主页文章《SpringConfig与git搭建配置中心》
二、项目架构
三、 配置与搭建
3.1 Spring cloud config 服务端搭建
Step1: 添加依赖jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Step2: 配置git 地址与密码、分支等
server:
port: 8888
spring:
profiles:
active: jdbc
application:
name: config-jdbc-server
datasource:
url: jdbc:mysql://10.xx.xx.xx:xxxx/config_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&max_query_size=100000000&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
config:
server:
jdbc:
sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?
order: 1
sqlWithoutProfile: SELECT key1, value1 from config_properties where APPLICATION=? and LABEL=?
Step3: 添加spring config server启动注解
@SpringBootApplication
@EnableConfigServer
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
3.2 Spring cloud config 客户端使用配置
Step1: 添加依赖jar
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Step2: 配置spring-config服务端与spring-config刷新配置
spring:
application:
name: application1
config:
import: "optional:configserver:http://localhost:8888"
cloud:
fail-fast: true
profiles:
active: dev
Step3: 配置类使用
在配置类中添加 @RefreshScope注解标注哪些配置需要刷新
@Component
@RefreshScope
public class TestConfig {
@Value("${config.value}")
private String configValue;
public String getConfigValue() {
return configValue;
}
}
3.3 添加MySQL表
CREATE TABLE `config_properties` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`key1` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`value1` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`application` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`profile` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`label` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='配置表'
3.4 添加配置与运行测试
先启动server端,然后添加mysql配置值,最后启动client端
-
Mysql中添加配置数据
insert into config_properties(key1, value1, application, profile, label) values("config.value", "mysql-data", "application1", "dev", "master")
- 获取配置
-
修改mysql配置值后,使用actuator暴露的接口刷新配置
curl -X 'POST' 'http://127.0.0.1:8080/actuator/refresh'
四、 结束语
项目github地址:见链接。如有前后端技术问题,欢迎一起交流!