javascript
Spring Cloud Config采用数据库存储配置内容【Edgware+】
在之前的《Spring Cloud構(gòu)建微服務(wù)架構(gòu):分布式配置中心》一文中,我們介紹的Spring Cloud Server配置中心采用了Git的方式進(jìn)行配置信息存儲(chǔ)。這一設(shè)計(jì)巧妙的利用Git自身機(jī)制以及其他具有豐富功能的Git服務(wù)端產(chǎn)品,讓Spring Cloud Server在配置存儲(chǔ)和管理的上避開(kāi)了很多與管理相關(guān)的復(fù)雜實(shí)現(xiàn),使其具備了配置中心存儲(chǔ)配置和讀取配置的基本能力;而更上層的管理機(jī)制,由于不具備普遍適用性,所以Spring Cloud Server并沒(méi)有自己去實(shí)現(xiàn)這部分內(nèi)容,而是通過(guò)Git服務(wù)端產(chǎn)品來(lái)提供一部分實(shí)現(xiàn),如果還需要更復(fù)雜的功能也能自己實(shí)現(xiàn)與定義。即便如此,對(duì)于Spring Cloud Server默認(rèn)使用Git來(lái)存儲(chǔ)配置的方案一直以來(lái)還是飽受爭(zhēng)議。所以,本文將介紹一下Spring Cloud Config從Edgware版本開(kāi)始新增的一種配置方式:采用數(shù)據(jù)庫(kù)存儲(chǔ)配置信息。
構(gòu)建配置中心服務(wù)端
第一步:創(chuàng)建一個(gè)基礎(chǔ)的Spring Boot項(xiàng)目,在pom.xml中引入幾個(gè)主要依賴(lài):
- spring-cloud-config-server:配置中心的基礎(chǔ)依賴(lài)
- spring-boot-starter-jdbc:由于需要訪(fǎng)問(wèn)數(shù)據(jù)庫(kù),所以需要加載jdbc的依賴(lài)
- mysql-connector-java:MySQL數(shù)據(jù)庫(kù)的連接包
- flyway-core:該內(nèi)容非強(qiáng)制,主要用來(lái)管理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> |
第二步:準(zhǔn)備schema創(chuàng)建文件。在resources下創(chuàng)建schema目錄,并加入V1__Base_version.sql文件,具體內(nèi)容如下:
| 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; |
該腳本會(huì)在程序運(yùn)行時(shí)由flyway自動(dòng)執(zhí)行
第三步:創(chuàng)建應(yīng)用主類(lèi),具體如下:
public class ConfigServerBootstrap { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class); // 測(cè)試用數(shù)據(jù),僅用于本文測(cè)試使用 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')"); } } |
這里增加了一些測(cè)試用數(shù)據(jù),以便于后續(xù)的配置讀取驗(yàn)證。
第四步:配置application.properties,具體內(nèi)容如下:
| 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 |
這里主要涉及幾個(gè)配置:
- spring.profiles.active=jdbc:必須設(shè)置,將配置中心的存儲(chǔ)實(shí)現(xiàn)切換到j(luò)dbc的方式
- spring.cloud.config.server.jdbc.sql:非必須,這里由于采用mysql數(shù)據(jù)源,key、value是保留關(guān)鍵詞,原生的實(shí)現(xiàn)語(yǔ)句會(huì)報(bào)錯(cuò),所以需要重寫(xiě)一下這句查詢(xún)語(yǔ)句(如果存儲(chǔ)的表結(jié)構(gòu)設(shè)計(jì)不同于上面準(zhǔn)備的內(nèi)容,也可以通過(guò)這個(gè)屬性的配置來(lái)修改配置的獲取邏輯)
- spring.datasource.*:存儲(chǔ)配置信息的數(shù)據(jù)源配置,這里采用mysql,開(kāi)發(fā)者根據(jù)自己實(shí)際情況修改
- flyway.locations:flyway加載schema創(chuàng)建sql的位置
服務(wù)端配置驗(yàn)證
完成了上一節(jié)內(nèi)容之后,我們就已經(jīng)構(gòu)建一個(gè)通過(guò)數(shù)據(jù)酷來(lái)存儲(chǔ)配置內(nèi)容的配置中心了,下面我們可以通過(guò)配置中心暴露的端點(diǎn)來(lái)嘗試讀取配置。
第一步:先將上面構(gòu)建的配置中心啟動(dòng)起來(lái)。
第二步:驗(yàn)證配置信息獲取:
- curl http://localhost:10020/config-client/stage/,獲取信息config-client服務(wù)stage環(huán)境的配置內(nèi)容,根據(jù)上面的數(shù)據(jù)準(zhǔn)備,我們會(huì)獲得如下返回內(nèi)容:
| { "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服務(wù),stage環(huán)境,develop標(biāo)簽的配置內(nèi)容,根據(jù)上面的數(shù)據(jù)準(zhǔn)備,我們會(huì)獲得如下返回內(nèi)容:
| { "name": "hello-service", "profiles": [ "online" ], "label": "develop", "version": null, "state": null, "propertySources": [ { "name": "hello-service-online", "source": { "com.didispace.message": "hello-online-develop" } } ] } |
關(guān)于如何訪(fǎng)問(wèn)Spring Cloud Config構(gòu)建配置中心獲取配置信息的詳細(xì)內(nèi)容
,可以查看前文:《Spring Cloud構(gòu)建微服務(wù)架構(gòu):分布式配置中心》,本文不做詳細(xì)介紹。
總結(jié)
本文主要具體介紹了在Spring Cloud Config在Edgware版本開(kāi)始新增的JDBC存儲(chǔ)的使用思路,具體使用實(shí)際上還有很多可以?xún)?yōu)化的空間,比如:索引的優(yōu)化、查詢(xún)語(yǔ)句的優(yōu)化;如果還需要進(jìn)一步定制管理,對(duì)于表結(jié)構(gòu)的優(yōu)化也是很有必要的。
最后,安利一個(gè)基于Spring Cloud Config的配置管理項(xiàng)目:https://github.com/dyc87112/spring-cloud-config-admin,正在緊鑼密鼓的開(kāi)發(fā)中,盡情期待!
本文示例
讀者可以根據(jù)喜好選擇下面的兩個(gè)倉(cāng)庫(kù)中查看config-server-db和config-client兩個(gè)項(xiàng)目:
- Github:https://github.com/dyc87112/SpringCloud-Learning/
- Gitee:https://gitee.com/didispace/SpringCloud-Learning/
如果您對(duì)這些感興趣,歡迎star、follow、收藏、轉(zhuǎn)發(fā)給予支持!
總結(jié)
以上是生活随笔為你收集整理的Spring Cloud Config采用数据库存储配置内容【Edgware+】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微服务系列:Dubbo与SpringCl
- 下一篇: Spring Cloud Config的