org.zodiac.autoconfigure.eureka.EurekaRegistrationAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.eureka;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnAppDiscoveryEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.autoconfigure.eureka.condition.ConditionalOnEurekaClientEnabled;
import org.zodiac.commons.concurrent.thread.EnhancedExecutors;
import org.zodiac.commons.json.jackson.JacksonFactory;
import org.zodiac.commons.util.Networks;
import org.zodiac.sdk.toolkit.util.SystemClock;
import com.netflix.discovery.EurekaClient;
@SpringBootConfiguration
@ConditionalOnEurekaClientEnabled
@ConditionalOnAppDiscoveryEnabled
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnClass(value = {org.zodiac.eureka.client.EurekaAppDiscoveryClient.class, com.netflix.discovery.EurekaClient.class, com.netflix.config.ConfigurationManager.class})
public class EurekaRegistrationAutoConfiguration implements InitializingBean {
private static Logger log = LoggerFactory.getLogger(EurekaRegistrationAutoConfiguration.class);
private final ExecutorService executorService = EnhancedExecutors.newSingleThreadExecutor("Eureka Application Client",
new LinkedBlockingQueue<>(50));
private EurekaProperties eurekaProperties;
private com.netflix.discovery.EurekaClient eurekaClient;
public EurekaRegistrationAutoConfiguration(EurekaProperties eurekaProperties, EurekaClient eurekaClient) {
this.eurekaProperties = eurekaProperties;
this.eurekaClient = eurekaClient;
}
@Override
public void afterPropertiesSet() throws Exception {
waitForRegistrationWithEureka();
}
protected void waitForRegistrationWithEureka() {
long startTime = SystemClock.nowTimeMillis();
executorService.submit(() -> {
while (true) {
if (SystemClock.nowTimeMillis() - startTime > eurekaProperties.getVerifyingTimeoutMills()) {
log.warn("Eureka registration status not verifing, please check it!");
return;
}
try {
List instanceInfos =
eurekaClient.getInstancesByVipAddress(eurekaProperties.getInstance().getVipAddr(), false);
for (com.netflix.appinfo.InstanceInfo nextInstanceInfo : instanceInfos) {
if (nextInstanceInfo.getIPAddr().equals(Networks.LOCALHOST_IP)
|| nextInstanceInfo.getIPAddr().equals(Networks.LOCAL_ADDRESS_IP)) {
String instanceInfoJson = JacksonFactory.newJacksonMapper().writerWithDefaultPrettyPrinter()
.writeValueAsString(nextInstanceInfo);
log.info("Verifying application registration with eureka finished, instance:\n{}",
instanceInfoJson);
return;
}
}
} catch (Throwable e) {
}
int awaitTime = 7000;
try {
Thread.sleep(awaitTime);
} catch (Exception e1) {
}
log.info("Waiting {} milliseconds. Verifying application registration with eureka.", awaitTime);
}
});
}
}