demo代碼地址:https://download.csdn.net/download/qq_33333654/12014918
環境:
jdk1.8
maven3.0
idea
springboot1.5.3
首先需要啓動自己的註冊中心,具體可參考我之前的註冊中心高可用博客:
https://blog.csdn.net/qq_33333654/article/details/102636388
在我們瞭解spring cloud config之前,我可以想想一個配置中心提供的核心功能應該有什麼
- 提供服務端和客戶端支持
- 集中管理各環境的配置文件
- 配置文件修改之後,可以快速的生效
- 可以進行版本管理
- 支持大的併發查詢
- 支持各種語言
Spring Cloud Config可以完美的支持以上所有的需求。
本片文章中的項目使用的是本地模式創建服務端,也會講解下使用其他的模式。
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client通過接口獲取數據、並依據此數據初始化自己的應用。Spring cloud使用git或svn存放配置文件,默認情況下使用git。真實項目中還是建議使用git
接下來創建一個普通的springboot項目,這裏我使用了1.5.3的版本,如果使用2.0+的時候,配置文件註解會找不到,具體原因後續會研究,喜歡的人也可以自行研究。
項目創建完之後修改Pom文件,這是我的代碼,可參考:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-config-server</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意三個部分:
註冊中心依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
springcloud環境
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
以及本篇文章必不可少的maven依賴,也是springCloudConfig的核心依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
接下來是在啓動類上添加註解:
@EnableConfigServer
然後修改配置文件:
server: port: 9020 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: spring-cloud-config-server profiles: active: native
講解:
9020是端口好,eureka下是我的註冊中心,spring-cloud-config-server是項目名稱也是註冊中心中的名稱,
profiles:
active: native
這個就比較特殊了,這個是代表config掃描的設置,這個是設置掃描項目本地路徑,也就是resources文件夾下的配置文件。
當然還有其他兩種。
Spring Cloud Config提供本地存儲配置的方式。我們只需要設置屬性spring.profiles.active=native
,Config Server會默認從應用的src/main/resource
目錄下檢索配置文件。也可以通過spring.cloud.config.server.native.searchLocations=file:E:/properties/
屬性來指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,但是爲了支持更好的管理內容和版本控制的功能,還是推薦使用git的方式。
git配置的例子:
server:
port: 8001
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git倉庫的地址
search-paths: config-repo # git倉庫地址下的相對地址,可以配置多個,用,分割。
username: # git倉庫的賬號
password: # git倉庫的密碼
補充下,還有一個lable屬性,這個屬性可以設置git的分支
絕對路徑的配置例子:
#配置中心端口
server.port=8887spring.application.name=config-server
logging.config=classpath:log4j2.xml
spring.profiles.active=native
#申明本地配置文件的存放位置
spring.cloud.config.server.native.searchLocations=file:D:\\etc\\native
#註冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
這個是網上找的,設置絕對路徑的時候,可以在D盤創建多個文件,如圖:
三種模式都講解了。言歸正傳,我這次試用native.
Pom文件修改完之後,我們在resources文件夾下創建一個配置文件名字爲:native-mysql.properties
配置文件內容爲: native.hello=hello_i_im_mysql
接下啓動項目。
訪問路徑:
http://localhost:9020/native/mysql
輸出內容如下:
{
"name": "native",
"profiles": ["mysql"],
"label": null,
"version": null,
"state": null,
"propertySources": [{
"name": "classpath:/native-mysql.properties",
"source": {
"native.hello": "hello_i_im_mysql"
}
}]
}
配圖:
從返回的json文件上我們應該就能看出,本次訪問的是native空間下mysql文件,配置文件路徑爲本地路徑classpath,配置文件的內容就是souorce的內容。
同樣的道理,如果你創建一個native-test.properties文件,再次訪問:
http://localhost:9020/native/test 觀察下效果。
由圖中我們可看出,裏面所有的文件的命名規則都遵循:{application}-{profile}.properties
所以我們客戶端bootstrap.properties文件中的spring.cloud.config.name=native參數,實際上是爲了匹配到這些配置文件的{application}前綴
那麼其他兩種方式呢?是否一樣呢? 建議親自試一下,這裏簡單說明下規則:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
這個就是對應的config訪問的規則。
到此,springCloudConfig的服務端創建完成