有关服务发现的问题,请结合。
目前,我有一个web应用程序,有执行器和弹簧引导库伯内特斯依赖关系。我也在使用kubernetes提供的发现客户端,一切都很好。
然而,当我卷曲我的健康端点时,我确实看到了一些奇怪的语句:
discoveryComposite":{"description":"Discovery Client not initialized","status":"UNKNOWN","components":{"discoveryClient":{"description":"Discovery Client not initialized","status":"UNKNOWN"}}
"reactiveDiscoveryClients":{"description":"Discovery Client not initialized","status":"UNKNOWN","components":{"Kubernetes Reactive Discovery Client":{"description":"Discovery Client not initialized","status":"UNKNOWN"}
Simple Reactive Discovery Client":{"description":"Discovery Client not initialized","status":"UNKNOWN"}}}
"readinessState":{"status":"UP"},"refreshScope":{"status":"UP"}},"groups":["liveness","readiness"]}*我可否问一问,为何“未知”?我希望这里的三家公司中至少有一家能做些什么,而不是“探索客户端没有初始化”。
我忘了初始化什么了吗?注册什么?来配置什么东西?
顺便说一句,这确实是一个关于库伯奈特斯发现的问题。与尤里卡无关,与领事等无关。
非常感谢
发布于 2020-12-16 05:46:15
有同样的问题,我注意到org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicator将其discoveryInitialized字段设置为false。这是因为没有人在应用程序上下文中触发InstanceRegisteredEvent。通常,事件应该从所谓的“注册”的start()方法中触发--这是一个bean,负责在服务注册表中注册当前应用程序实例,例如在Eureka情况下的EurekaAutoServiceRegistration。
要点是,Kubernetes本身并不是一个服务注册中心,它不需要显式注册(因为由于编排的性质,它中的每个pod在默认情况下都是“注册”的)。这就是默认不使用Kubernetes注册应用程序实例的原因。
但是,有一些org.springframework.cloud.kubernetes.registry.KubernetesAutoServiceRegistration类可以通过向日志发送消息来“模拟”自动注册的过程。对于v1.1.0,这个类是@Deprecated,在其他框架中没有使用。我目前看到使用它的唯一好处是,它可以触发丢失的InstanceRegisteredEvent来初始化DiscoveryClientHealthIndicator。您可以通过向任何@Configuration类添加以下bean声明来使用它:
@Bean
public KubernetesAutoServiceRegistration kubernetesAutoServiceRegistration(
ApplicationContext context,
KubernetesServiceRegistry registry,
KubernetesRegistration registration) {
return new KubernetesAutoServiceRegistration(context, registry, registration);
}但是请不要太依赖它,,因为它可能会在即将发布的框架版本中被删除。
作为另一种选择,您可以在代码中的某个地方自行发出InstanceRegisteredEvent,但要确保在应用程序实际准备使用发现客户端时可以这样做。
发布于 2022-06-17 21:50:53
DiscoveryClientHealthIndicator实现了ApplicationListener<InstanceRegisteredEvent<?>>,并重写了onApplicationEvent(InstanceRegisteredEvent<?> event)方法来侦听事件并使DiscoveryClientHealthIndicator无效,因为disoveryInitialize字段在开始时设置为false。
要触发InstanceRegisteredEvent,您需要创建该事件的发布服务器。发布者构造事件对象并将其发布给正在侦听的任何人。
要发布事件,发布者可以简单地注入ApplicationEventPublisher并使用publishEvent() API。在bean属性初始化之后调用它在带有@PostConstruct注释的方法中,以确保应用程序实际上已经准备好与发现客户端一起工作。
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@PostConstruct
public void postConstruct(){
String message = "Instance register event triggered";
publishCustomEvent(message);
}
public void publishCustomEvent(final String message) {
InstanceRegisteredEvent<?> instanceRegisteredEvent = new InstanceRegisteredEvent<>(this, message);
applicationEventPublisher.publishEvent(instanceRegisteredEvent);
}https://stackoverflow.com/questions/64939576
复制相似问题