天天看點

Spring Cloud Config采用資料庫存儲配置内容【Edgware+】

在之前的 《Spring Cloud建構微服務架構:分布式配置中心》 一文中,我們介紹的Spring Cloud Server配置中心采用了Git的方式進行配置資訊存儲。這一設計巧妙的利用Git自身機制以及其他具有豐富功能的Git服務端産品,讓Spring Cloud Server在配置存儲和管理的上避開了很多與管理相關的複雜實作,使其具備了配置中心存儲配置和讀取配置的基本能力;而更上層的管理機制,由于不具備普遍适用性,是以Spring Cloud Server并沒有自己去實作這部分内容,而是通過Git服務端産品來提供一部分實作,如果還需要更複雜的功能也能自己實作與定義。即便如此,對于Spring Cloud Server預設使用Git來存儲配置的方案一直以來還是飽受争議。是以,本文将介紹一下Spring Cloud Config從Edgware版本開始新增的一種配置方式:采用資料庫存儲配置資訊。

https://blog.didispace.com/spring-cloud-starter-edgware-3-1/#%E6%9E%84%E5%BB%BA%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E6%9C%8D%E5%8A%A1%E7%AB%AF 建構配置中心服務端

第一步:建立一個基礎的Spring Boot項目,在pom.xml中引入幾個主要依賴:

  • spring-cloud-config-server

    :配置中心的基礎依賴
  • spring-boot-starter-jdbc

    :由于需要通路資料庫,是以需要加載jdbc的依賴
  • mysql-connector-java

    :MySQL資料庫的連接配接包
  • flyway-core

    :該内容非強制,主要用來管理schema(如果您不了解可以看一下 這篇文章
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.11.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.0.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>      

第二步:準備schema建立檔案。在

resources

下建立

schema

目錄,并加入

V1__Base_version.sql

檔案,具體内容如下:

CREATE TABLE `properties` (
  `id` int(11) NOT NULL,
  `key` varchar(50) NOT NULL,
  `value` varchar(500) NOT NULL,
  `application` varchar(50) NOT NULL,
  `profile` varchar(50) NOT NULL,
  `label` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;      

該腳本會在程式運作時由flyway自動執行

第三步:建立應用主類,具體如下:

@EnableConfigServer
@SpringBootApplication
public class ConfigServerBootstrap {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class);

        // 測試用資料,僅用于本文測試使用
        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        jdbcTemplate.execute("delete from properties");
        jdbcTemplate.execute("INSERT INTO properties VALUES(1, 'com.didispace.message', 'test-stage-master', 'config-client', 'stage', 'master')");
        jdbcTemplate.execute("INSERT INTO properties VALUES(2, 'com.didispace.message', 'test-online-master', 'config-client', 'online', 'master')");
        jdbcTemplate.execute("INSERT INTO properties VALUES(3, 'com.didispace.message', 'test-online-develop', 'config-client', 'online', 'develop')");
        jdbcTemplate.execute("INSERT INTO properties VALUES(4, 'com.didispace.message', 'hello-online-master', 'hello-service', 'online', 'master')");
        jdbcTemplate.execute("INSERT INTO properties VALUES(5, 'com.didispace.message', 'hello-online-develop', 'hello-service', 'online', 'develop')");
    }

}      

這裡增加了一些測試用資料,以便于後續的配置讀取驗證。

第四步:配置

application.properties

,具體内容如下:

pring.application.name=config-server-db
server.port=10020

spring.profiles.active=jdbc

spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?

spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

flyway.locations=/schema      

這裡主要涉及幾個配置:

  • spring.profiles.active=jdbc

    :必須設定,将配置中心的存儲實作切換到jdbc的方式
  • spring.cloud.config.server.jdbc.sql

    :非必須,這裡由于采用mysql資料源,

    key

    value

    是保留關鍵詞,原生的實作語句會報錯,是以需要重寫一下這句查詢語句(如果存儲的表結構設計不同于上面準備的内容,也可以通過這個屬性的配置來修改配置的擷取邏輯)
  • spring.datasource.*

    :存儲配置資訊的資料源配置,這裡采用mysql,開發者根據自己實際情況修改
  • flyway.locations

    :flyway加載schema建立sql的位置

https://blog.didispace.com/spring-cloud-starter-edgware-3-1/#%E6%9C%8D%E5%8A%A1%E7%AB%AF%E9%85%8D%E7%BD%AE%E9%AA%8C%E8%AF%81 服務端配置驗證

完成了上一節内容之後,我們就已經建構一個通過資料酷來存儲配置内容的配置中心了,下面我們可以通過配置中心暴露的端點來嘗試讀取配置。

第一步:先将上面建構的配置中心啟動起來。

第二步:驗證配置資訊擷取:

  • curl http://localhost:10020/config-client/stage/

    ,擷取資訊

    config-client

    服務

    stage

    環境的配置内容,根據上面的資料準備,我們會獲得如下傳回内容:
{
    "name": "config-client",
    "profiles": [
    "stage"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "config-client-stage",
            "source": {
            "com.didispace.message": "test-stage-master"
            }
        }
    ]
}      
  • curl http://localhost:10020/hello-service/stage/develop

    hello-service

    服務,

    stage

    環境,

    develop

    标簽的配置内容,根據上面的資料準備,我們會獲得如下傳回内容:
"name": "hello-service",
    "profiles": [
        "online"
    ],
    "label": "develop",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "hello-service-online",
            "source": {
                "com.didispace.message": "hello-online-develop"
            }
        }
    ]
}      

關于如何通路Spring Cloud Config建構配置中心擷取配置資訊的詳細内容

,可以檢視前文:

,本文不做詳細介紹。

https://blog.didispace.com/spring-cloud-starter-edgware-3-1/#%E6%80%BB%E7%BB%93 總結

本文主要具體介紹了在Spring Cloud Config在Edgware版本開始新增的JDBC存儲的使用思路,具體使用實際上還有很多可以優化的空間,比如:索引的優化、查詢語句的優化;如果還需要進一步定制管理,對于表結構的優化也是很有必要的。

最後,安利一個基于Spring Cloud Config的配置管理項目:

https://github.com/dyc87112/spring-cloud-config-admin

,正在緊鑼密鼓的開發中,盡情期待!

https://blog.didispace.com/spring-cloud-starter-edgware-3-1/#%E6%9C%AC%E6%96%87%E7%A4%BA%E4%BE%8B 本文示例

讀者可以根據喜好選擇下面的兩個倉庫中檢視

config-server-db

config-client

兩個項目: