Spring Cloud config中,使用數據庫存儲配置信息

主要內容

  1. 在springcloud config中,使用數據庫存儲配置信息。java

    系統默認採用git的方式,此處咱們介紹使用jdbc的方式存儲配置信息mysql

準備數據庫

  1. 數據庫咱們使用mysql。
  2. 新建庫 p-config-server
  3. 建立配置須要的表,並初始化一些配置信息git

    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-eurekaweb

    groupId: com.ms
    artifactId: p-eureka

    在這裏插入圖片描述

  2. pom.xml內容spring

    <?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.ymlsql

    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);
     }
    
    }

    注意:apache

    • @EnableEurekaServer:表示這是一個eureka註冊中心
  5. 運行PEurekaApplicationbootstrap

  6. 訪問http://localhost:7001/微信

    在這裏插入圖片描述

    建立配置中心(config server)

  7. 建立springcloud項目p-config-server

    groupId: com.ms
    artifactId: p-config-server

    在這裏插入圖片描述

  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

    在這裏插入圖片描述

  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
相關文章
相關標籤/搜索