在分佈式系統中,服務數量會很多,如果要修改服務的配置文件,會很麻煩,這個時候,我們想把配置文件放在一個地方統一管理,實時更新,Spring Cloud 就給我們提供了這樣一個組件——Spring Cloud Config。
簡介
在Spring Cloud中,有分佈式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。
創建ConfigServer
New-projec-spring initializr , next-next-cloud config-server(client端就選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.example</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</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>
添加註解
在工程啓動類上添加@EnableConfigServer開啓配置服務器功能
@EnableConfigServer
配置application.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/shmilyah/cloud-config-samples.git
search-paths: '{application}'
label: master
server:
port: 8888
spring.cloud.config.server.git.uri:配置git倉庫地址
spring.cloud.config.server.git.searchPaths:配置倉庫路徑,這裏的配置會根據傳入的應用名動態查找
由於我這裏配置的 git 倉庫爲公開的,所以不需要配置用戶名和密碼,如果你配置的是私有倉庫,還需要配置用戶名和密碼
spring.cloud.config.server.git.username 和spring.cloud.config.server.git.password
啓動程序訪問 http://localhost:8888/config-client/dev/master
將會顯示
{“name”:“config-client”,“profiles”:[“dev”],“label”:“master”,“version”:“62a1d6b3d883e8378ed961a3a1566adfc48239b6”,“state”:null,“propertySources”:[{“name”:“https://github.com/shmilyah/cloud-config-samples.git/config-client/config-client-dev.yaml",“source”:{“hello”:"world”}}]}
http請求地址和資源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
創建ConfigClient
按照之前的方法,選擇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.example</groupId>
<artifactId>configclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configclient</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>Dalston.RC1</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: 8989
spring:
application:
name: con-client
cloud:
config:
uri: http://localhost:8888/
profile: dev
label: master
spring.cloud.config.profile: dev開發環境配置文件 test測試環境 pro正式環境
寫一個api接口,返回讀取的配置中心的值
@SpringBootApplication
@RestController
public class ConfigclientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
@Value("${hello}")
private String hello;
@RequestMapping("/hh")
public String hh(){
return hello;
}
}
啓動程序訪問 http://localhost:8989/hh
將會顯示:
world
真的十分十分的簡單!照着做即可。希望大家好好的去學習。
參考文章 https://blog.csdn.net/hubo_88/article/details/80692156
https://blog.csdn.net/forezp/article/details/81041028