org.zodiac.loadbalancer.ribbon.discovery.InternalDiscoveryServerList Maven / Gradle / Ivy
package org.zodiac.loadbalancer.ribbon.discovery;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;
import com.netflix.loadbalancer.Server;
import org.zodiac.core.application.AppInstance;
import org.zodiac.core.bootstrap.discovery.AppDiscoveryClient;
import org.zodiac.core.bootstrap.discovery.EnhancedAppDiscoveryClient;
import java.util.List;
import java.util.stream.Collectors;
public class InternalDiscoveryServerList extends AbstractServerList {
String appServiceId;
AppDiscoveryClient appDiscoveryClient;
public InternalDiscoveryServerList(AppDiscoveryClient appDiscoveryClient) {
this.appDiscoveryClient = appDiscoveryClient;
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
this.appServiceId = clientConfig.getClientName();
}
@Override
public List getInitialListOfServers() {
return getServers();
}
@Override
public List getUpdatedListOfServers() {
return getServers();
}
private List getServers() {
if(appDiscoveryClient instanceof EnhancedAppDiscoveryClient) {
List instances = ((EnhancedAppDiscoveryClient) appDiscoveryClient).getNativeDiscoveryClient().getAppInstances(appServiceId);
return instances.stream().map(InternalDiscoveryServer::new).collect(Collectors.toList());
}
return appDiscoveryClient.getAppInstances(appServiceId).stream().map(InternalDiscoveryServer::new).collect(Collectors.toList());
}
}