Spring Cloud Config支持多種配置方式,包括本地文件系統,git等等。這次用git的方式。
git地址:https://github.com/chechengjiang/configDemo/tree/master/config
這裏有三個配置文件,其中config-other-pj代表的是一個工程,config-single-client代表的是一個工程。
兩個工程:config-other-pj,config-single-client
-dev表示開發環境,-prod表示正式環境。
這裏的文件名是Spring Cloud Config要求的標準文件命名,工程名——環境.yml。如果不是這種命名方式,則找不到文件,稍後解釋。
Config中心,其實就是建立一個以Spring Boot爲基礎的cloud服務端,所有要獲取配置的客戶端從他這裏獲取。
下面開始分別建立服務端和客戶端:
Config 服務端:
pom.xml:
<?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>
<!--Spring Boot項目必須 注意版本和Maven的關係,有可能導致不兼容-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--Spring Cloud 配置必須-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--boot項目必須-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web項目必須-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 服務端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--Maven項目必須-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
按照這個配置,就生成了一個基本的cloud服務端。也是boot項目的基本配置。這樣就可以運行了。
其中服務端專屬:
<!-- spring cloud config 服務端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
在resource文件夾下有兩個配置文件:
bootstrap.yml
spring:
application:
name:config-single-client,config-other-pj
cloud:
config:
server:
git:
uri: https://github.com/chechengjiang/configDemo
username: [email protected]
password: xxxxxxxxxxx
default-label: master
search-paths: config
功能就是連接git。注意,application.name下有兩個工程,分別是config-single-client和config-other-pj,也就是說可以把很多個工程的配置文件放在git,不同的工程通過指定的application.name來獲取不同的配置。這裏的application.name是爲了讓client找到用的,對於服務端來說不影響從git上面獲取配置信息。也就是說,即使去掉application.name,服務端依然可以從git獲取配置信息,但是client就找不到對應的配置了。而在client的bootstrap.yml文件裏,application.name必須與服務端的application.name匹配,並且要和git上的yml文件名匹配。
applicaiton.yml
server:
port: 8070
服務端的端口號。
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
啓動類的話需要加上@EnableConfigServer的註解,表示這是一個服務端。
測試服務端是否好用,啓動之後按照config的約定規則訪問:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
直接在瀏覽器訪問:http://localhost:8070/config-single-client/dev/master
上面說到,如果不採用工程名——環境名.yml的方式命名,那麼在這裏就獲取不到信息,只能爲{}。
Client客戶端:
pom.xml:
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 客戶端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- @Data 不用寫get set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency><!--頁面模板依賴-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中客戶端專屬:
<!-- spring cloud config 客戶端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring cloud config 自動刷新配置文件更新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml:
spring:
profiles:
active: dev
---
spring:
profiles: prod
application:
name: config-single-client
cloud:
config:
uri: http://localhost:8070
label: master
profile: prod
---
spring:
profiles: dev
application:
name: config-other-pj
cloud:
config:
uri: http://localhost:8070
label: master
profile: dev
profiles.active代表當前的環境,spring.application.name表示要從config獲取的工程,spring.profiles則爲環境,uri爲config地址。
application.yml:
server:
port: 8080
management:
endpoint:
shutdown:
enabled: false
endpoints:
web:
exposure:
include: "*"
data:
env: NaN
user:
username: NaN
password: NaN
其中 management 是關於 actuator 相關的,接下來自動刷新配置的時候需要使用。
data 部分是當無法讀取配置中心的配置時,使用此配置,以免項目無法啓動。
創建Java類接受配置信息:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
@Data
@Component
public class GitConfig {
@Value("${data.env}")
private String env;
@Value("${data.user.username}")
private String username;
@Value("${data.user.password}")
private String password;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
測試類:
package com.example.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class testMain {
@Autowired
private GitConfig gitConfig;
@RequestMapping(value = "show")
@ResponseBody
public Object show(){
return gitConfig;
}
}
如果GitConfig類省略了get,set方法,那麼返回到頁面的json數據爲{}。
配置正確後啓動:
相關連接: