欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud Config(jdbc) 搭建

程序员文章站 2022-07-03 19:42:33
...

众所周知

Spring Cloud Config 支持git,svn,本地文件和jdbc的部署,但是关于jdbc的教程不多,所以这里记录下。

官网描述:

地址:https://cloud.spring.io/spring-cloud-config/reference/html/#_spring_cloud_config_server

Spring Cloud Config(jdbc) 搭建
这样你就可以在你后台进行增删查改了。

启动

配置文件

server:
  port: 18003
  tomcat:
    uri-encoding: UTF-8
    threads:
      max: 800
      min-spare: 30
spring:
  cloud:
    config:
      server:
        jdbc:
          order: 1
          sql: "SELECT config_name, config_value from sys_config where application=? and profile=? and label=?"
    consul:
      host: 172.18.63.211
      port: 8500
      discovery:
        prefer-ip-address: true
        heartbeat:
          enabled: true
        serviceName: ${spring.application.name}
  application:
    name: jiangsu-config
  profiles:
    active: jdbc
  jackson:
    date-format: 'yyyy-MM-dd HH:mm:ss'
    time-zone: GMT+8
  datasource:
    username: docker
    password: aaa@qq.com
    url: jdbc:mysql://172.18.63.211:3306/jiangsu_authcharacterEncoding=utf8&serverTimezone=Asia/Shanghai

注意点:

  1. spring.profiles.active必须为jdbc
  2. 文件名必须为application.yml或者其他规定格式,不能带如-dev标识
  3. sql可以自定义

依赖

<dependencies>
    <!-- consul -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <!-- actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- hystrix -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <!-- web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- config -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- logging -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

启动类

@SpringCloudApplication
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
        System.out.println("http://localhost:18003");
    }
}

数据表

-- ----------------------------
-- Table structure for sys_config
-- ----------------------------
DROP TABLE IF EXISTS `sys_config`;
CREATE TABLE `sys_config`  (
  `id` bigint(0) NOT NULL COMMENT '主键',
  `application` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '应用名称',
  `profile` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '环境dev/prod',
  `label` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '标签,默认master',
  `config_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '配置名称',
  `config_value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '配置值',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统配置' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of sys_config
-- ----------------------------
INSERT INTO `sys_config` VALUES (1, 'jiangsu-lexicon', 'dev', 'master', 'use.bzj', '1', '2020-06-03 17:05:56', NULL);

测试

restful

 /{application}/{profile}[/{label}]
 /{application}-{profile}.yml
 /{label}/{application}-{profile}.yml
 /{application}-{profile}.properties
 /{label}/{application}-{profile}.properties

GET http://localhost:18003/jiangsu-lexicon/dev/master

返回:

{
    "name":"jiangsu-lexicon",
    "profiles":[
        "dev"],
    "label":"master",
    "version":null,
    "state":null,
    "propertySources":[
        {
            "name":"jiangsu-lexicon-dev",
            "source":{
                "use.bzj":"1"
            }
        }]
}

客户端测试

官网:https://cloud.spring.io/spring-cloud-config/reference/html/#_spring_cloud_config_client

依赖

<!--config client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

配置文件

spring:
  application:
    name: jiangsu-lexicon
  cloud:
    config:
      name: ${spring.application.name}
      profile: ${spring.profiles.active}
      label: master
      uri: http://localhost:18003

测试代码(常用)

    @Value("${use.bzj}")
    public String bzj;

    @ApiOperation(value = "config")
    @GetMapping("/bzj")
    public R<String> bzj() {
        return R.ok(bzj);
    }

最后

  1. config 不会自动刷新配置信息,即使你修改了配置值
  2. 使用了config server,越来越复杂,或许需要替代品