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


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/spring-cloud-config/single/spring-cloud-config.html#_jdbc_backend

源碼下載

https://github.com/forezp/SpringCloudLearning/tree/master/chapter10-5-jdbc


掃一掃,支持下作者吧

(轉載本站文章請注明作者和出處 方志朋的博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM