一致性hash
/**
* 指定方法设置负载均衡
*
* @param serviceConfig
* @author Jamin
* @date 2020/8/23 18:07
*/
public static void setLoadbalance(ServiceConfig serviceConfig) {
serviceConfig.setLoadbalance("consistenthash");
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("getByName");
Map<String, String> params = new HashMap<>(10);
params.put("hash.arguments", "0,1");
params.put("hash.nodes", "200");
methodConfig.setParameters(params); serviceConfig.setMethods(Collections.singletonList(methodConfig));
}
异步
public static void main(String[] args) throws IOException {
ApplicationConfig applicationConfig = new ApplicationConfig("client");
RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.150.120:2181");
ReferenceConfig referenceConfig = new ReferenceConfig();
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(UserService.class);
// referenceConfig.setUrl("dubbo://192.168.21.1:20880/cn.jaminye.base.UserService");
referenceConfig.setApplication(applicationConfig);
// referenceConfig.setGroup("jamin");
setLoadbalance(referenceConfig);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (bufferedReader.readLine().equals("quit")) {
break;
}
UserService userService = (UserService) referenceConfig.get();
System.out.println(userService.getByName("测试", "12"));
System.out.println(userService.getByName("212121212", "313dsdas"));
System.out.println(userService.getByName("1231asdas", "casdas"));
System.out.println(userService.getByName("12cacas3344D", "12121213122112"));
System.out.println(userService.getByName("12312adsd", "dasdas"));
System.out.println(userService.getByName("测试", "12"));
}
}