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

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

package org.zodiac.eureka.client;

import java.util.Objects;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.transport.EurekaHttpClient;
import com.netflix.discovery.shared.transport.EurekaHttpResponse;

public class DefaultEurekaClientService implements EurekaClientService {

    protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());

    private ApplicationInfoManager applicationInfoManager;
    private EurekaClient eurekaClient;
    private EurekaHttpClient eurekaHttpClient;
    private InstanceInfo instanceInfo;
    private String appPathIdentifier;

    public DefaultEurekaClientService(@NotNull ApplicationInfoManager applicationInfoManager, @NotNull EurekaClient eurekaClient,
        EurekaHttpClient eurekaHttpClient) {
        super();
        this.applicationInfoManager = Objects.requireNonNull(applicationInfoManager, "applicationInfoManager");
        this.eurekaClient = Objects.requireNonNull(eurekaClient, "eurekaClient");

        this.instanceInfo = applicationInfoManager.getInfo();
        String appName = this.instanceInfo.getAppName();
        String instanceId = this.instanceInfo.getId();
        this.appPathIdentifier = String.format("%s/%s", appName, instanceId);
    }

    @Override
    public void unregisterClient() {
        if (null == eurekaHttpClient) {
            log.warn("No EurekaHttpClient provided, this operation is not supported.");
            return;
        }
        InstanceInfo self = applicationInfoManager.getInfo();
        try {
            String appName = self.getAppName();
            String instanceId = self.getId();
            log.info("Unregistering Eureka client [{} - {}]...", appName, instanceId);
            EurekaHttpResponse httpResponse = eurekaHttpClient.cancel(appName, instanceId);
            log.info("{} deregistered status [{}] .", appPathIdentifier, httpResponse.getStatusCode());
        } catch (Exception e) {
            log.error(String.format("%s deregistered failed, caused by %s .", appPathIdentifier, e.getMessage()), e);
        }
    }

    @Override
    public void stopClient() {
        eurekaClient.shutdown();
    }

    @Override
    public void clientStatusUp() {
        changeClientStatus(InstanceStatus.UP);
    }

    @Override
    public void clientStatusDown() {
        changeClientStatus(InstanceStatus.DOWN);
    }

    @Override
    public void clientStatusOutOfService() {
        changeClientStatus(InstanceStatus.OUT_OF_SERVICE);
    }

    @Override
    public void clientStatusUnknow() {
        changeClientStatus(InstanceStatus.UNKNOWN);
    }

    private void changeClientStatus(InstanceStatus instanceStatus) {
        applicationInfoManager.setInstanceStatus(instanceStatus);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy