Spring Cloud Config 簡(jiǎn)介
什么是Srping Cloud Config?
- Spring Cloud Config 是一種分布式配置中心框架敲茄, 為分布式系統(tǒng)中的外部化配置提供服務(wù)器和客戶(hù)端支持。(同類(lèi)技術(shù)還有vault讨勤,zookeeper订框,Consul)
- 使用Config Server析苫,可以在所有環(huán)境中管理應(yīng)用程序的外部屬性〈┌猓客戶(hù)端和服務(wù)器上的概念映射與Spring Environment和PropertySource抽象衩侥,因此它們非常適合Spring應(yīng)用程序,但可以與任何語(yǔ)言運(yùn)行的任何應(yīng)用程序一起使用矛物。當(dāng)應(yīng)用程序通過(guò)部署管道從開(kāi)發(fā)到測(cè)試并進(jìn)入生產(chǎn)時(shí)茫死,您可以管理這些環(huán)境之間的配置,并確保應(yīng)用程序具有遷移時(shí)需要運(yùn)行的所有內(nèi)容履羞。服務(wù)器存儲(chǔ)后端的默認(rèn)實(shí)現(xiàn)使用git峦萎,因此它可以輕松支持配置環(huán)境的標(biāo)記版本屡久,以及可用于管理內(nèi)容的各種工具。
- Spring Cloud Config也主要由兩部分組成:
- Config-Server: 用于配置外部的資源文件,支持對(duì)屬性值進(jìn)行加解密
- Config-client:綁定到config server使用遠(yuǎn)程配置文件初始化spring爱榔,支持對(duì)屬性值進(jìn)行加解密
本文示例說(shuō)明
- 為了更直觀(guān)的理解spring cloud config涂身,本文通過(guò)server獲取整個(gè)配置,效果上與通過(guò)配置springboot的active 來(lái)切換環(huán)境一致搓蚪;通過(guò)接口查詢(xún)數(shù)據(jù)庫(kù)以查看效果蛤售;
- 采用高可用架構(gòu);此處使用eureka去做服務(wù)注冊(cè)發(fā)現(xiàn)(暫時(shí)也只會(huì)這個(gè)妒潭。悴能。),eureka配置說(shuō)明可以查看springCloud之eureka
新建ConfigServer
為了減少文章長(zhǎng)度雳灾,盡量干貨漠酿,創(chuàng)建過(guò)程就不截說(shuō)明了,建議使用Intellij idea進(jìn)行創(chuàng)建谎亩;
- 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lc.springcloud</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>Greenwich.M2</spring-cloud.version>
</properties>
<dependencies>
<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>
</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- application.yml配置如
server:
port: 8100
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/lvchaogit/SpringCloud
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/ # 服務(wù)注冊(cè)中心地址
通過(guò)配置不難理解炒嘲,該配置是從git上獲取配置,更多配置后續(xù)詳解匈庭;
- application.java
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
主要是加上@EnableConfigServer注解夫凸,開(kāi)啟configserver功能
搭建 Config Client
- 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lc.springcloud</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>Greenwich.M2</spring-cloud.version>
</properties>
<dependencies>
<!-- config starter begin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- config starter end-->
<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-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.1.9</version>
<exclusions>
<exclusion>
<artifactId>tomcat-jdbc</artifactId>
<groupId>org.apache.tomcat</groupId>
</exclusion>
</exclusions>
</dependency>
<!--config監(jiān)控模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- bootstrap.yml
spring:
application:
name: config-client
cloud:
config:
profile: dev
label: master
discovery:
enabled: true
service-id: config-server
server:
port: 2001
eureka:
client:
register-with-eureka: false #因此處只是消費(fèi),不提供服務(wù)阱持,所以不需要向eureka server注冊(cè)
service-url:
defaultZone: http://localhost:8081/eureka/ # 服務(wù)注冊(cè)中心地址
- 首先注意配置文件名稱(chēng)為:bootstartp.yml夭拌,并不是
application.yml - 通過(guò)配置discovery,并設(shè)置enabled為true衷咽,使client通過(guò)服務(wù)發(fā)現(xiàn)去獲取server鸽扁,server-id為注冊(cè)中心里配置的服務(wù)名稱(chēng)
- label=git的標(biāo)簽;profile=配置文件版本(類(lèi)似于spring boot中的active)
常用configserver配置
采用URI占位符
spring:
cloud:
config:
server:
git:
uri: https://github.com/lvchaogit/{application}
- 使用{application} 和 {profile}(如果使用{label}镶骗,請(qǐng)記住它是使用在git標(biāo)簽中的)桶现。因此你可以輕松的支持“一個(gè)應(yīng)用一個(gè)倉(cāng)庫(kù)”的原則
模式匹配和多倉(cāng)庫(kù)
spring:
cloud:
config:
server:
git:
uri: https://github.com/lvchaogit/SpringCloud
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
- 在{application}和{profile}參數(shù)中使用模式匹配可以支持更多復(fù)雜的需求。模式的格式是一組逗號(hào)分隔的{application}/{profile}鼎姊,其中的參數(shù)可以使用通配符.
- 如果{application}/{profile}沒(méi)有匹配到任何模式骡和,它將使用默認(rèn)的倉(cāng)庫(kù)地址:spring.cloud.config.server.git.uri。上面的例子中此蜈,"simple"倉(cāng)庫(kù)匹配的是“simple/”(它僅僅匹配一個(gè)倉(cāng)庫(kù)simple即横,在所有的環(huán)境下)。"local"倉(cāng)庫(kù)將匹配所有{application}的名字以“l(fā)ocal”開(kāi)頭的裆赵,并且也是在所有的環(huán)境下东囚。“/”前綴自動(dòng)添加到所有沒(méi)有設(shè)置{profile}的模式中战授。
匹配倉(cāng)庫(kù)子目錄
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
searchPaths: foo,bar*
- 在foo和以bar開(kāi)頭的目錄中页藻,搜索配置文件桨嫁。
克隆遠(yuǎn)程的倉(cāng)庫(kù)
spring:
cloud:
config:
server:
git:
uri: https://git/common/config-repo.git
repos:
team-a:
pattern: team-a-*
cloneOnStart: true
uri: http://git/team-a/config-repo.git
team-b:
pattern: team-b-*
cloneOnStart: false
uri: http://git/team-b/config-repo.git
team-c:
pattern: team-c-*
uri: http://git/team-a/config-repo.git
- 服務(wù)器默認(rèn)在第一次請(qǐng)求配置文件時(shí)克隆遠(yuǎn)程的倉(cāng)庫(kù),也可以配置在啟動(dòng)的時(shí)候克隆倉(cāng)庫(kù)
- team-a的倉(cāng)庫(kù)將在服務(wù)端啟動(dòng)時(shí)進(jìn)行克隆份帐,其他的倉(cāng)庫(kù)將在第一次請(qǐng)求時(shí)克隆璃吧。
倉(cāng)庫(kù)認(rèn)證
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
username: trolley
password: strongpassword
- 輸入用戶(hù)名密碼
常用配置參考:configServer常用配置
文中示例代碼:SpringCloudConfig 示例