mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-21 08:27:11 +00:00
chore[loadbalancer]: cached ConsistentHashConsumer comment
This commit is contained in:
@@ -18,7 +18,6 @@ import com.zfoo.net.consumer.registry.ZookeeperRegistry;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.util.AssertionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -65,7 +64,7 @@ public class ConfigManager implements IConfigManager {
|
||||
for (var provider : providerConfig.getProviders()) {
|
||||
var protocolModule = provider.getProtocolModule();
|
||||
var providerName = provider.getProvider();
|
||||
AssertionUtils.isTrue(ProtocolManager.moduleByModuleName(protocolModule) != null, "provider:[{}] does not exist in the protocol manager", provider);
|
||||
AssertionUtils.isTrue(ProtocolManager.moduleByModuleName(protocolModule) != null, "protocol module [{}] does not exist in the protocol manager", provider);
|
||||
AssertionUtils.isTrue(providerSet.add(providerName), "provider:[{}] has duplicate provider name module [provider:{}]", provider, protocolModule);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
package com.zfoo.net.config.model;
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -67,4 +69,9 @@ public class ProviderModule {
|
||||
public int hashCode() {
|
||||
return Objects.hash(protocolModule, provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return StringUtils.format("[{}-{}]", protocolModule, provider);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -48,8 +48,8 @@ public class CachedConsistentHashLoadBalancer extends AbstractConsumerLoadBalanc
|
||||
|
||||
var arg = (Number) argument;
|
||||
var protocolModuleId = (long) ProtocolManager.moduleByProtocol(packet.getClass()).getId();
|
||||
// 8 Byte cachedKey = 1 byte of protocolModuleId + 7 byte of argument
|
||||
var cachedKey = (protocolModuleId << (7 * 8)) | (0X00FFFFFF_FFFFFFFFL & arg.longValue());
|
||||
// 8 Byte cachedKey = 7 byte of argument + 1 byte of protocolModuleId
|
||||
var cachedKey = arg.longValue() << 8 | protocolModuleId;
|
||||
var sid = cache.getIfPresent(cachedKey);
|
||||
if (sid == null) {
|
||||
var providerSession = ConsistentHashLoadBalancer.getInstance().selectProvider(providers, packet, argument);
|
||||
|
||||
@@ -366,7 +366,7 @@ public class ZookeeperRegistry implements IRegistry {
|
||||
var localProviderPath = PROVIDER_ROOT_PATH + StringUtils.SLASH + localProviderVoStr;
|
||||
|
||||
// /zfoo/provider
|
||||
// applicationNameTest | 192.168.1.104:12400 | provider:[providerTest-myProvider1, providerTest-myProvider2]
|
||||
// applicationNameTest | 192.168.1.104:12400 | provider:[myProviderModule-provider1, myProviderModule-provider2]
|
||||
var localProviderStat = curator.checkExists().forPath(localProviderPath);
|
||||
if (Objects.isNull(localProviderStat)) {
|
||||
curator.create()
|
||||
@@ -383,7 +383,7 @@ public class ZookeeperRegistry implements IRegistry {
|
||||
.deletingChildrenIfNeeded()
|
||||
.withVersion(localProviderStat.getVersion())
|
||||
.forPath(localProviderPath);
|
||||
throw new RuntimeException(StringUtils.format("session of curator[sessionId:{}] and providerNode[sessionId:{}] can not match"
|
||||
throw new RuntimeException(StringUtils.format("session of curator[sessionId:{}] and providerNode[sessionId:{}] can not match, delete old old session data"
|
||||
, curatorSessionId, providerNodeSessionId));
|
||||
}
|
||||
}
|
||||
@@ -397,7 +397,7 @@ public class ZookeeperRegistry implements IRegistry {
|
||||
* @throws Exception
|
||||
*/
|
||||
private void initConsumerCache() throws Exception {
|
||||
// applicationNameTest | 192.168.1.104:12400 | provider:[providerTest-myProvider1, providerTest-myProvider2]
|
||||
// /zfoo/provider/applicationNameTest | 192.168.1.104:12400 | provider:[myProviderModule-provider1, myProviderModule-provider2]
|
||||
var localRegisterVO = NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO();
|
||||
// 初始化providerCacheSet
|
||||
// 遍历provider下注册的所有节点
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
<protocol id="2072" location="com.zfoo.net.packet.websocket.WebSocketObjectB" enhance="false"/>
|
||||
</module>
|
||||
|
||||
<module id="5" name="providerTest">
|
||||
<module id="5" name="myProviderModule">
|
||||
<protocol id="4000" location="com.zfoo.net.packet.provider.ProviderMessAsk" enhance="false"/>
|
||||
<protocol id="4001" location="com.zfoo.net.packet.provider.ProviderMessAnswer" enhance="false"/>
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
</net:registry>
|
||||
|
||||
<net:consumers>
|
||||
<net:consumer load-balancer="consistent-hash" consumer="myProvider1"/>
|
||||
<net:consumer load-balancer="cached-consistent-hash" consumer="provider1"/>
|
||||
</net:consumers>
|
||||
</net:config>
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
</net:registry>
|
||||
|
||||
<net:consumers>
|
||||
<net:consumer load-balancer="cached-consistent-hash" consumer="myProvider1"/>
|
||||
<net:consumer load-balancer="consistent-hash" consumer="provider1"/>
|
||||
</net:consumers>
|
||||
</net:config>
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
</net:registry>
|
||||
|
||||
<net:consumers>
|
||||
<net:consumer load-balancer="random" consumer="myProvider1"/>
|
||||
<net:consumer load-balancer="random" consumer="provider1"/>
|
||||
</net:consumers>
|
||||
</net:config>
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
|
||||
<!--1.这里声明自己是服务提供者 2.提供接口是providerTest模块下的接口 3.提供者的名字是myProvider1-->
|
||||
<net:providers>
|
||||
<net:provider protocol-module="providerTest" provider="myProvider1"/>
|
||||
<net:provider protocol-module="providerTest" provider="myProvider2"/>
|
||||
<net:provider protocol-module="myProviderModule" provider="provider1"/>
|
||||
<net:provider protocol-module="myProviderModule" provider="provider2"/>
|
||||
</net:providers>
|
||||
</net:config>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user