前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊eureka client的shutdown

聊聊eureka client的shutdown

作者头像
code4it
发布2018-09-17 16:36:50
2.3K0
发布2018-09-17 16:36:50
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下eureka client的shutdown

EurekaRegistration

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java

代码语言:javascript
复制
    @Bean
    @ConditionalOnBean(AutoServiceRegistrationProperties.class)
    @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
    public EurekaRegistration eurekaRegistration(EurekaClient eurekaClient, CloudEurekaInstanceConfig instanceConfig, ApplicationInfoManager applicationInfoManager, ObjectProvider<HealthCheckHandler> healthCheckHandler) {
        return EurekaRegistration.builder(instanceConfig)
                .with(applicationInfoManager)
                .with(eurekaClient)
                .with(healthCheckHandler)
                .build();
    }

    @Bean
    @ConditionalOnBean(AutoServiceRegistrationProperties.class)
    @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
    public EurekaAutoServiceRegistration eurekaAutoServiceRegistration(ApplicationContext context, EurekaServiceRegistry registry, EurekaRegistration registration) {
        return new EurekaAutoServiceRegistration(context, registry, registration);
    }

    //...
    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
    public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config) {
        return new CloudEurekaClient(manager, config, this.optionalArgs,
                    this.context);
    }

这里自动创建EurekaRegistration,以及EurekaAutoServiceRegistration,EurekaClient

EurekaClient

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/EurekaClient.java

可以看到EurekaClient标注了@Bean(destroyMethod = “shutdown”),也就是在bean销毁的时候,会触发eurekaClient.shutdown方法

EurekaRegistration

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java

代码语言:javascript
复制
public class EurekaRegistration implements Registration, Closeable {
    //......
    @Override
    public void close() throws IOException {
        this.eurekaClient.shutdown();
    }
}

这里实现了Closeable接口的close方法,在spring容器关闭的时候触发,这里调用了eurekaClient.shutdown()

EurekaAutoServiceRegistration

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java

代码语言:javascript
复制
    @Override
    public void start() {
        // only set the port if the nonSecurePort or securePort is 0 and this.port != 0
        if (this.port.get() != 0) {
            if (this.registration.getNonSecurePort() == 0) {
                this.registration.setNonSecurePort(this.port.get());
            }

            if (this.registration.getSecurePort() == 0 && this.registration.isSecure()) {
                this.registration.setSecurePort(this.port.get());
            }
        }

        // only initialize if nonSecurePort is greater than 0 and it isn't already running
        // because of containerPortInitializer below
        if (!this.running.get() && this.registration.getNonSecurePort() > 0) {

            this.serviceRegistry.register(this.registration);

            this.context.publishEvent(
                    new InstanceRegisteredEvent<>(this, this.registration.getInstanceConfig()));
            this.running.set(true);
        }
    }
    @Override
    public void stop() {
        this.serviceRegistry.deregister(this.registration);
        this.running.set(false);
    }

这里的start,stop实现的是Lifecycle的方法,也就在spring容器关闭的时候,会触发stop方法,然后调用的是serviceRegistry.deregister(this.registration)

RefreshScopeRefreshedEvent

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaDiscoveryClientConfiguration.java

代码语言:javascript
复制
    @Configuration
    @ConditionalOnClass(RefreshScopeRefreshedEvent.class)
    protected static class EurekaClientConfigurationRefresher {

        @Autowired(required = false)
        private EurekaClient eurekaClient;

        @Autowired(required = false)
        private EurekaAutoServiceRegistration autoRegistration;

        @EventListener(RefreshScopeRefreshedEvent.class)
        public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
            //This will force the creation of the EurkaClient bean if not already created
            //to make sure the client will be reregistered after a refresh event
            if(eurekaClient != null) {
                eurekaClient.getApplications();
            }
            if (autoRegistration != null) {
                // register in case meta data changed
                this.autoRegistration.stop();
                this.autoRegistration.start();
            }
        }
    }

接收到RefreshScopeRefreshedEvent的时候,会先stop,再start

DiscoveryClient.shutdown

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/DiscoveryClient.java

代码语言:javascript
复制
    public synchronized void shutdown() {
        if (isShutdown.compareAndSet(false, true)) {
            logger.info("Shutting down DiscoveryClient ...");

            if (statusChangeListener != null && applicationInfoManager != null) {
                applicationInfoManager.unregisterStatusChangeListener(statusChangeListener.getId());
            }

            cancelScheduledTasks();

            // If APPINFO was registered
            if (applicationInfoManager != null
                    && clientConfig.shouldRegisterWithEureka()
                    && clientConfig.shouldUnregisterOnShutdown()) {
                applicationInfoManager.setInstanceStatus(InstanceStatus.DOWN);
                unregister();
            }

            if (eurekaTransport != null) {
                eurekaTransport.shutdown();
            }

            heartbeatStalenessMonitor.shutdown();
            registryStalenessMonitor.shutdown();

            logger.info("Completed shut down of DiscoveryClient");
        }
    }

    /**
     * unregister w/ the eureka service.
     */
    void unregister() {
        // It can be null if shouldRegisterWithEureka == false
        if(eurekaTransport != null && eurekaTransport.registrationClient != null) {
            try {
                logger.info("Unregistering ...");
                EurekaHttpResponse<Void> httpResponse = eurekaTransport.registrationClient.cancel(instanceInfo.getAppName(), instanceInfo.getId());
                logger.info(PREFIX + "{} - deregister  status: {}", appPathIdentifier, httpResponse.getStatusCode());
            } catch (Exception e) {
                logger.error(PREFIX + "{} - de-registration failed{}", appPathIdentifier, e.getMessage(), e);
            }
        }
    }

这里可以看到,先设置状态为DOWN,然后调用cancel方法

RestTemplateEurekaHttpClient

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/http/RestTemplateEurekaHttpClient.java

代码语言:javascript
复制
    public EurekaHttpResponse<Void> cancel(String appName, String id) {
        String urlPath = serviceUrl + "apps/" + appName + '/' + id;

        ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.DELETE,
                null, Void.class);

        return anEurekaHttpResponse(response.getStatusCodeValue())
                .headers(headersOf(response)).build();
    }

    @Override
    public EurekaHttpResponse<Void> register(InstanceInfo info) {
        String urlPath = serviceUrl + "apps/" + info.getAppName();

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

        ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.POST,
                new HttpEntity<>(info, headers), Void.class);

        return anEurekaHttpResponse(response.getStatusCodeValue())
                .headers(headersOf(response)).build();
    }

cancel方法就是调用REST的DELETE操作,注销掉服务

小结

springcloud封装的eureka自动注册,关闭的时候主要分两大类:

  • 依赖生命周期在销毁时调用shutdown、close EurekaRegistration是在close的时候,会触发eurekaClient.shutdown方法 EurekaAutoServiceRegistration则在stop的时候,标记状态为DOWN,发布StatusChangeEvent事件 EurekaClient则标注了@Bean(destroyMethod = “shutdown”),也就是在bean销毁的时候,会触发eurekaClient.shutdown方法
  • 期间状态变更发布StatusChangeEvent事件 com/netflix/discovery/DiscoveryClient.java有个StatusChangeListener,状态变更在需要的时候,会触发InstanceInfoReplicator的onDemandUpdate方法,该方法会调用discoveryClient.register()去与eureka server更新自身实例的状态。 这里相当于变相通过register接口更改status状态。

这里要区分一下cancel与StatusChangeEvent的区别,cancel是从eureka server删掉这条instanceInfo,而StatusChangeEvent变更状态,不会删除这条instanceInfo,只是更新status状态(status状态一共有UP,DOWN,STARTING,OUT_OF_SERVICE,UNKNOWN几类)。

doc

  • ServiceRegistry Auto-Registration
  • beans-factory-lifecycle
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-05-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • EurekaRegistration
    • EurekaClient
      • EurekaRegistration
        • EurekaAutoServiceRegistration
        • RefreshScopeRefreshedEvent
        • DiscoveryClient.shutdown
          • RestTemplateEurekaHttpClient
          • 小结
          • doc
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档