1、Config簡介
當項目越來越多,每個項目又要去維護多個配置文件,而且每個配置文件還要區分各個環境(開發、測試、預發佈、線上等),想想都頭大,不過市面上已經存在很多好用的開源的配置中心,如百度的disconf,攜程的Apollo等,這些開源的軟件確實都很棒,但是SpringCloud Config確更加輕量級,功能也強大,可以無縫的和spring體系相結合,簡單易用。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client通過接口獲取數據、並依據此數據初始化自己的應用。
在使用springcloud config之前我們可以想想一個配置中心應該提供哪些核心功能呢?
- 提供服務端和客戶端支持
- 集中管理各環境的配置文件
- 配置文件修改之後,可以快速的生效
- 可以進行版本管理
- 支持大的併發查詢
- 支持各種語言
2、創建配置文件
本文的配置文件從git拉取,我們再github上新增了倉庫config-server用於放配置文件,我這裏建了三個配置文件:https://github.com/xujiangdong/config-server,每個配置文件裏面的key都一樣,只是value不一樣
3、配置中心Server端
另起個項目叫confif-server,引入核心依賴spring-cloud-config-server
<?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>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-netflix-eureka-client</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
配置文件application.yml
這裏將配置中心服務端註冊到註冊中心,客戶端通過註冊中心獲取配置中心服務端,當配置中心服務端集羣部署的時候可以保證服務端的高可用
- uri:你的github地址
- search-paths:你的倉庫名稱,多個用逗號隔開,可以不配置,加在uri後面即可,如:https://github.com/xujiangdong/config-server
- username\password:公開庫就不需要配置了,私有庫需要
- basedir:配置文件下取下來後存儲的本地地址,可以不配置
- label:對應的分支,可以不配置,默認master
server:
port: 8006
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/xujiangdong/
search-paths: config-server
username:
password:
basedir: G:\test
label: master
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8000/eureka
啓動類上開啓註冊中心(@EnableConfigServer),並將該實例註冊到註冊中心
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4、啓動項目
註冊中心還是用的第一篇文章的那個項目,啓動後訪問:http://localhost:8006/config-tst.properties,後綴還可以用yml或json,以不同的格式打印,如:http://localhost:8006/config-tst.yml
5、配置中心Client端
新建一個項目config-client1,並引入核心依賴spring-cloud-config-client
<?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>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
新建配置文件bootstrap.yml,爲啥不用application.yml?因爲bootstrap.yml的加載順序在application.yml之前,這樣可以保證配置文件在項目編譯前被拉取下來,這裏通過註冊中心獲取配置中心服務端
- service-id:需要訪問的實例名稱
- name:需要讀取的配置文件名
- profile:對應的環境
server:
port: 8007
spring:
application:
name: config-client1
cloud:
config:
discovery:
enabled: true
service-id: CONFIG-SERVER
profile: tst
name: config-tst
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8000/eureka/
啓動類
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClient1Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient1Application.class, args);
}
}
新建一個測試接口測試能否正常獲取配置文件
/**
* @author XuJD
* @create 2019-11-28 17:21
**/
@RestController
public class TestController {
@Value("${db.url}")
private String url;
@RequestMapping("getConfig")
public String getConfig(){
return url;
}
}
訪問:http://localhost:8007/getConfig,可以看到已經拿到配置項了