Spring Cloud Config採用數據庫存儲配置內容

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

構建配置中心服務端

第一步:創建一個基礎的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,具體內容如下:

spring.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數據源,keyvalue是保留關鍵詞,原生的實現語句會報錯,所以需要重寫一下這句查詢語句(如果存儲的表結構設計不同於上面準備的內容,也可以通過這個屬性的配置來修改配置的獲取邏輯)
  • spring.datasource.*:存儲配置信息的數據源配置,這裏採用mysql,開發者根據自己實際情況修改
  • flyway.locations:flyway加載schema創建sql的位置

服務端配置驗證

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

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

第二步:驗證配置信息獲取:

  • 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構建配置中心獲取配置信息的詳細內容
,可以查看前文:《Spring Cloud構建微服務架構:分佈式配置中心》,本文不做詳細介紹。

總結

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

最後,安利一個基於Spring Cloud Config的配置管理項目:https://github.com/dyc87112/spring-cloud-config-admin,正在緊鑼密鼓的開發中,盡情期待!

本文示例

讀者可以根據喜好選擇下面的兩個倉庫中查看config-server-dbconfig-client兩個項目:

如果您對這些感興趣,歡迎star、follow、收藏、轉發給予支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章