三、spring cloud config 配置中心
1、 當一個系統中的配置檔案發生改變的時候,經常的做法是重新啟動該服務,才能使得新的配置檔案生效,spring cloud config可以實現微服務中的所有系統的配置檔案的統一管理,而且還可以實現當配置檔案發生變化的時候,系統會自動更新獲取新的配置。
將配置檔案放入git或者svn等服務中,通過一個Config Server服務來獲取git或者svn中的配置資料,二其他服務需要配置資料時在通過Config Client從Config Server獲取。
2、 在git倉庫新建如下圖目錄
具體內容檢視:https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos
3、 新建maven專案sc-config-server,對應pom.xml
<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>spring-cloud</groupId> <artifactId>sc-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-config-server</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.0.1.RELEASE</version> </dependency> </dependencies>
4、 新建類ConfigServerApplication.java
package sc.config.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
5、 建立bootstrap.yml檔案
#服務埠
server:
port: 8100
#服務註冊中心
eureka:
client:
registerWithEureka: true #是否將自己註冊到Eureka服務中,預設為true
fetchRegistry: true #是否從Eureka中獲取註冊資訊,預設為true
serviceUrl:
defaultZone: http://localhost:5001/eureka/
instance:
prefer-ip-address: true #將自己的ip地址註冊到Eureka服務中
ipAddress: 127.0.0.1
spring:
application:
name: sc-config-server #服務名稱
cloud:
config:
label: master #配置檔案所在的分支
server:
git:
uri: https://gitee.com/hjj520/spring-cloud-2.x.git #服務的git倉庫地址
#git倉庫的使用者名稱
#username: huangjinjin
#git倉庫的密碼
#password: ********
search-paths: /config-repos/sc-consumer-config #配置檔案所在的目錄
備註:search-paths可以使用佔位符{application},不過需要注意的必須使用這樣的方式:’{application}’ (單引號引起來),不然可能出現https://blog.csdn.net/weixin_35022258/article/details/79019033帖子說的問題,具體這個佔位符以後會說到。
6、 啟動註冊中心Eureka,然後在啟動sc-config-server專案
http請求地址和資原始檔對映如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
具體可以使用哪種http請求地址和資原始檔對映可以在config server的日誌可以看到
7、 驗證獲取倉庫中的配置資料
http://127.0.0.1:8100/application/dev
http://127.0.0.1:8100/application/prd
更多有用技術文章關注