网站建设费摊销,wordpress怎么放音乐,茶叶品质网站建设,手机软件制作器下载Nacos集群数据同步
当我们有服务进行注册以后#xff0c;会写入注册信息同时会触发ClientChangedEvent事件#xff0c;通过这个事件#xff0c;就会开始进行Nacos的集群数据同步#xff0c;当然这其中只有有一个Nacos节点来处理对应的客户端请求#xff0c;其实这其中…Nacos集群数据同步
当我们有服务进行注册以后会写入注册信息同时会触发ClientChangedEvent事件通过这个事件就会开始进行Nacos的集群数据同步当然这其中只有有一个Nacos节点来处理对应的客户端请求其实这其中还涉及到一个负责节点和非负责节点
负责节点
这是首先我们要查看的是DistroClientDataProcessor客户端数据一致性处理器类型这个类型会处理当前节点负责的Client那我们要查看其中的syncToAllServer方法。
private void syncToAllServer(ClientEvent event) {Client client event.getClient();// 判断客户端是否为空是否是临时实例判断是否是负责节点if (null client || !client.isEphemeral() || !clientManager.isResponsibleClient(client)) {return;}if (event instanceof ClientEvent.ClientDisconnectEvent) {// 客户端断开连接DistroKey distroKey new DistroKey(client.getClientId(), TYPE);distroProtocol.sync(distroKey, DataOperation.DELETE);} else if (event instanceof ClientEvent.ClientChangedEvent) {// 客户端新增/修改DistroKey distroKey new DistroKey(client.getClientId(), TYPE);distroProtocol.sync(distroKey, DataOperation.CHANGE);}
} distroProtocol会循环所有其他nacos节点提交一个异步任务这个异步任务会延迟1s其实这里我们就可以看到这里涉及到客户端的断开和客户端的新增和修改对于Delete操作由DistroSyncDeleteTask处理对于Change操作由DistroSyncChangeTask处理这里我们从DistroSyncChangeTask来看
public class DistroSyncChangeTask extends AbstractDistroExecuteTask {private static final DataOperation OPERATION DataOperation.CHANGE;public DistroSyncChangeTask(DistroKey distroKey, DistroComponentHolder distroComponentHolder) {super(distroKey, distroComponentHolder);}Overrideprotected DataOperation getDataOperation() {return OPERATION;}// 无回调Overrideprotected boolean doExecute() {String type getDistroKey().getResourceType();DistroData distroData getDistroData(type);if (null distroData) {Loggers.DISTRO.warn([DISTRO] {} with null data to sync, skip, toString());return true;}return getDistroComponentHolder().findTransportAgent(type).syncData(distroData, getDistroKey().getTargetServer());}// 有回调Overrideprotected void doExecuteWithCallback(DistroCallback callback) {String type getDistroKey().getResourceType();DistroData distroData getDistroData(type);if (null distroData) {Loggers.DISTRO.warn([DISTRO] {} with null data to sync, skip, toString());return;}getDistroComponentHolder().findTransportAgent(type).syncData(distroData, getDistroKey().getTargetServer(), callback);}Overridepublic String toString() {return DistroSyncChangeTask for getDistroKey().toString();}// 从DistroClientDataProcessor获取DistroDataprivate DistroData getDistroData(String type) {DistroData result getDistroComponentHolder().findDataStorage(type).getDistroData(getDistroKey());if (null ! result) {result.setType(OPERATION);}return result;}
} 获取到的DistroData其实是从ClientManager实时获取Client。
// DistroClientDataProcessor
Override
public DistroData getDistroData(DistroKey distroKey) {Client client clientManager.getClient(distroKey.getResourceKey());if (null client) {return null;}// 把生成的同步数据放入到数组中byte[] data ApplicationUtils.getBean(Serializer.class).serialize(client.generateSyncData());return new DistroData(distroKey, data);
} AbstractClient继承了Client同时给DistroClientDataProcessorClient提供Client的注册信息包括客户端注册了哪些namespace哪些group哪些service哪些instance。
Override
public ClientSyncData generateSyncData() {ListString namespaces new LinkedList();ListString groupNames new LinkedList();ListString serviceNames new LinkedList();ListInstancePublishInfo instances new LinkedList();for (Map.EntryService, InstancePublishInfo entry : publishers.entrySet()) {namespaces.add(entry.getKey().getNamespace());groupNames.add(entry.getKey().getGroup());serviceNames.add(entry.getKey().getName());instances.add(entry.getValue());}return new ClientSyncData(getClientId(), namespaces, groupNames, serviceNames, instances);
} 这里我们在回过头来看syncData方法这个方法实际上是由DistroClientTransportAgent封装为DistroDataRequest调用其他Nacos节点。
Override
public boolean syncData(DistroData data, String targetServer) {if (isNoExistTarget(targetServer)) {return true;}DistroDataRequest request new DistroDataRequest(data, data.getType());Member member memberManager.find(targetServer);if (checkTargetServerStatusUnhealthy(member)) {Loggers.DISTRO.warn([DISTRO] Cancel distro sync caused by target server {} unhealthy, targetServer);return false;}try {Response response clusterRpcClientProxy.sendRequest(member, request);return checkResponse(response);} catch (NacosException e) {Loggers.DISTRO.error([DISTRO-FAILED] Sync distro data failed! , e);}return false;
}非负责节点
当负责节点将数据发送给非负责节点以后将要处理发送过来的Client数据。这里我们要看DistroClientDataProcessor.processData方法
Override
public boolean processData(DistroData distroData) {switch (distroData.getType()) {case ADD:case CHANGE:ClientSyncData clientSyncData ApplicationUtils.getBean(Serializer.class).deserialize(distroData.getContent(), ClientSyncData.class);//处理同步数据handlerClientSyncData(clientSyncData);return true;case DELETE:String deleteClientId distroData.getDistroKey().getResourceKey();Loggers.DISTRO.info([Client-Delete] Received distro client sync data {}, deleteClientId);clientManager.clientDisconnected(deleteClientId);return true;default:return false;}
} 然后来查看具体处理方法handlerClientSyncData
private void handlerClientSyncData(ClientSyncData clientSyncData) {Loggers.DISTRO.info([Client-Add] Received distro client sync data {}, clientSyncData.getClientId());// 同步客户端连接clientManager.syncClientConnected(clientSyncData.getClientId(), clientSyncData.getAttributes());// 获取Client此时注册到的是ConnectionBasedClientClient client clientManager.getClient(clientSyncData.getClientId());// 更新Client数据upgradeClient(client, clientSyncData);
} DistroClientDataProcessor的upgradeClient方法更新Client里的注册表信息发布对应事件
private void upgradeClient(Client client, ClientSyncData clientSyncData) {ListString namespaces clientSyncData.getNamespaces();ListString groupNames clientSyncData.getGroupNames();ListString serviceNames clientSyncData.getServiceNames();ListInstancePublishInfo instances clientSyncData.getInstancePublishInfos();SetService syncedService new HashSet();for (int i 0; i namespaces.size(); i) {Service service Service.newService(namespaces.get(i), groupNames.get(i), serviceNames.get(i));Service singleton ServiceManager.getInstance().getSingleton(service);syncedService.add(singleton);InstancePublishInfo instancePublishInfo instances.get(i);if (!instancePublishInfo.equals(client.getInstancePublishInfo(singleton))) {client.addServiceInstance(singleton, instancePublishInfo);NotifyCenter.publishEvent(new ClientOperationEvent.ClientRegisterServiceEvent(singleton, client.getClientId()));}}for (Service each : client.getAllPublishedService()) {if (!syncedService.contains(each)) {client.removeServiceInstance(each);NotifyCenter.publishEvent(new ClientOperationEvent.ClientDeregisterServiceEvent(each, client.getClientId()));}}
} **注意**这里要注意下此时的Client实现类ConnectionBasedClient只不过它的isNative属性为false这是非负责节点和负责节点的主要区别。
其实判断当前nacos节点是否为负责节点的依据就是这个isNative属性如果是客户端直接注册在这个nacos节点上的ConnectionBasedClient它的isNative属性为true如果是由Distro协议同步到这个nacos节点上的ConnectionBasedClient它的isNative属性为false。
那其实我们都知道2.x的版本以后使用了长连接所以通过长连接建立在哪个节点上哪个节点就是责任节点客户端也只会向这个责任节点发送请求。
Distro协议负责集群数据统一
Distro为了确保集群间数据一致不仅仅依赖于数据发生改变时的实时同步后台有定时任务做数据同步。
在1.x版本中责任节点每5s同步所有Service的Instance列表的摘要md5给非责任节点非责任节点用对端传来的服务md5比对本地服务的md5如果发生改变需要反查责任节点。
在2.x版本中对这个流程做了改造责任节点会发送Client全量数据非责任节点定时检测同步过来的Client是否过期减少1.x版本中的反查。
责任节点每5s向其他节点发送DataOperationVERIFY类型的DistroData来维持非责任节点的Client数据不过期。
//DistroVerifyTimedTask
Override
public void run() {try {// 所有其他节点ListMember targetServer serverMemberManager.allMembersWithoutSelf();if (Loggers.DISTRO.isDebugEnabled()) {Loggers.DISTRO.debug(server list is: {}, targetServer);}for (String each : distroComponentHolder.getDataStorageTypes()) {// 遍历想这些节点发送Client.isNativetrue的DistroDatatype VERIFYverifyForDataStorage(each, targetServer);}} catch (Exception e) {Loggers.DISTRO.error([DISTRO-FAILED] verify task failed., e);}
} 非责任节点每5s扫描isNativefalse的client如果client30s内没有被VERIFY的DistroData更新过续租时间会删除这个同步过来的Client数据。
//ConnectionBasedClientManager-ExpiredClientCleaner
private static class ExpiredClientCleaner implements Runnable {private final ConnectionBasedClientManager clientManager;public ExpiredClientCleaner(ConnectionBasedClientManager clientManager) {this.clientManager clientManager;}Overridepublic void run() {long currentTime System.currentTimeMillis();for (String each : clientManager.allClientId()) {ConnectionBasedClient client (ConnectionBasedClient) clientManager.getClient(each);if (null ! client client.isExpire(currentTime)) {clientManager.clientDisconnected(each);}}}
}
-------------------------------------------------------------------------------------------
Override
public boolean isExpire(long currentTime) {// 判断30s内没有续租 认为过期return !isNative() currentTime - getLastRenewTime() ClientConfig.getInstance().getClientExpiredTime();
}
文章转载自: http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn