org.zodiac.loadbalancer.ribbon.breaker.routing.BaseServiceRoutingRule Maven / Gradle / Ivy
package org.zodiac.loadbalancer.ribbon.breaker.routing;
import org.zodiac.core.application.AppInstance;
import org.zodiac.core.bootstrap.breaker.routing.AbstractAppRouter;
import org.zodiac.core.bootstrap.breaker.routing.AppRoutingInfo;
import org.zodiac.core.bootstrap.discovery.AppDiscoveryClient;
import org.zodiac.core.bootstrap.discovery.EnhancedAppDiscoveryClient;
import org.zodiac.loadbalancer.ribbon.discovery.InternalDiscoveryServer;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
public abstract class BaseServiceRoutingRule extends ZoneAvoidanceRule {
private AppRoutingInfo appRoutingInfo;
private AbstractAppRouter serviceRoutingRouter;
private AppDiscoveryClient appDiscoveryClient;
public BaseServiceRoutingRule(AppRoutingInfo appRoutingInfo,
AbstractAppRouter serviceRoutingRouter, AppDiscoveryClient appDiscoveryClient) {
super();
this.appRoutingInfo = appRoutingInfo;
this.serviceRoutingRouter = serviceRoutingRouter;
this.appDiscoveryClient = appDiscoveryClient;
}
@Override
public Server choose(Object key) {
if(!appRoutingInfo.isEnabled()){
return super.choose(key);
}
AppDiscoveryClient client = appDiscoveryClient;
if(appDiscoveryClient instanceof EnhancedAppDiscoveryClient) {
client = ((EnhancedAppDiscoveryClient) appDiscoveryClient).getNativeDiscoveryClient();
}
DynamicServerListLoadBalancer> loadBalancer = (DynamicServerListLoadBalancer>) getLoadBalancer();
String appServiceId = loadBalancer.getName();
T appInstance = serviceRoutingRouter.choose(client.getAppInstances(appServiceId));
if(null == appInstance) {
return null;
}
return new InternalDiscoveryServer(appInstance);
}
}