Nacos注册中心——ClientEvent.ClientChangedEvent

王守钰 2021-10-11 15:10:50

DistroClientComponentRegistry

@Component
public class DistroClientComponentRegistry {
    
    private final ServerMemberManager serverMemberManager;
    
    private final DistroProtocol distroProtocol;
    
    private final DistroComponentHolder componentHolder;
    
    private final DistroTaskEngineHolder taskEngineHolder;
    
    private final ClientManager clientManager;
    
    private final ClusterRpcClientProxy clusterRpcClientProxy;
    
    private final UpgradeJudgement upgradeJudgement;
    
    public DistroClientComponentRegistry(ServerMemberManager serverMemberManager, DistroProtocol distroProtocol,
            DistroComponentHolder componentHolder, DistroTaskEngineHolder taskEngineHolder,
            ClientManagerDelegate clientManager, ClusterRpcClientProxy clusterRpcClientProxy,
            UpgradeJudgement upgradeJudgement) {
        this.serverMemberManager = serverMemberManager;
        this.distroProtocol = distroProtocol;
        this.componentHolder = componentHolder;
        this.taskEngineHolder = taskEngineHolder;
        this.clientManager = clientManager;
        this.clusterRpcClientProxy = clusterRpcClientProxy;
        this.upgradeJudgement = upgradeJudgement;
    }
    
    /**
     * Register necessary component to distro protocol for v2 {@link com.alibaba.nacos.naming.core.v2.client.Client}
     * implement.
     */
    @PostConstruct
    public void doRegister() {
        DistroClientDataProcessor dataProcessor = new DistroClientDataProcessor(clientManager, distroProtocol,
                upgradeJudgement);
        DistroTransportAgent transportAgent = new DistroClientTransportAgent(clusterRpcClientProxy,
                serverMemberManager);
        DistroClientTaskFailedHandler taskFailedHandler = new DistroClientTaskFailedHandler(taskEngineHolder);
        componentHolder.registerDataStorage(DistroClientDataProcessor.TYPE, dataProcessor);
        componentHolder.registerDataProcessor(dataProcessor);
        componentHolder.registerTransportAgent(DistroClientDataProcessor.TYPE, transportAgent);
        componentHolder.registerFailedTaskHandler(DistroClientDataProcessor.TYPE, taskFailedHandler);
    }
}
public DistroClientDataProcessor(ClientManager clientManager, DistroProtocol distroProtocol,
        UpgradeJudgement upgradeJudgement) {
    this.clientManager = clientManager;
    this.distroProtocol = distroProtocol;
    this.upgradeJudgement = upgradeJudgement;
    NotifyCenter.registerSubscriber(this, NamingEventPublisherFactory.getInstance());
}

当容器启动时,DistroClientComponentRegistry会进行实例化,然后调用doRegister方法,doRegister方法会去实例化DistroClientDataProcessor,在实例化DistroClientDataProcessor过程中会注册订阅者。这时候也就建立了ClientEvent.ClientChangedEvent事件和NamingEventPublisher发布者之间的关系。

onEvent

public void onEvent(Event event) {
    if (EnvUtil.getStandaloneMode()) {
        return;
    }
    if (!upgradeJudgement.isUseGrpcFeatures()) {
        return;
    }
    if (event instanceof ClientEvent.ClientVerifyFailedEvent) {
        syncToVerifyFailedServer((ClientEvent.ClientVerifyFailedEvent) event);
    } else {
        syncToAllServer((ClientEvent) event);
    }
}

当我们接收到ClientEvent.ClientChangedEvent事件的时候,系统会触发进入到onEvent方法中,因为我们现在调试的环境是standalone模式,所以暂时不会执行下面方法。