Spencer Gibb
10 years ago
5 changed files with 91 additions and 20 deletions
@ -0,0 +1,11 @@ |
|||||||
|
package org.springframework.cloud.client; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* @author Spencer Gibb |
||||||
|
* TODO: name? Server? HostAndPort? Instance? |
||||||
|
*/ |
||||||
|
public interface ServiceInstance { |
||||||
|
public String getHost(); |
||||||
|
public int getPort(); |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
package org.springframework.cloud.client.loadbalancer; |
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Spencer Gibb |
||||||
|
*/ |
||||||
|
public interface LoadBalancerClient { |
||||||
|
/** |
||||||
|
* Choose a {@see ServiceInstance} from the LoadBalancer for the specified service |
||||||
|
* @param serviceId The serviceId to use to look up the LoadBalancer |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public ServiceInstance choose(String serviceId); |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
package org.springframework.cloud.netflix.ribbon; |
||||||
|
|
||||||
|
import com.netflix.client.ClientFactory; |
||||||
|
import com.netflix.loadbalancer.ILoadBalancer; |
||||||
|
import com.netflix.loadbalancer.Server; |
||||||
|
import org.springframework.cloud.client.ServiceInstance; |
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Spencer Gibb |
||||||
|
*/ |
||||||
|
public class RibbonLoadBalancerClient implements LoadBalancerClient { |
||||||
|
@Override |
||||||
|
public ServiceInstance choose(String serviceId) { |
||||||
|
ILoadBalancer loadBalancer = ClientFactory.getNamedLoadBalancer(serviceId); |
||||||
|
Server server = loadBalancer.chooseServer(null); |
||||||
|
if (server == null) { |
||||||
|
throw new IllegalStateException("Unable to locate ILoadBalancer for service: "+ serviceId); |
||||||
|
} |
||||||
|
return new RibbonServer(server); |
||||||
|
} |
||||||
|
|
||||||
|
private class RibbonServer implements ServiceInstance { |
||||||
|
private Server server; |
||||||
|
|
||||||
|
private RibbonServer(Server server) { |
||||||
|
this.server = server; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getHost() { |
||||||
|
return server.getHost(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getPort() { |
||||||
|
return server.getPort(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue