nacos源码分析

D:\newWorkSpace\nacos-2.0.0-bugfix\naming\src\main\java\com\alibaba\nacos\naming\consistency\ephemeral\distro\DistroConsistencyServiceImpl.java


/**

*一种称为Distro的一致性协议算法

*<p>使用distro算法将数据划分为多个块。每个Nacos服务器节点负责

*一块数据。每个数据块都由其负责的服务器生成、删除和同步。所以每

*Nacos服务器只处理总服务数据的一个子集的写入。

*同时每个Nacos服务器都接收到其他Nacos服务器的数据同步,所以每个Nacos服务器最终都会有一套完整的数据。

*/

@Override
public void put(String key, Record value) throws NacosException {
onPut(key, value);
// If upgrade to 2.0.X, do not sync for v1.
if (ApplicationUtils.getBean(UpgradeJudgement.class).isUseGrpcFeatures()) {
return;
}
distroProtocol.sync(new DistroKey(key, KeyBuilder.INSTANCE_LIST_KEY_PREFIX), DataOperation.CHANGE,
globalConfig.getTaskDispatchPeriod() / 2);
}

 


D:\newWorkSpace\nacos-2.0.0-bugfix\core\src\main\java\com\alibaba\nacos\core\distributed\distro\DistroProtocol.java
/**
* Start to sync data to all remote server.
* 开始将数据同步到所有远程服务器
* @param distroKey distro key of sync data
* @param action the action of data operation
* @param delay delay time for sync
*/
public void sync(DistroKey distroKey, DataOperation action, long delay) {
for (Member each : memberManager.allMembersWithoutSelf()) {
syncToTarget(distroKey, action, each.getAddress(), delay);
}
}

 

 

服务注册

D:\newWorkSpace\nacos-2.0.0-bugfix\naming\src\main\java\com\alibaba\nacos\naming\core\v2\upgrade\doublewrite\execute\DoubleWriteInstanceChangeToV2Task.java
instanceOperator.registerInstance(namespace, serviceName, instance);
D:\newWorkSpace\nacos-2.0.0-bugfix\naming\src\main\java\com\alibaba\nacos\naming\core\InstanceOperatorClientImpl.java
/**
* This method creates {@code IpPortBasedClient} if it don‘t exist.
*/
@Override
public void registerInstance(String namespaceId, String serviceName, Instance instance) {
boolean ephemeral = instance.isEphemeral();//Ephemeral 短暂的,临时的
String clientId = IpPortBasedClient.getClientId(instance.toInetAddr(), ephemeral);
createIpPortClientIfAbsent(clientId, ephemeral);
Service service = getService(namespaceId, serviceName, ephemeral);
clientOperationService.registerInstance(service, instance, clientId);
}
D:\newWorkSpace\nacos-2.0.0-bugfix\naming\src\main\java\com\alibaba\nacos\naming\core\v2\service\impl\EphemeralClientOperationServiceImpl.java
@Override
public void registerInstance(Service service, Instance instance, String clientId) {
Service singleton = ServiceManager.getInstance().getSingleton(service);
Client client = clientManager.getClient(clientId);
InstancePublishInfo instanceInfo = getPublishInfo(instance);
client.addServiceInstance(singleton, instanceInfo);
client.setLastUpdatedTime();
NotifyCenter.publishEvent(new ClientOperationEvent.ClientRegisterServiceEvent(singleton, clientId));
NotifyCenter
.publishEvent(new MetadataEvent.InstanceMetadataEvent(singleton, instanceInfo.getMetadataId(), false));
}

 


D:\newWorkSpace\nacos-2.0.0-bugfix\naming\src\main\java\com\alibaba\nacos\naming\core\ServiceManager.java

nacos源码分析

上一篇:Smartbi:大数据平台架构设计系统


下一篇:Sonar-Qube 生成PDF报告发送指定邮箱