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

Spring Cloud config中,使用数据库存储配置信息

程序员文章站 2022-04-09 08:39:51
主要内容 1. 在springcloud config中,使用数据库存储配置信息。 系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息 准备数据库 1. 数据库我们使用mysql。 2. 新建库 p config server 3. 创建配置需要的表,并初始化一些配置信息 sql ......

主要内容

  1. 在springcloud config中,使用数据库存储配置信息。

    系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息

准备数据库

  1. 数据库我们使用mysql。
  2. 新建库 p-config-server
  3. 创建配置需要的表,并初始化一些配置信息

    create table `properties` (
      `id` bigint(20) not null auto_increment,
      `application` varchar(64) collate utf8_bin not null default '',
      `profile` varchar(64) collate utf8_bin not null default '',
      `label` varchar(64) collate utf8_bin not null default '',
      `key` varchar(128) collate utf8_bin not null default '',
      `value` varchar(4096) collate utf8_bin not null default '',
      primary key (`id`)
    ) engine=innodb auto_increment=9 default charset=utf8 collate=utf8_bin;
    
    insert into `properties` values ('1', 'p-config-client', 'dev', 'master', 'key-1', 'value-1');
    insert into `properties` values ('2', 'p-config-client', 'dev', 'master', 'key-2', 'value-2');
    insert into `properties` values ('3', 'p-config-client', 'dev1', 'master', 'key-1', 'value-3');
    insert into `properties` values ('4', 'p-config-client', 'dev1', 'master', 'key-4', 'value-4');
    insert into `properties` values ('5', 'p-config-client', 'dev1', 'master', 'key-5', 'value-5');
    

