chap08-Spring Cloud Config -1- 快速入門

Spring Cloud Config

Spring Cloud Config 是Spring Cloud團隊創建的一個全新項目,用來爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分爲服務端和客戶端兩個部分。

其中服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫併爲科幻段提供獲取配置信息、加密/解密信息等訪問接口;

客戶端則是微服務架構中的各個微服務應用或基礎設置,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。

Spring Cloud默認採用Git來存儲配置信息,也支持SVN、本地化文件系統。

快速入門

接下來,將演示如何構建一個基於Git存儲的分佈式配置中心。

構建配置中心:config-server

maven依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

主類上添加 @EnableConfigServer 註解

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
}

application.yaml配置信息

spring:
  cloud:
    config:
      server:
        git:
          ## 遠程git地址
          uri: https://gitee.com/it_freshman/spring-cloud-learning.git

          ### 配置倉庫 相對搜索路徑,可以配置多個
          search-paths: /config-repo/
          username: ###用戶名
          password: ###密碼

git上添加配置文件

${uri}/${search-paths}下添加配置針對4種不同環境的 配置文件:

  • orders.properties: 配置內容爲from=git-default-1.0
  • orders-dev.properties: 配置內容爲from=git-dev-1.0
  • orders-test.properties: 配置內容爲from=git-test-1.0
  • orders-prod.properties: 配置內容爲from=git-prod-1.0
    在這裏插入圖片描述
    同時增加一個分支v2.0,將裏面的內容改爲git-*-2.0.

在這裏插入圖片描述

完成上述的配置之後,可以通過URL訪問查看配置文件映射信息,有如下幾種方式:

  • /{application}/{profile}/{label}
  • /{application}-{profile}.[yml | properties]
  • /{label}/{application}-{profile}.[yml | properties]

通過http://localhost:7001/orders/test/v2.0訪問,獲取配置信息:

{	
 
   "label" : "v2.0",     # label
   "name" : "orders",    # application
   "profiles" : [ "test" ], # profile
   "propertySources" : [
      {
         "name" : "https://gitee.com/it_freshman/spring-cloud-learning.git/config-repo/orders-test.properties",
         "source" : {
            "from" : "git-test-2.0"
         }
      },
      {
         "name" : "https://gitee.com/it_freshman/spring-cloud-learning.git/config-repo/orders.properties",
         "source" : {
            "from" : "git-default-2.0"
         }
      }
   ],
   "state" : null,
   "version" : "6beb5e6b1071766ad8b50246dfcb7f1d3e77bcff"
}

同時config-server在從Git獲取配置信息之後,還會存儲一份在config-server的文件系統中,實質上是通過git clone命令將配置內容複製一份在本地存儲,然後讀取這些內容返回給微服務應用進行加載。 通過這種方式,可以有效的防止當Git倉庫出現故障引起無法加載配置信息的情況。

客戶端配置映射

客戶端無需添加特殊註解。

maven依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

bootstrap.properties

將屬性配置放置在bootstrap.properties中,因爲SpringBoot會優先加載名爲bootstrap.properties配置,使得應用從配置中心獲取外部配置信息。

spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/

## 指定應用名稱
spring.application.name=orders

測試類TestController

@RestController
@RefreshScope
public class TestController {

    @Value("from")
    private String from;

    @RequestMapping("/from")
    public String from(){
       return from;
    }
}

測試

啓動應用後,訪問http://localhost:7002/from,

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章