Spring Cloud Config(Greenwich版本)基于数据库搭建统一配置中心

简介: Spring Cloud Config(Greenwich版本)基于数据库搭建统一配置中心

Spring Cloud Config(统一配置中心)


github开源项目地址:https://github.com/hack-feng/spring-cloud

这里是本项目的配置中心,开始基于git进行配置,后改为基于Mysql数据库配置,并增加前端UI配置界面。

基于git配置教程:https://blog.csdn.net/qq_34988304/article/details/95784724


前端UI界面:https://github.com/zhua-an/adminUI.git


这里介绍本项目的配置和基于Mysql搭建配置中心


config-master项目搭建


pom文件引入:

<!-- Spring Cloud ==> 引入config配置中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>


数据库表结构:

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for system_config_properties
-- ----------------------------
DROP TABLE IF EXISTS `system_config_properties`;
CREATE TABLE `system_config_properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `key1` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '配置中key的值',
  `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL COMMENT '配置中value的值',
  `application` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '应用名',
  `profile` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '环境',
  `label` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '读取的分支',
  `sort` int(11) DEFAULT '0' COMMENT '排序',
  `create_date` datetime DEFAULT NULL COMMENT '创建时间',
  `modify_date` datetime DEFAULT NULL COMMENT '修改时间',
  `create_id` int(11) DEFAULT NULL COMMENT '创建人',
  `modify_id` int(11) DEFAULT NULL COMMENT '修改人',
  `remark` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=185 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='系统配置-config动态配置';

ConfigMasterApplication.java文件:

@SpringBootApplication
@EnableConfigServer     //配置中心的服务端
public class ConfigMasterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigMasterApplication.class, args);
    }
}

application.yml文件:

server:
  port: 2000
spring:
  profiles:
    active: jdbc
  application:
    name: config-master
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: 123456
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/maple?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
  cloud:
    config:
      label: master
      server:
        jdbc:
          sql: SELECT key1, value1 from system_config_properties where APPLICATION=? and PROFILE=? and LABEL=?
        default-label: master
      profile: dev

spring.profiles.active为spring读取的配置文件名,从数据库中读取,必须为jdbc

spring.datasource配置了数据库相关的信息

spring.cloud.config.label读取的配置的分支,这个需要在数据库中数据对应

spring.cloud.config.server.jdbc.sql为查询数据库的sql语句,该语句的字段必须与数据库的表字段一致


Config Click搭建


创建gateway-master click项目,在resources目录下创建bootstrap.yml文件


  • bootstrap.yml是Spring Cloud的配置文件,在Spring Cloud项目启动时,会优先加载该配置文件
  • application.yml是Spring Boot的配置文件,其加载优先级在bootstrap.yml 之后


bootstrap.yml文件配置:

spring:
  application:
    name: gateway-master
  cloud:
    config:
      uri: http://127.0.0.1:2000
      profile: dev
      label: master

在数据库中插入数据:

INSERT INTO `maple`.`system_config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`, `sort`, `create_date`, `modify_date`, `create_id`, `modify_id`, `remark`) VALUES ('1', 'server.port', '5001', 'gateway-master', 'dev', 'master', '1', '2019-08-12 14:56:22', NULL, NULL, NULL, '端口号');
INSERT INTO `maple`.`system_config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`, `sort`, `create_date`, `modify_date`, `create_id`, `modify_id`, `remark`) VALUES ('2', 'maple.test', '测试数据', 'gateway-master', 'dev', 'master', '1', '2019-08-15 16:46:22', '2019-08-15 17:43:25', NULL, NULL, 'ceshi');

创建TestController:

@RestController("test")
public class TestController {
    // 启动时从配置文件中读取key1为maple.test的value1值
    @Value("${maple.test}")
    private String test;
    @GetMapping
    public String test(){
        return test;
    }
}

启动项目时,还需要配置一些文件:

启动时打印:

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://127.0.0.1:2000
......
o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 5001


说明调用config server成功,若调用的端口号为:8888,则bootstrap.yml配置有误。8888端口号为config server默认的端口号。

启动的端口号是数据库中配置的server.port。


启动成功后,访问:http://127.0.0.1:5001/test


访问:http://127.0.0.1:2000/gateway-master/dev 可以查看项目的配置


