com.github.df.restypass.lb.LoadBalanceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resty-pass Show documentation
Show all versions of resty-pass Show documentation
High-Performance Restful Client Library
The newest version!
package com.github.df.restypass.lb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap;
/**
* 负载均衡器工厂类
* Created by darrenfu on 17-6-28.
*/
public class LoadBalanceFactory {
private static final Logger log = LoggerFactory.getLogger(LoadBalanceFactory.class);
private static final ConcurrentHashMap serviceLoadBalancerMap = new ConcurrentHashMap<>();
/**
* 按照service提供LB
*
* @param serviceName
* @param loadBalancerClz
* @return
*/
public static LoadBalancer createLoadBalancerForService(String serviceName, Class extends LoadBalancer> loadBalancerClz) {
LoadBalancer balancer = serviceLoadBalancerMap.get(serviceName);
if (balancer == null) {
LoadBalancer newLoadBalancer = createLoadBalancer(loadBalancerClz);
serviceLoadBalancerMap.putIfAbsent(serviceName, newLoadBalancer);
balancer = serviceLoadBalancerMap.get(serviceName);
}
return balancer;
}
private static LoadBalancer createLoadBalancer(Class extends LoadBalancer> loadBalancer) {
if (RandomLoadBalancer.class.equals(loadBalancer)) {
return createRandomLoadBalancer();
} else if (RoundRobinLoadBalancer.class.equals(loadBalancer)) {
return createRoundRobinBalancer();
} else {
try {
return loadBalancer.newInstance();
} catch (InstantiationException | IllegalAccessException | ClassCastException e) {
log.error("无法创建指定的负载均衡器:{},ex:{} ", loadBalancer, e.getMessage(), e);
}
}
return createRoundRobinBalancer();
}
/**
* Create random load balancer load balancer.
*
* @return the load balancer
*/
public static LoadBalancer createRandomLoadBalancer() {
return new RandomLoadBalancer();
}
public static LoadBalancer createRoundRobinBalancer() {
return new RoundRobinLoadBalancer();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy