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,发现应用已成功从注册中心读取配置文件并注册到注册中心

Spring Cloud Config(三):基于JDBC搭建配置中心的更多相关文章
- Spring Cloud Config、Apollo、Nacos配置中心选型及对比
Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...
- Spring Cloud Config 使用Bus的动态配置中心
server端配置 POM文件 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- Spring cloud config client获取不到配置中心的配置
Spring cloud client在配置的时候,配置文件要用 bootstrap.properties 贴几个说明的链接.但是觉得说的依然不够详细,得空详查. 链接1 链接2 链接3 原文地址:h ...
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- Spring Cloud Config(二):基于Git搭建配置中心
1.简述 本文选用Git作为配置仓库,新建两个环境的配置文件夹,dev 和 test,文件夹中分别存放 Config Client 端的配置文件,目录结构如下: ├ ─ ─ dev └ ─ ─ con ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
- 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- Spring Cloud Alibaba 整合 Nacos 实现服务配置中心
在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...
随机推荐
- Django rest-framework框架-组件之渲染器
渲染器: from rest_framework.renderers import BrowsableAPIRenderer,AdminRenderer,HTMLFormRenderer,JSONRe ...
- insert buffer/change buffer double write buffer,双写 adaptive hash index(AHI) innodb的crash recovery innodb重要参数 innodb监控
https://yq.aliyun.com/articles/41000 http://blog.itpub.net/22664653/viewspace-1163838/ http://www.cn ...
- ORA-3136 问题处理
Alert 日志报错: Wed May :: *********************************************************************** Fatal ...
- 【Spring】源码浅析 - ResponseEntity.ok 转载
https://www.jianshu.com/p/1238bfb29ee1 ResponseEntity.ok具体源码
- rsync & sersync 实时同步
1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.sersync的部署 ①.部署服务(安装和配置过程) #Master 部署Sersync服务 mkdir ...
- MySql忘记密码了咋办
对内 忘记密码终端修改操作: #停止mysql服务 sudo /opt/lampp/lampp stopmysql #参数启动mysqld sudo /opt/lampp/sbin/mysqld -- ...
- gitlab-ce白名单设置杜绝并发数过大引起的封ip故障
gitlab-ce 7.9安装手札以及上篇文章的问题解决 鸣谢 感谢ruby大神===>章鱼的一路指点,才能拨开迷雾见云天! 章鱼大人: 国内Ansible部落原创翻译之一! 资深运维! ROR ...
- mysql总复习
目录 数据库操作 库操作 表操作 数据行操作 表关系操作 单表操作 外键创建 多表联查 pymysql模块 索引 主键索引 唯一索引 普通索引 数据库操作 库操作 create database 库名 ...
- phpstudy修改端口及网站根目录和访问 localhost 显示目录文件夹
一.其它选项菜单=>phpStudy设置=>端口常规设置(勾选允许目录列表): 二. Apache http端口:80 网站目录:D:\phpStudy\PHPTutorial\WWW 默 ...
- Pycharm----设置背景颜色和字体的样式
编辑器默认显示的样式背景为白色,看着会刺眼,也不方便查找我们写的某些参数等,通过设定,可以对页面的样式进行选择更改,方便直观的在编辑器中查看自己所写的代码, 设置前: 设置后 操作方式: