Spring Cloud Config Server最多見是將配置文件放在本地或者遠程Git倉庫,放在本地是將將全部的配置文件統一寫在Config Server工程目錄下,若是須要修改配置,須要重啓config server;放在Git倉庫,是將配置統一放在Git倉庫,能夠利用Git倉庫的版本控制。本文將介紹使用另一種方式存放配置信息,即將配置存放在Mysql中。html
整個流程:Config Sever暴露Http API接口,Config Client 經過調用Config Sever的Http API接口來讀取配置Config Server的配置信息,Config Server從數據中讀取具體的應用的配置。流程圖以下:java
在本案例中須要由2個工程,分爲config-server和config-client,其中config-server工程須要鏈接Mysql數據庫,讀取配置;config-client則在啓動的時候從config-server工程讀取。本案例Spring Cloud版本爲Greenwich.RELEASE,Spring Boot版本爲2.1.0.RELEASE。mysql
工程 | 描述 |
---|---|
config-server | 端口8769,從數據庫中讀取配置 |
config-client | 端口8083,從config-server讀取配置 |
建立工程config-server,在工程的pom文件引入config-server的起步依賴,mysql的鏈接器,jdbc的起步依賴,代碼以下:git
<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下作如下的配置:github
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語句,該語句的字段必須與數據庫的表字段一致。web
在程序的啓動文件ConfigServerApplication加上@EnableConfigServer註解,開啓ConfigServer的功能,代碼以下:spring
@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數據庫下,執行如下的數據庫腳本:sql
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兩個配置,具體數據庫腳本以下:bootstrap
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工程的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中讀取了配置。
掃一掃,支持下做者吧
(轉載本站文章請註明做者和出處 方誌朋的博客)