You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "chaoyoung (via GitHub)" <gi...@apache.org> on 2023/09/21 02:38:07 UTC

[GitHub] [dubbo] chaoyoung commented on issue #13081: Directory of type RegistryDirectory already destroyed for service xxx while dubbo graceful shutdown

chaoyoung commented on issue #13081:
URL: https://github.com/apache/dubbo/issues/13081#issuecomment-1728680272

   目前通过外部先切掉入口HTTP流量解决了此问题。
   
   Spring Boot `application.yml` 配置graceful shutdown
   
   ```yml
   server:
     shutdown: graceful
   spring:
     lifecycle:
       timeout-per-shutdown-phase: 30s
   ```
   
   应用程序提供实例下线的接口
   
   ```java
   @RestController
   @RequiredArgsConstructor
   @Slf4j
   public class GracefulShutdownController {
   
       private final NacosAutoServiceRegistration nacosAutoServiceRegistration;
   
       @PostMapping("/instance/offline")
       public void instanceOffline() {
           nacosAutoServiceRegistration.destroy();
           log.info("instance offline from nacos.");
       }
   }
   ```
   
   k8s preStop 通过curl调用实例下线的接口,将入口http流量先断掉,然后等Spring和Dubbo graceful shutdown,最多等待30s。
   
   ```yaml
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: xxx-backend
     namespace: default
     labels:
       app: xxx-backend
   spec:
     replicas: 1
     selector:
       matchLabels:
         app: xxx-backend
     template:
       metadata:
         labels:
           app: xxx-backend
       spec:
         containers:
         - name: xxx-backend
           image: registry.cn-hangzhou.aliyuncs.com/namespace/xxx-backend:latest
           imagePullPolicy: Always
           ports:
           - containerPort: 8080
             name: http
             protocol: TCP
           lifecycle:
             preStop:
               exec:
                 command: ["/bin/sh", "-c", "curl -X POST localhost:8080/instance/offline"]
         terminationGracePeriodSeconds: 30
   ```
   
   因为使用了Spring Cloud Gateway作为后端微服务网关,所以Gateway这边也需要在实例下线的时候清理缓存的操作。
   
   ```java
   /**
    * 订阅Nacos通知
    * 接收Nacos推送的微服务上下线实例信息
    */
   @Component
   @Slf4j
   public  class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> {
       @Resource
       private CacheManager caffeineLoadBalancerCacheManager;
       @PostConstruct
       public void registerToNotifyCenter() {
           NotifyCenter.registerSubscriber(this);
       }
       @Override
       public void onEvent(InstancesChangeEvent event) {
           log.info("Spring Gateway 接收实例刷新事件:{}, 开始刷新缓存", JacksonUtils.toJson(event));
           Cache cache = caffeineLoadBalancerCacheManager.getCache(SERVICE_INSTANCE_CACHE_NAME);
           if (cache != null) {
               cache.evict(event.getServiceName());
           }
           log.info("Spring Gateway 实例刷新完成");
       }
       @Override
       public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() {
           return InstancesChangeEvent.class;
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org