[英]Spring Boot: Load common config application properties file using bootstrap.yml
[英]Spring Cloud Config Server lookup through Eureka using Java instead of bootstrap.yml
我正在嘗試在我們的基礎pom中構建代碼,通過Eureka自動配置Spring Cloud Config服務器查找。 我們這樣做是為了避免模仿開發人員構建微服務的.yml屬性。 例如,我們想要java配置從這些屬性觸發的所有行為:
spring:
application:
name: MyMicroservice
cloud:
config:
enabled: true
server:
prefix: /diagnostics/admin/config
failFast: true
discovery:
enabled: true
serviceId: echo
management:
context-path: /diagnostics/admin
eureka:
password: password
client:
serviceUrl:
defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
statusPageUrlPath: /diagnostics/admin/info
healthCheckUrlPath: /diagnostics/admin/health
經過大量實驗后,除了Eureka發現的配置服務器(導致沒有覆蓋的配置屬性)之外,以下方法大部分都有效:
@Order(-1)
public class AdditionalBootstrapPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
Map<String, Object> theBootstrapYmlConfig = new HashMap<>();
theBootstrapYmlConfig.put("spring.cloud.config.enabled", new Boolean(true));
theBootstrapYmlConfig.put("spring.cloud.config.server.prefix", "/diagnostics/admin/config");
theBootstrapYmlConfig.put("spring.cloud.config.failFast", new Boolean(true));
theBootstrapYmlConfig.put("spring.cloud.config.discovery.enabled", new Boolean(true));
theBootstrapYmlConfig.put("spring.cloud.config.discovery.serviceId", "echo");
theBootstrapYmlConfig.put("management.context-path", "/diagnostics/admin");
theBootstrapYmlConfig.put("eureka.client.serviceUrl.defaultZone", "http://user:password@localhost:8761/eureka/");
theBootstrapYmlConfig.put("eureka.instance.leaseRenewalIntervalInSeconds", new Integer(10));
theBootstrapYmlConfig.put("eureka.instance.statusPageUrlPath", "/diagnostics/admin/info");
theBootstrapYmlConfig.put("eureka.instance.healthCheckUrlPath", "/diagnostics/admin/health");
return new MapPropertySource("myExtraBootstrap", theBootstrapYmlConfig);
}
}
我似乎也需要這個Bean:
@ConditionalOnWebApplication
@Configuration
@Import(EurekaClientAutoConfiguration.class)
public class WorkfrontDiscoveryClientConfigServiceBootstrapConfiguration {
@Bean
@ConditionalOnClass({ DiscoveryClient.class, ConfigServicePropertySourceLocator.class })
@ConditionalOnMissingBean
DiscoveryClientConfigServiceBootstrapConfiguration discoveryClientConfigServiceBootstrapConfiguration() {
DiscoveryClientConfigServiceBootstrapConfiguration discoveryClientConfigServiceBootstrapConfiguration =
new DiscoveryClientConfigServiceBootstrapConfiguration();
return discoveryClientConfigServiceBootstrapConfiguration;
}
}
最后,我將兩者放入spring.factories以確保它們被構造。 問題是PropertySourceLocator永遠不會用於在ConfigServicePropertySourceLocator中構造調用來檢索屬性。 無論我做什么,我似乎無法匹配指定bootstrap.yml中的屬性會產生的行為。
4天后編輯
這里的關鍵因素(和限制)是通過Eureka查找配置服務器的能力。 在當前的Spring雲版本(1.0.2)中,在Spring初始化周期中過早檢索和構造屬性源,以用於上面的config-lookup-through-eureka java屬性源配置。 此外,如果Eureka服務器在引導啟動時很慢或不可用,則在Eureka最終出現時,永遠不會重建Config服務器屬性源。 在我看來這是一個錯誤。
我通過消除通過Eureka查找配置服務器的概念解決了這一切,並在bootstrap.yml中要求這個最小配置:
spring:
application:
name: MyMicroservice
cloud:
config:
uri: http://localhost:8888/diagnostics/admin/config
eureka:
client:
serviceUrl:
defaultZone: http://user:password@localhost:8761/eureka/
然后在java AdditionalBootstrapPropertySourceLocator中設置其余部分
30天后編輯
Java配置引導屬性仍然是一個挑戰。 我這樣做是因為我正在開發一個沒有模板化或代碼生成的框架(彈簧啟動的前提)。 我已經添加了spring-retry並且client-to-config被重試但是重新注冊到Eureka沒有。 這就是Eureka首先不得不為我拋棄的原因。 我投票將spring-retry整合到Eureka注冊過程中,所以我可以回到Eureka-first來獲取我的框架。 仍然在Spring Cloud 1.0.2上。
100天后編輯
更新我們最終的位置。 繼續我們避免財產模板,在代碼中實施策略和實踐的咒語......並且在沒有Eureka優先概念的情況下繼續,我們放棄了PropertySourceLocator並簡單地使用SpringApplicationRunListener,如下所示:
public class OurFrameworkProperties implements SpringApplicationRunListener {
:
public void started() {
if (TestCaseUtils.isRunningFromTestCase()) {
System.setProperty("spring.cloud.config.failFast", "false");
System.setProperty("spring.cloud.config.enabled", "false");
System.setProperty("eureka.client.enabled", "false");
} else {
// set production values same way
}
}
}
注意,每次Spring應用程序運行或獲取Actuator refresh()時,這個開始()實際上會在spring代碼中調用兩次(一次不傳遞任何程序參數btw)。
如果你的PropertySourceLocator
列在spring.factories
(我假設是一個BootstrapConfiguration
),那么它需要是@Component
(或者甚至是@Configuration
)。
你必須在boostrap.properties中設置這個屬性
eureka.instance.metadataMap.configPath: /your-app-name
並評論這一個
#spring.cloud.config.uri=http://localhost:8888/
顯然它也必須是這個
eureka.client.serviceUrl.defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
eureka.client.instance.preferIpAddress: true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.