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

org.zodiac.eureka.client.util.EurekaClientUtil Maven / Gradle / Ivy

package org.zodiac.eureka.client.util;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.sdk.toolkit.util.ReflectionUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.shared.transport.EurekaHttpClient;

public abstract class EurekaClientUtil {

    private EurekaClientUtil() {
    }

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

    public static EurekaHttpClient getQueryEurekaHttpClient(DiscoveryClient discoveryClient) {
        return getEurekaHttpClient(discoveryClient, "queryClient");
    }

    public static EurekaHttpClient getRegistrationEurekaHttpClient(DiscoveryClient discoveryClient) {
        return getEurekaHttpClient(discoveryClient, "registrationClient");
    }

    private static EurekaHttpClient getEurekaHttpClient(DiscoveryClient discoveryClient, String clientFieldName) {
        if (null == discoveryClient || StrUtil.isBlank(clientFieldName)) {
            return null;
        }
        try {
            Field eurekaTransportField = ReflectionUtil.findField(DiscoveryClient.class,
                "eurekaTransport");
            if (null == eurekaTransportField)
                return null;
            ReflectionUtil.makeAccessible(eurekaTransportField);
            Object eurekaTransport = eurekaTransportField.get(discoveryClient);
            if (null == eurekaTransport)
                return null;
            Field httpClientField = ReflectionUtil.findField(eurekaTransport.getClass(), clientFieldName);
            if (null == httpClientField)
                return null;
            ReflectionUtil.makeAccessible(httpClientField);
            EurekaHttpClient eurekaHttpClient = (EurekaHttpClient) httpClientField.get(eurekaTransport);
            return eurekaHttpClient;
        } catch (IllegalAccessException e) {
            log.error("Error getting EurekaHttpClient for field {}, caused by {} .", clientFieldName, e.getMessage());
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy