All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.eureka.client.EurekaAppServiceRegistry Maven / Gradle / Ivy

package org.zodiac.eureka.client;

import static com.netflix.appinfo.InstanceInfo.InstanceStatus.UNKNOWN;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.core.bootstrap.registry.AppServiceRegistry;

import com.netflix.appinfo.InstanceInfo;

public class EurekaAppServiceRegistry implements AppServiceRegistry {

    private static final Logger log = LoggerFactory.getLogger(EurekaAppServiceRegistry.class);

    @Override
    public void register(EurekaAppRegistration registration) {
        maybeInitializeClient(registration);

        if (log.isInfoEnabled()) {
            log.info("Registering application {} with eureka with status {} .",
                registration.getApplicationInfoManager().getInfo().getAppName(),
                registration.getInstanceConfig().getInitialStatus());
        }

        registration.getApplicationInfoManager()
                .setInstanceStatus(registration.getInstanceConfig().getInitialStatus());

        Optional.ofNullable(registration.getHealthCheckHandler()).ifPresent(healthCheckHandler -> registration
            .getEurekaClient().registerHealthCheck(healthCheckHandler));
    }

    @Override
    public void deregister(EurekaAppRegistration registration) {
        if (registration.getApplicationInfoManager().getInfo() != null) {

            if (log.isInfoEnabled()) {
                log.info("Unregistering application {} with eureka with status DOWN.",
                    registration.getApplicationInfoManager().getInfo().getAppName());
            }

            registration.getApplicationInfoManager()
                    .setInstanceStatus(InstanceInfo.InstanceStatus.DOWN);

            // shutdown of eureka client should happen with EurekaRegistration.close()
            // auto registration will create a bean which will be properly disposed
            // manual registrations will need to call close()
        }
    }

    @Override
    public void close() {
    }

    @Override
    public void setStatus(EurekaAppRegistration registration, String status) {
        InstanceInfo info = registration.getApplicationInfoManager().getInfo();

        // TODO: howto deal with delete properly?
        if ("CANCEL_OVERRIDE".equalsIgnoreCase(status)) {
            registration.getEurekaClient().cancelOverrideStatus(info);
            return;
        }

        // TODO: howto deal with status types across discovery systems?
        InstanceInfo.InstanceStatus newStatus = InstanceInfo.InstanceStatus
                .toEnum(status);
        registration.getEurekaClient().setStatus(newStatus, info);
    }

    @Override
    public Object getStatus(EurekaAppRegistration registration) {
        String appname = registration.getApplicationInfoManager().getInfo().getAppName();
        String instanceId = registration.getApplicationInfoManager().getInfo().getId();
        InstanceInfo info = registration.getEurekaClient().getInstanceInfo(appname,
                instanceId);

        Map status = new HashMap<>();
        if (info != null) {
            status.put("status", info.getStatus().toString());
            status.put("overriddenStatus", info.getOverriddenStatus().toString());
        } else {
            status.put("status", UNKNOWN.toString());
        }

        return status;
    }

    private void maybeInitializeClient(EurekaAppRegistration registration) {
        // force initialization of possibly scoped proxies
        registration.getApplicationInfoManager().getInfo();
        registration.getEurekaClient().getApplications();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy