天天看點

Spring cloud配置用戶端

Spring Environment

Environment是一種在Spring容器内已配置(Profile)和屬性(Properties)為模型的應用環境抽象整合。

Spring Framewwork提供了兩種Enviroment的實作,即

  • 一般應用:StandardEnvironment
  • Web應用:StandardServletEnvironment

Spring Profiles

配置(Profile)

在Spring容器,Profile是一種命名的Bean定義邏輯組。一個Spring應用可以同時激活多個Profile,常見的使用場景如:應用部署環境(test、stage、production)、單元測試等。

應用程式可通過調用ConfigurableEnvironment接口控制Profile的激活,如:

  • setActiveProfiles(String...)會覆寫之前profile
  • addActiveProfile(String)會增加profile
  • setDefaultProfiles(String...)設定預設初始化方法

Spring Properties

屬性(Properties)

屬性又稱之為配置項,Key-Value的形式。在Spring應用中常用作占位符(Placeholder),而在API層面,Spring Framework如下抽象來表述:

  • 組合屬性:PropertySources
  • 單一屬性:PropertySource

Spring 事件監聽器

事件(Event)

ApplicationEvent

事件監聽器(EventListener)

ApplicationListener

ConfigFileApplicationListener

在Spring Boot場景中,用于讀取預設以及Profile關聯的配置檔案(application.properties)

Spring Boot配置檔案

application.properties或application.xml

加載器:PropertiesPropertySourceLoader

application.yml或者application.yaml

加載器:YamlPropertySourceLoader

Environment端點

請求URL:/env

資料來源:EnvironmentEndpoint

Controller來源:EnvironmentMvcEndpoint