org.zodiac.autoconfigure.bootstrap.discovery.AbstractAppDiscoveryClientConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.bootstrap.discovery;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.zodiac.autoconfigure.application.ApplicationInfoProperties;
import org.zodiac.autoconfigure.bootstrap.discovery.simple.SimpleAppDiscoveryProperties;
import org.zodiac.commons.constants.Constants;
import org.zodiac.commons.util.Networks;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.core.bootstrap.discovery.AppDiscoveryClient;
import org.zodiac.core.bootstrap.discovery.simple.SimpleAppDiscoveryClient;
import org.zodiac.sdk.toolkit.util.ProtocolScheme;
public abstract class AbstractAppDiscoveryClientConfiguration {
@Autowired(required = false)
private ServerProperties server;
@Autowired
private ApplicationInfoProperties applicationInfo;
private int port = 0;
private SimpleAppDiscoveryProperties simple = new SimpleAppDiscoveryProperties();
protected SimpleAppDiscoveryProperties simpleAppDiscoveryProperties() {
simple.getLocal().setAppServiceId(defaultOf(defaultOf(applicationInfo.getId(), applicationInfo.getName()),
Constants.Spring.DEFAULT_APP_NAME));
simple.getLocal()
.setAppUri(URI.create(ProtocolScheme.HTTP.scheme() + "://" + Networks.LOCAL_HOSTNAME + ":" + findPort()));
return simple;
}
protected AppDiscoveryClient simpleDiscoveryClient() {
return new SimpleAppDiscoveryClient(simpleAppDiscoveryProperties());
}
protected int getPort() {
return port;
}
protected void setPort(int port) {
this.port = port;
}
protected SimpleAppDiscoveryProperties getSimple() {
return simple;
}
private int findPort() {
if (port > 0) {
return port;
}
if (this.server != null && this.server.getPort() != null && this.server.getPort() > 0) {
return this.server.getPort();
}
if (this.applicationInfo != null && this.applicationInfo.getPort() != null
&& this.applicationInfo.getPort() > 0) {
return this.applicationInfo.getPort();
}
return Constants.Spring.DEFAULT_SERVER_PORT_NUMBER;
}
private static String defaultOf(String value, String defaultValue) {
return Strings.defaultIfEmpty(value, defaultValue);
}
}