页面显示返回:测试数据。到这里config配置中心搭建完成。


配置文件修改后,不停机加载配置信息


在上一节的gateway-master项目中进行优化。

在bootstrap.xml配置文件中添加:

# 开启监控接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
        #include: refresh,health,info #打开部分


在TestController文件中添加注解 @RefreshScop

启动项目后,修改maple.test的值为:“修改后的测试数据”,使用postman POST调用:http://127.0.0.1:5001/actuator/refresh 接口触发刷新


spring boot2.0版本之前,刷新调用: http://127.0.0.1:5001/refresh

spring boot2.0版本之后,刷新调用: http://127.0.0.1:5001/actuator/refresh


重新访问:http://127.0.0.1:5001/test 返回:“修改后的测试数据”


使用 Spring Cloud Bus消息总线,动态更新配置信息


Spring cloud bus被国内很多都翻译为消息总线,也挺形象的。大家可以将它理解为管理和传播所有分布式项目中的消息既可,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一。


本项目中使用RabbitMQ。安装并启动RabbitMQ,创建admin/123456用户。


基于上文的项目我们继续优化,将项目注册到Eureka注册中心


分别在server和click项目中添加pom依赖:

<!-- Spring Cloud ==> 引入bus的消息总线-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

修改 config-master的application.yml 配置文件:

# 开启监控接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
        # include: refresh,health,info #打开部分

分别在server和click项目中添加rabbitMQ配置:

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: 123456


然后依次启动eureka,config-master,gateway-click项目


POST调用:http://127.0.0.1:2000/actuator/refresh-bus刷新配置文件,该刷新将会刷新所有调用config-master统一配置中心的项目。


/actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务。


使用范围:该/actuator/bus-refresh端点清除@RefreshScope缓存和重新绑定 @ConfigurationProperties


customers:9000 解释:应用程序的每个实例都有一个服务ID,其值可以设置为 spring.cloud.bus.id,其值应该是以冒号分隔的标识符列表,从最不具体到最具体。默认值是从环境构造为spring.application.name和 server.port(或spring.application.index,如果设置)的组合。

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
XML Java Nacos
Spring Boot 整合Nacos 版本兼容适配 史上最详细文档
本文介绍SpringBoot整合Nacos的完整流程,涵盖Nacos下载安装、配置中心与服务发现集成、版本兼容性问题及实战配置。重点解决SpringBoot 3.3.0与Nacos版本适配难题,推荐使用Spring Cloud Alibaba方案,并提供项目开源地址供参考学习。
|
应用服务中间件 Nacos nginx
黑马头条_SpringCloud项目阶段一:环境搭建(Mac版本)
本文为 Mac 用户介绍微服务项目环境搭建,含阿里云服务器用 Docker 装 Nacos 1.2.0,本地通过 brew 装 OpenJDK 8、Maven 3.6.1、Redis,Docker 部署 MySQL 5.7 并配字符集,及 Nginx 安装与反向代理设置,附命令与配置步骤。
727 4
黑马头条_SpringCloud项目阶段一:环境搭建(Mac版本)
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
1867 0
|
Java 关系型数据库 开发工具
idea创建不了spring2.X版本,无法使用JDK8,最低支持JDK17 , 如何用idea创建spring2.X版本,使用JDK8解决方案
本文提供了解决方案,如何在IDEA中创建Spring 2.X版本的项目并使用JDK8,尽管Spring 2.X已停止维护且IDEA不再直接支持,通过修改pom.xml或使用阿里云的国内源来创建项目。
3186 0
idea创建不了spring2.X版本,无法使用JDK8,最低支持JDK17 , 如何用idea创建spring2.X版本,使用JDK8解决方案
|
前端开发 Java Spring
【非降版本解决】高版本Spring boot Swagger 报错解决方案
【非降版本解决】高版本Spring boot Swagger 报错解决方案
1656 3
|
Java 开发工具 对象存储
简化配置管理:Spring Cloud Config与Netflix OSS中的动态配置解决方案
简化配置管理:Spring Cloud Config与Netflix OSS中的动态配置解决方案
433 2
|
JavaScript 前端开发 应用服务中间件
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
1103 0
|
前端开发 JavaScript
vite vue3 config配置
【10月更文挑战第5天】
1111 0
|
Java 测试技术 开发工具