Spring Cloud Config(三):基于JDBC搭建配置中心
1、简介
本文主要内容是基于jdbc搭建配置中心,使应用从配置中心读取配置信息并成功注册到注册中心,关于配置信息表结构仅供参考,大家可以根据具体需要进行扩展。
2、Config Server 搭建
2.1、Maven 依赖
因为需要从数据库读取配置文件,所以需要添加MySQL的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2.2、JDBC 数据库准备
这里的表结构只为测试使用,具体可根据业务需要进行调整,此结构仅供参考
-- ----------------------------
-- Table structure for properties
-- ----------------------------
DROP TABLE IF EXISTS `properties`;
CREATE TABLE `properties` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` varchar(50) DEFAULT NULL,
`value` varchar(500) DEFAULT NULL,
`application` varchar(50) DEFAULT NULL,
`profile` varchar(50) DEFAULT NULL,
`label` varchar(50) DEFAULT NULL,
`remark` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of properties
-- ----------------------------
BEGIN;
INSERT INTO `properties` VALUES (2, 'eureka.client.serviceUrl.defaultZone', '${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/', 'eureka-client', 'dev', 'v0.0.1', '配置中心地址');
INSERT INTO `properties` VALUES (3, 'management.endpoint.conditions.enabled', 'true', 'eureka-client', 'dev', 'v0.0.1', '启用终结点');
INSERT INTO `properties` VALUES (4, 'eureka.instance.prefer-ip-address', 'true', 'eureka-client', 'dev', 'v0.0.1', '使用IP地址注册到注册中心');
INSERT INTO `properties` VALUES (5, 'spring.application.name', 'eureka-client', 'eureka-client', 'dev', 'v0.0.1', '应用名称');
INSERT INTO `properties` VALUES (6, 'eureka.instance.instanceId', '${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}', 'eureka-client', 'dev', 'v0.0.1', '在注册中心的实例ID');
INSERT INTO `properties` VALUES (7, 'management.endpoints.web.exposure.include', '*', 'eureka-client', 'dev', 'v0.0.1', '开放哪些监控端口');
INSERT INTO `properties` VALUES (8, 'server.port', '8000', 'eureka-client', 'dev', 'v0.0.1', '应用服务端口号');
COMMIT;
2.3、Config Server 配置
server.port=1000
spring.application.name=lkf-cloud-config-jdbc
spring.profiles.active=jdbc
#数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lkf_cloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#读取配置文件的SQL语句
spring.cloud.config.server.jdbc.sql=SELECT `key`,`value` FROM properties WHERE application=? AND PROFILE=? AND label=?
# 配置中心api前缀
spring.cloud.config.server.prefix=lkf
启动配置中心,访问 http://localhost:1000/lkf/eureka-client/dev/v0.0.1,得到应用为 eureka-client ,开放环境【dev】,版本号为【v0.0.1】 的配置信息
{
"name": "eureka-client",
"profiles": ["dev"],
"label": "v0.0.1",
"version": null,
"state": null,
"propertySources": [{
"name": "eureka-client-dev",
"source": {
"eureka.client.serviceUrl.defaultZone": "${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/",
"management.endpoint.conditions.enabled": "true",
"eureka.instance.prefer-ip-address": "true",
"spring.application.name": "eureka-client",
"eureka.instance.instanceId": "${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}",
"management.endpoints.web.exposure.include": "*",
"server.port": "8000"
}
}]
}
至此,基于jdbc的配置中心已经成功从MySQL数据库读取配置信息,配置中心搭建完成
3、Config Client 配置
3.1、Maven 依赖
<!--注册中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator 监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2、Config Client 配置
#配置环境
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:1000/lkf
#配置文件名称
spring.cloud.config.name=eureka-client
#配置文件版本号
spring.cloud.config.label=v0.0.1
启动应用,在浏览器访问注册中心:http://127.0.0.1:8888,发现应用已成功从注册中心读取配置文件并注册到注册中心
博主文章同步发布:https://blog.csdn.net/u010647035/article/details/83959552