標籤:springcloud spring cloud springcloud微服務 微服務雲架構
引導過程添加的外部配置的預設屬性源是Config Server,但您可以通過將PropertySourceLocator類型的bean添加到引導上下文(通過spring.factories)添加其他源。您可以使用此方法從其他伺服器或資料庫中插入其他屬性。
作為一個例子,請考慮以下微不足道的自訂定位器:
@Configurationpublic class CustomPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource<?> locate(Environment environment) { return new MapPropertySource("customProperty", Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended")); }}
傳入的Environment是要建立的ApplicationContext的Environment,即為我們提供額外的屬性來源的。它將已經具有正常的Spring Boot提供的資源來源,因此您可以使用它們來定位特定於此Environment的屬性源(例如通過將其綁定在spring.application.name上,如在預設情況下所做的那樣Config Server屬性源定位器)。
如果你在這個類中建立一個jar,然後添加一個META-INF/spring.factories包含:
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那麼“customProperty”PropertySource將顯示在其類路徑中包含該jar的任何應用程式中。
更多資料和來源查看
Spring Cloud - 自訂引導屬性源