Search in sources :

Example 1 with ClientSyncData

use of com.alibaba.nacos.naming.core.v2.client.ClientSyncData in project nacos by alibaba.

the class DistroClientDataProcessor method getDatumSnapshot.

@Override
public DistroData getDatumSnapshot() {
    List<ClientSyncData> datum = new LinkedList<>();
    for (String each : clientManager.allClientId()) {
        Client client = clientManager.getClient(each);
        if (null == client || !client.isEphemeral()) {
            continue;
        }
        datum.add(client.generateSyncData());
    }
    ClientSyncDatumSnapshot snapshot = new ClientSyncDatumSnapshot();
    snapshot.setClientSyncDataList(datum);
    byte[] data = ApplicationUtils.getBean(Serializer.class).serialize(snapshot);
    return new DistroData(new DistroKey(DataOperation.SNAPSHOT.name(), TYPE), data);
}
Also used : ClientSyncData(com.alibaba.nacos.naming.core.v2.client.ClientSyncData) ClientSyncDatumSnapshot(com.alibaba.nacos.naming.core.v2.client.ClientSyncDatumSnapshot) Client(com.alibaba.nacos.naming.core.v2.client.Client) DistroKey(com.alibaba.nacos.core.distributed.distro.entity.DistroKey) LinkedList(java.util.LinkedList) Serializer(com.alibaba.nacos.naming.cluster.transport.Serializer) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 2 with ClientSyncData

use of com.alibaba.nacos.naming.core.v2.client.ClientSyncData in project nacos by alibaba.

the class DistroClientDataProcessor method 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 client = clientManager.getClient(clientSyncData.getClientId());
    upgradeClient(client, clientSyncData);
}
Also used : Client(com.alibaba.nacos.naming.core.v2.client.Client)

Example 3 with ClientSyncData

use of com.alibaba.nacos.naming.core.v2.client.ClientSyncData in project nacos by alibaba.

the class DistroClientDataProcessor method upgradeClient.

private void upgradeClient(Client client, ClientSyncData clientSyncData) {
    List<String> namespaces = clientSyncData.getNamespaces();
    List<String> groupNames = clientSyncData.getGroupNames();
    List<String> serviceNames = clientSyncData.getServiceNames();
    List<InstancePublishInfo> instances = clientSyncData.getInstancePublishInfos();
    Set<Service> 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()));
        }
    }
}
Also used : ClientOperationEvent(com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) HashSet(java.util.HashSet)

Example 4 with ClientSyncData

use of com.alibaba.nacos.naming.core.v2.client.ClientSyncData in project nacos by alibaba.

the class DistroClientDataProcessor method 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;
    }
}
Also used : ClientSyncData(com.alibaba.nacos.naming.core.v2.client.ClientSyncData) Serializer(com.alibaba.nacos.naming.cluster.transport.Serializer)

Aggregations

Serializer (com.alibaba.nacos.naming.cluster.transport.Serializer)2 Client (com.alibaba.nacos.naming.core.v2.client.Client)2 ClientSyncData (com.alibaba.nacos.naming.core.v2.client.ClientSyncData)2 DistroData (com.alibaba.nacos.core.distributed.distro.entity.DistroData)1 DistroKey (com.alibaba.nacos.core.distributed.distro.entity.DistroKey)1 ClientSyncDatumSnapshot (com.alibaba.nacos.naming.core.v2.client.ClientSyncDatumSnapshot)1 ClientOperationEvent (com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent)1 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)1 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1