apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
28.94k stars 10.19k forks source link

configservice内部的Eureka注册服务,如何删除失效的实例。 #5090

Closed liwei-geng closed 5 months ago

liwei-geng commented 5 months ago

操作系统:Centos7 部署方式:分布式虚拟机/物理机部署 问题描述:因同一个Eureka注册两个服务器,后因业务需要去掉了一台,但是注册的记录一直存在,但这个实例明明已经没在连接了,在咱们的描述里,找不到这个删除的地方,还望解答,谢谢,如果不清楚我可以再次补充。

dosubot[bot] commented 5 months ago

🤖

你好,@liwei-geng!我是Dosu,一个可以帮助你解决问题、回答问题以及成为贡献者的机器人。我在这里帮助你,同时我们等待人类维护者的回应。我会仔细查看你的问题,并且很快给你一个完整的答案。

liwei-geng commented 5 months ago

微信截图_20240203195432 bi比如红色框内的部分,明明实例已经删除了,但是在系统信息或者Eureka里还存在,该如何删除呢

dosubot[bot] commented 5 months ago

🤖

要从Apollo的configservice的Eureka注册中删除无效的实例,您可以使用InstanceService类中的batchDeleteInstanceConfig方法。此方法会删除所有具有指定的configAppIdconfigClusterNameconfigNamespaceName的实例。

以下是如何使用此方法的示例:

InstanceService instanceService = ... // 获取InstanceService的实例
int deletedCount = instanceService.batchDeleteInstanceConfig("yourConfigAppId", "yourConfigClusterName", "yourConfigNamespaceName");

在此示例中,将"yourConfigAppId""yourConfigClusterName""yourConfigNamespaceName"替换为您要删除的实例的实际appIdclusterNamenamespaceName

请注意,此操作是事务性的,这意味着如果发生错误,它将删除所有匹配的实例或者一个都不删除。此外,此操作直接从数据库中删除实例,因此请谨慎使用。

至于Eureka注册中的实例注册和注销,Apollo的configservice通过ApolloServiceRegistryDeregisterApplicationListener类处理实例注销。此类实现了ApplicationListener<ContextClosedEvent>接口,这意味着它监听ContextClosedEvent事件。此事件在Spring应用程序上下文关闭时发布,通常在应用程序关闭时。

onApplicationEvent(ContextClosedEvent event)方法由ContextClosedEvent触发时,它调用deregister()方法。此方法尝试通过调用this.serviceRegistry.deregister(this.registration)从Eureka注册中注销服务。

如果注销成功,它会记录成功消息,包括服务名称,URI和集群。如果注销失败,它会记录错误消息,包括相同的信息和抛出的异常。

相关代码如下:

@Override
public void onApplicationEvent(ContextClosedEvent event) {
  this.deregister();
}

private void deregister() {
  try {
    this.serviceRegistry.deregister(this.registration);
    log.info(
        "deregister success, '{}' uri '{}', cluster '{}'",
        this.registration.getServiceName(),
        this.registration.getUri(),
        this.registration.getCluster()
    );
  } catch (Throwable t) {
    log.error(
        "deregister fail, '{}' uri '{}',  cluster '{}'",
        this.registration.getServiceName(),
        this.registration.getUri(),
        this.registration.getCluster(),
        t
    );
  }
}

注册过程在提供的上下文中没有显示,所以我无法提供详细信息。

参考资料:

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.