创建eureka注册中心

  1. 创建springcloud项目p-eureka

    groupid: com.ms
    artifactid: p-eureka

    Spring Cloud config中,使用数据库存储配置信息

  2. pom.xml内容

    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelversion>4.0.0</modelversion>
     <parent>
         <groupid>org.springframework.boot</groupid>
         <artifactid>spring-boot-starter-parent</artifactid>
         <version>2.1.4.release</version>
         <relativepath/> <!-- lookup parent from repository -->
     </parent>
     <groupid>com.ms</groupid>
     <artifactid>p-eureka</artifactid>
     <version>0.0.1-snapshot</version>
     <name>p-eureka</name>
     <description>demo project for spring boot</description>
    
     <properties>
         <java.version>1.8</java.version>
         <spring-cloud.version>greenwich.sr1</spring-cloud.version>
     </properties>
    
     <dependencies>
         <dependency>
             <groupid>org.springframework.cloud</groupid>
             <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
         </dependency>
    
         <dependency>
             <groupid>org.springframework.boot</groupid>
             <artifactid>spring-boot-starter-test</artifactid>
             <scope>test</scope>
         </dependency>
     </dependencies>
    
     <dependencymanagement>
         <dependencies>
             <dependency>
                 <groupid>org.springframework.cloud</groupid>
                 <artifactid>spring-cloud-dependencies</artifactid>
                 <version>${spring-cloud.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencymanagement>
    
     <build>
         <plugins>
             <plugin>
                 <groupid>org.springframework.boot</groupid>
                 <artifactid>spring-boot-maven-plugin</artifactid>
             </plugin>
         </plugins>
     </build>
    
    </project>
  3. application.properties改为application.yml

    server:
      port: 7001
    spring:
      application:
        name: p-eureka
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultzone: http://localhost:7001/eureka/
  4. peurekaapplication内容

    package com.ms;
    
    import org.springframework.boot.springapplication;
    import org.springframework.boot.autoconfigure.springbootapplication;
    import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;
    
    @springbootapplication
    @enableeurekaserver
    public class peurekaapplication {
    
     public static void main(string[] args) {
         springapplication.run(peurekaapplication.class, args);
     }
    
    }

    注意:

    • @enableeurekaserver:表示这是一个eureka注册中心
  5. 运行peurekaapplication

  6. 访问http://localhost:7001/

    Spring Cloud config中,使用数据库存储配置信息

    创建配置中心(config server)

  7. 创建springcloud项目p-config-server

    groupid: com.ms
    artifactid: p-config-server

    Spring Cloud config中,使用数据库存储配置信息

  8. pom.xml内容

    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelversion>4.0.0</modelversion>
        <parent>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-parent</artifactid>
            <version>2.0.4.release</version>
            <relativepath/> <!-- lookup parent from repository -->
        </parent>
        <groupid>com.ms</groupid>
        <artifactid>p-config-server</artifactid>
        <version>0.0.1-snapshot</version>
        <name>p-config-server</name>
        <description>demo project for spring boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>finchley.sr1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupid>org.springframework.cloud</groupid>
                <artifactid>spring-cloud-config-server</artifactid>
            </dependency>
            <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-jdbc</artifactid>
            </dependency>
            <dependency>
                <groupid>mysql</groupid>
                <artifactid>mysql-connector-java</artifactid>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-test</artifactid>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencymanagement>
            <dependencies>
                <dependency>
                    <groupid>org.springframework.cloud</groupid>
                    <artifactid>spring-cloud-dependencies</artifactid>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencymanagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupid>org.springframework.boot</groupid>
                    <artifactid>spring-boot-maven-plugin</artifactid>
                </plugin>
            </plugins>
        </build>
    
    </project>
  9. application.yml 内容

    server:
      port: 8080
    eureka:
      client:
        service-url:
          defaultzone: http://localhost:7001/eureka/
    spring:
      application:
        name: p-config-server
      cloud:
        config:
          enabled: true
          profile: jdbc
          server:
            jdbc:
              sql: select `key`, `value` from properties where application=? and profile=? and label=?
      datasource:
        driver-class-name: com.mysql.jdbc.driver
        url: jdbc:mysql://localhost:3306/config-server?characterencoding=utf-8
        username: root
        password: root123
        type: com.zaxxer.hikari.hikaridatasource
      profiles:
        active: jdbc

    注意

    • 数据连接地址、用户名、密码,这些值根据自己的机器进行修改
  10. pconfigserverapplication内容

    package com.ms;
    
    import org.springframework.boot.springapplication;
    import org.springframework.boot.autoconfigure.springbootapplication;
    import org.springframework.cloud.config.server.enableconfigserver;
    import org.springframework.cloud.netflix.eureka.enableeurekaclient;
    
    @springbootapplication
    @enableconfigserver
    @enableeurekaclient
    public class pconfigserverapplication {
    
        public static void main(string[] args) {
            springapplication.run(pconfigserverapplication.class, args);
        }
    
    }

    注意:

    • @enableconfigserver:表示这是一个注册中心应用
    • @enableeurekaclient:将当前服务注册到eureka中,可以被其他应用发现,然后使用
  11. 运行pconfigserverapplication

  12. 访问:http://localhost:8080/master/p-config-client-dev,dev1.yml 结果如下:

    key-1: value-3
    key-2: value-2
    key-4: value-4
    key-5: value-5

创建客户端

  1. 创建springcloud项目p-config-client

    groupid: com.ms
    artifactid: p-config-client

    Spring Cloud config中,使用数据库存储配置信息

  2. pom.xml内容

    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelversion>4.0.0</modelversion>
        <parent>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-parent</artifactid>
            <version>2.0.4.release</version>
            <relativepath/> <!-- lookup parent from repository -->
        </parent>
        <groupid>com.ms</groupid>
        <artifactid>p-config-client</artifactid>
        <version>0.0.1-snapshot</version>
        <name>p-config-client</name>
        <description>demo project for spring boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>finchley.sr1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-web</artifactid>
            </dependency>
            <dependency>
                <groupid>org.springframework.cloud</groupid>
                <artifactid>spring-cloud-starter-config</artifactid>
            </dependency>
            <dependency>
                <groupid>org.springframework.cloud</groupid>
                <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
            </dependency>
            <dependency>
                <groupid>org.projectlombok</groupid>
                <artifactid>lombok</artifactid>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-test</artifactid>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencymanagement>
            <dependencies>
                <dependency>
                    <groupid>org.springframework.cloud</groupid>
                    <artifactid>spring-cloud-dependencies</artifactid>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencymanagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupid>org.springframework.boot</groupid>
                    <artifactid>spring-boot-maven-plugin</artifactid>
                </plugin>
            </plugins>
        </build>
    
    </project>
  3. application.properties改为bootstrap.yml 内容

    注意:此处必须使用bootstrap名称

    server:
      port: 8081
    spring:
      application:
        name: p-config-client
      cloud:
        config:
          discovery:
            enabled: true
            service-id: p-config-server
          name: ${spring.application.name}
          profile: dev,dev1
          label: master
          override-system-properties: false
    eureka:
      client:
        service-url:
          defaultzone: http://localhost:7001/eureka/
    • config.name对应数据库properties表中的application字段

    • config.profile对应表中的profile字段,config.profile如果包含多个值,之间用英文逗号隔开

    • config.label对应表中的label字段,我们可以使用label来区分环境(dev【开发环境】、test【测试环境】、prod【线上环境】)

  4. pconfigclientapplication内容

    package com.ms;
    
    import org.springframework.boot.springapplication;
    import org.springframework.boot.autoconfigure.springbootapplication;
    import org.springframework.cloud.netflix.eureka.enableeurekaclient;
    
    @springbootapplication
    @enableeurekaclient
    public class pconfigclientapplication {
    
     public static void main(string[] args) {
         springapplication.run(pconfigclientapplication.class, args);
     }
    
    }

    注意:

    • @enableeurekaclient:当前应用会访问eureka注册中心,发现config service
  5. 创建democontroller

    package com.ms;
    
    import lombok.extern.slf4j.slf4j;
    import org.springframework.beans.factory.annotation.autowired;
    import org.springframework.beans.factory.annotation.value;
    import org.springframework.core.env.environment;
    import org.springframework.web.bind.annotation.pathvariable;
    import org.springframework.web.bind.annotation.requestmapping;
    import org.springframework.web.bind.annotation.restcontroller;
    
    import java.util.hashmap;
    import java.util.map;
    
    @restcontroller
    @slf4j
    public class democontroller {
    
        @autowired
        private environment environment;
    
        @requestmapping("/{key}")
        public string index(@pathvariable("key") string key) {
            return this.environment.getproperty(key);
        }
    }
  6. 运行pconfigclientapplication

  7. 访问 http://localhost:8081/key-1 结果如下:

    value-3

总结

  1. 使用数据库存储配置信息还是比较靠谱的,相对于git来说会更好一些
  2. 代码已上传至git,获取源码方式:微信公众号javacode2018,发送:sccj