• Spring Cloud Config 使用 JDBC 方式


    Config Server

        
            
                org.springframework.boot
                spring-boot-starter-jdbc
            
            
                org.springframework.cloud
                spring-cloud-config-server
            
            
                org.springframework.cloud
                spring-cloud-starter-eureka
            
    
            
                mysql
                mysql-connector-java
                runtime
            
    
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    添加 @EnableConfigServer

    @EnableConfigServer
    @SpringBootApplication
    public class ConfigServerJdbcApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerJdbcApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    application.properties

    management.security.enabled=false
    #config默认支持git模式,但是同时也提供了svn,vault,jdbc三种配置模式
    spring.profiles.active=jdbc
    #数据库配置
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/qinwei?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    #config配置
    spring.cloud.config.label=master
    spring.cloud.config.server.jdbc=true
    #重写SQL,因为KEY为mysql关键字,在mysql需要使用反引号来表示其为字段
    spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    application.yml

    server:
      port: 8090
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8081/eureka-server/eureka/
    spring:
      application:
        name: config-server-jdbc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    mysql数据库中创建 properties 表

    CREATE TABLE `properties` (
      `id` int(11) NOT NULL,
      `key` varchar(50) DEFAULT NULL,
      `value` varchar(500) DEFAULT NULL,
      `application` varchar(50) DEFAULT NULL,
      `label` varchar(50) DEFAULT NULL,
      `profile` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这里写图片描述

    Config Client

    bootstrap.properties

    server.port=8088
    eureka.client.service-url.defaultZone=http://localhost:8081/eureka-server/eureka/
    spring.cloud.config.label=master
    spring.cloud.config.profile=dev
    spring.cloud.config.discovery.enabled=true
    spring.cloud.config.discovery.service-id=config-server-jdbc
    #设为true,如果无法连接config server,启动时会抛异常,并停止服务
    spring.cloud.config.failFast=true
    #关闭权限验证
    management.security.enabled=false
    #显示每个服务实例发送的所有事件和所有的ack
    spring.cloud.bus.trace.enabled=true
    
    // 必须在使用的地方加上 @RefreshScope 注解,否则无效
    @RefreshScope
    @RestController
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ConfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApplication.class, args);
        }
    
        @Value("${msg}")
        String msg;
        @Value("${name}")
        String name;
        @Value("${version}")
        String version;
    
        @RequestMapping("/hello")
        public String getConfig() {
            return "msg = " + msg + "; name = " + name + "; version = " + version;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    POST 请求调用 http://localhost:8088/refresh ,可以看到配置刷新了
    这里写图片描述

    如果使用消息总线,可以通过POST 请求调用 http://localhost:8088/bus/refresh ,刷新整个 client 集群
    这里写图片描述

    http://localhost:8088/bus/refresh 可以添加 destination 参数刷新指定的服务,如/bus/refreshdestination=config-client:8089
    这里写图片描述

    **注意:**我总共启了3个client,端口分别是8087、8089、8089,如果请求http://localhost:8088/bus/refresh destination=config-client:8089,则8088和8089会刷新配置,而8087不会。也就是说你调用哪个服务的POST请求,那个服务肯定会刷新。
    destination参数的值指的是 服务的ApplicationContext ID,默认情况下,ApplicationContext ID是spring.application.name:server.port

  • 相关阅读:
    项目中升级mysql遇到的若干问题
    2023-05-26:golang关于垃圾回收和析构函数的选择题,多数人会选错。
    axios 源码简析
    2023下半年软考各地区准考证打印时间汇总
    GC调优思路
    这几个高效软件简直是打工人的宝藏软件
    从0到1实现五子棋游戏!!
    联想M7216NWA打印一体机墨粉清零方法
    Nginx实现四层代理与七层代理
    暑期JAVA学习(42.1)TCP通信——使用线程池优化
  • 原文地址:https://blog.csdn.net/fwdwqdwq/article/details/126496802