提交 7390bed8 编写于 作者: G guide

[test]add ConsistentHashLoadBalance test

上级 dbf630c7
......@@ -11,21 +11,21 @@ import java.util.Map;
* @createTime 2020年06月03日 15:04:00
*/
public final class SingletonFactory {
private static volatile Map<String, Object> OBJECT_MAP = new HashMap<>();
private static volatile Map<String, Object> objectMap = new HashMap<>();
private SingletonFactory() {
}
public static <T> T getInstance(Class<T> c) {
String key = c.toString();
Object instance = OBJECT_MAP.get(key);
Object instance = objectMap.get(key);
if (instance == null) {
synchronized (SingletonFactory.class) {
instance = OBJECT_MAP.get(key);
instance = objectMap.get(key);
if (instance == null) {
try {
instance = c.getDeclaredConstructor().newInstance();
OBJECT_MAP.put(key, instance);
objectMap.put(key, instance);
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (NoSuchMethodException | InvocationTargetException e) {
......
......@@ -12,7 +12,8 @@ import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* refer to dubbo consistent hash load balance: http://dubbo.apache.org/zh-cn/blog/dubbo-consistent-hash-implementation.html
* refer to dubbo consistent hash load balance: https://github.com/apache/dubbo/blob/2d9583adf26a2d8bd6fb646243a9fe80a77e65d5/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
*
* @author RicardoZ
* @createTime 2020年10月20日 18:15:20
*/
......@@ -56,15 +57,13 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
}
static byte[] md5(String key) {
MessageDigest md = null;
MessageDigest md;
try {
md = MessageDigest.getInstance("md5");
md = MessageDigest.getInstance("MD5");
byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
md.update(bytes);
} catch (NoSuchAlgorithmException e) {
log.error("An encryption algorithm that does not exist is used: ", e);
e.printStackTrace();
throw new IllegalStateException(e.getMessage(), e);
}
return md.digest();
......@@ -75,7 +74,7 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
}
public String select(String rpcServiceName) {
byte[] digest = md5(rpcServiceName);
byte[] digest = md5(rpcServiceName);
return selectForKey(hash(digest, 0));
}
......
package github.javaguide.loadbalance.loadbalancer;
import github.javaguide.extension.ExtensionLoader;
import github.javaguide.loadbalance.LoadBalance;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
class ConsistentHashLoadBalanceTest {
@Test
void TestConsistentHashLoadBalance() {
LoadBalance loadBalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension("loadBalance");
List<String> serviceUrlList = new ArrayList<>(Arrays.asList("127.0.0.1:9997", "127.0.0.1:9998", "127.0.0.1:9999"));
String userRpcServiceName = "github.javaguide.UserServicetest1version1";
String userServiceAddress = loadBalance.selectServiceAddress(serviceUrlList, userRpcServiceName);
assertEquals("127.0.0.1:9999",userServiceAddress);
String schoolRpcServiceName = "github.javaguide.SchoolServicetest1version1";
String schoolServiceAddress = loadBalance.selectServiceAddress(serviceUrlList, schoolRpcServiceName);
assertEquals("127.0.0.1:9997",schoolServiceAddress);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册