gpt4 book ai didi

java - 使用 Java 而不是 bootstrap.yml 通过 Eureka 进行 Spring Cloud Config Server 查找

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:41 24 4
gpt4 key购买 nike

我正在尝试将代码构建到我们的基础 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 cloud 版本 (1.0.2) 中,对于我上面的 config-lookup-through-eureka java property source 配置,在 spring 初始化周期中过早地检索和构造属性源。另外,如果 Eureka 服务器在 Bootstrap 启动时速度很慢或不可用,则当 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 配置 Bootstrap 属性仍然是一个挑战。我这样做是因为我正在开发一个没有模板或代码生成的框架(spring boot 的前提)。我已经在混合中添加了 spring-retry 并且 client-to-config 被重试但是重新注册到 Eureka 没有。这就是为什么必须为我放弃 Eureka-first 的原因。我投票赞成将 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() 时,这个 started() 实际上在 spring 代码中被调用两次(顺便说一句,一次没有传递任何程序参数)。

最佳答案

如果您的 PropertySourceLocator 列在 spring.factories 中(我假设是 BootstrapConfiguration),那么它需要是一个 @组件(或者甚至是 @Configuration)。

关于java - 使用 Java 而不是 bootstrap.yml 通过 Eureka 进行 Spring Cloud Config Server 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com