spring cloud config將配置儲存在資料庫中

方誌朋發表於2019-02-21

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中讀取了配置。

參考資料

cloud.spring.io/spring-clou…

原始碼下載

github.com/forezp/Spri…

spring cloud config將配置儲存在資料庫中
掃一掃,支援下作者吧

(轉載本站文章請註明作者和出處 方誌朋的部落格

相關文章