spring cloud config將配置存儲在數據庫中

轉載請標明出處:
https://blog.csdn.net/forezp/...
本文出自方誌朋的博客

Spring Cloud Config Server最常見是將配置文件放在本地或者遠程Git倉庫,放在本地是將將所有的配置文件統一寫在Config Server工程目錄下,如果需要修改配置,需要重啓config server;放在Git倉庫,是將配置統一放在Git倉庫,可以利用Git倉庫的版本控制。本文將介紹使用另外一種方式存放配置信息,即將配置存放在Mysql中。

整個流程:Config Sever暴露Http API接口,Config Client 通過調用Config Sever的Http API接口來讀取配置Config Server的配置信息,Config Server從數據中讀取具體的應用的配置。流程圖如下:

61.png

案例實戰

在本案例中需要由2個工程,分爲config-server和config-client,其中config-server工程需要連接Mysql數據庫,讀取配置;config-client則在啓動的時候從config-server工程讀取。本案例Spring Cloud版本爲Greenwich.RELEASE,Spring Boot版本爲2.1.0.RELEASE。

工程 描述
config-server 端口8769,從數據庫中讀取配置
config-client 端口8083,從config-server讀取配置

搭建config-server工程

創建工程config-server,在工程的pom文件引入config-server的起步依賴,mysql的連接器,jdbc的起步依賴,代碼如下:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

在工程的配置文件application.yml下做以下的配置:

spring:
  profiles:
     active: jdbc
  application:
     name: config-jdbc-server
  datasource:
     url: jdbc:mysql://127.0.0.1:3306/config-jdbc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
     username: root
     password: 123456
     driver-class-name: com.mysql.jdbc.Driver
  cloud:
     config:
       label: master
       server:
         jdbc: true
server:
  port: 8769
spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?

其中,spring.profiles.active爲spring讀取的配置文件名,從數據庫中讀取,必須爲jdbc。spring.datasource配置了數據庫相關的信息,spring.cloud.config.label讀取的配置的分支,這個需要在數據庫中數據對應。spring.cloud.config.server.jdbc.sql爲查詢數據庫的sql語句,該語句的字段必須與數據庫的表字段一致。

在程序的啓動文件ConfigServerApplication加上@EnableConfigServer註解,開啓ConfigServer的功能,代碼如下:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

初始化數據庫

由於Config-server需要從數據庫中讀取,所以讀者需要先安裝MySQL數據庫,安裝成功後,創建config-jdbc數據庫,數據庫編碼爲utf-8,然後在config-jdbc數據庫下,執行以下的數據庫腳本:

CREATE TABLE `config_properties` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `key1` varchar(50) COLLATE utf8_bin NOT NULL,
  `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,
  `application` varchar(50) COLLATE utf8_bin NOT NULL,
  `profile` varchar(50) COLLATE utf8_bin NOT NULL,
  `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

其中key1字段爲配置的key,value1字段爲配置的值,application字段對應於應用名,profile對應於環境,label對應於讀取的分支,一般爲master。

插入數據config-client 的2條數據,包括server.port和foo兩個配置,具體數據庫腳本如下:


insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');
insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');

搭建config-client

在 config-client工程的pom文件,引入web和config的起步依賴,代碼如下:

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

在程序的啓動配置文件 bootstrap.yml做程序的相關配置,一定要是bootstrap.yml,不可以是application.yml,bootstrap.yml的讀取優先級更高,配置如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8769
      fail-fast: true
  profiles:
    active: dev

其中spring.cloud.config.uri配置的config-server的地址,spring.cloud.config.fail-fast配置的是讀取配置失敗後,執行快速失敗。spring.profiles.active配置的是spring讀取配置文件的環境。

在程序的啓動文件ConfigClientApplication,寫一個RestAPI,讀取配置文件的foo配置,返回給瀏覽器,代碼如下:

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/foo")
    public String hi(){
        return foo;
    }
}

依次啓動2個工程,其中config-client的啓動端口爲8083,這個是在數據庫中的,可見config-client從 config-server中讀取了配置。在瀏覽器上訪問http://localhost:8083/foo,瀏覽器顯示bar-jdbc,這個是在數據庫中的,可見config-client從 config-server中讀取了配置。

參考資料

https://cloud.spring.io/sprin...

源碼下載

https://github.com/forezp/Spr...

<div>

<p align="center">
    <img src="https://www.fangzhipeng.com/img/avatar.jpg" width="258" height="258"/>
    <br>
    掃一掃,支持下作者吧
</p>
<p align="center" style="margin-top: 15px; font-size: 11px;color: #cc0000;">
    <strong>(轉載本站文章請註明作者和出處 <a href="https://www.fangzhipeng.com">方誌朋的博客</a>)</strong>
</p>

</div>

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