Search in sources :

Example 66 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class PersistentClientOperationServiceImpl method registerInstance.

@Override
public void registerInstance(Service service, Instance instance, String clientId) {
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    if (singleton.isEphemeral()) {
        throw new NacosRuntimeException(NacosException.INVALID_PARAM, String.format("Current service %s is ephemeral service, can't register persistent instance.", singleton.getGroupedServiceName()));
    }
    final InstanceStoreRequest request = new InstanceStoreRequest();
    request.setService(service);
    request.setInstance(instance);
    request.setClientId(clientId);
    final WriteRequest writeRequest = WriteRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(serializer.serialize(request))).setOperation(DataOperation.ADD.name()).build();
    try {
        protocol.write(writeRequest);
    } catch (Exception e) {
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
    }
}
Also used : WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) IOException(java.io.IOException)

Example 67 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class PersistentClientOperationServiceImpl method onInstanceRegister.

private void onInstanceRegister(Service service, Instance instance, String clientId) {
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    if (!clientManager.contains(clientId)) {
        clientManager.clientConnected(clientId, new ClientAttributes());
    }
    Client client = clientManager.getClient(clientId);
    InstancePublishInfo instancePublishInfo = getPublishInfo(instance);
    client.addServiceInstance(singleton, instancePublishInfo);
    client.setLastUpdatedTime();
    NotifyCenter.publishEvent(new ClientOperationEvent.ClientRegisterServiceEvent(singleton, clientId));
}
Also used : ClientOperationEvent(com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) Client(com.alibaba.nacos.naming.core.v2.client.Client) ClientAttributes(com.alibaba.nacos.naming.core.v2.client.ClientAttributes)

Example 68 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceStorage method getAllInstancesFromIndex.

private List<Instance> getAllInstancesFromIndex(Service service) {
    Set<Instance> result = new HashSet<>();
    Set<String> clusters = new HashSet<>();
    for (String each : serviceIndexesManager.getAllClientsRegisteredService(service)) {
        Optional<InstancePublishInfo> instancePublishInfo = getInstanceInfo(each, service);
        if (instancePublishInfo.isPresent()) {
            Instance instance = parseInstance(service, instancePublishInfo.get());
            result.add(instance);
            clusters.add(instance.getClusterName());
        }
    }
    // cache clusters of this service
    serviceClusterIndex.put(service, clusters);
    return new LinkedList<>(result);
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 69 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class HealthOperatorV2Impl method updateHealthStatusForPersistentInstance.

@Override
public void updateHealthStatusForPersistentInstance(String namespace, String fullServiceName, String clusterName, String ip, int port, boolean healthy) throws NacosException {
    String groupName = NamingUtils.getGroupName(fullServiceName);
    String serviceName = NamingUtils.getServiceName(fullServiceName);
    Service service = Service.newService(namespace, groupName, serviceName);
    Optional<ServiceMetadata> serviceMetadata = metadataManager.getServiceMetadata(service);
    if (!serviceMetadata.isPresent() || !serviceMetadata.get().getClusters().containsKey(clusterName)) {
        throwHealthCheckerException(fullServiceName, clusterName);
    }
    ClusterMetadata clusterMetadata = serviceMetadata.get().getClusters().get(clusterName);
    if (!HealthCheckType.NONE.name().equals(clusterMetadata.getHealthyCheckType())) {
        throwHealthCheckerException(fullServiceName, clusterName);
    }
    String clientId = IpPortBasedClient.getClientId(ip + InternetAddressUtil.IP_PORT_SPLITER + port, false);
    Client client = clientManager.getClient(clientId);
    if (null == client) {
        return;
    }
    InstancePublishInfo oldInstance = client.getInstancePublishInfo(service);
    if (null == oldInstance) {
        return;
    }
    Instance newInstance = InstanceUtil.parseToApiInstance(service, oldInstance);
    newInstance.setHealthy(healthy);
    clientOperationService.registerInstance(service, newInstance, clientId);
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) Client(com.alibaba.nacos.naming.core.v2.client.Client) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Example 70 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class InstanceOperatorClientImpl method batchDeleteMetadata.

@Override
public List<String> batchDeleteMetadata(String namespaceId, InstanceOperationInfo instanceOperationInfo, Map<String, String> metadata) throws NacosException {
    boolean isEphemeral = !UtilsAndCommons.PERSIST.equals(instanceOperationInfo.getConsistencyType());
    String serviceName = instanceOperationInfo.getServiceName();
    Service service = getService(namespaceId, serviceName, isEphemeral);
    List<String> result = new LinkedList<>();
    List<Instance> needUpdateInstance = findBatchUpdateInstance(instanceOperationInfo, service);
    for (Instance each : needUpdateInstance) {
        String metadataId = InstancePublishInfo.genMetadataId(each.getIp(), each.getPort(), each.getClusterName());
        Optional<InstanceMetadata> instanceMetadata = metadataManager.getInstanceMetadata(service, metadataId);
        InstanceMetadata newMetadata = instanceMetadata.map(this::cloneMetadata).orElseGet(InstanceMetadata::new);
        metadata.keySet().forEach(key -> newMetadata.getExtendData().remove(key));
        metadataOperateService.updateInstanceMetadata(service, metadataId, newMetadata);
        result.add(each.toInetAddr() + ":" + UtilsAndCommons.LOCALHOST_SITE + ":" + each.getClusterName() + ":" + (each.isEphemeral() ? UtilsAndCommons.EPHEMERAL : UtilsAndCommons.PERSIST));
    }
    return result;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceMetadata(com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) LinkedList(java.util.LinkedList)

Aggregations

Service (com.alibaba.nacos.naming.core.v2.pojo.Service)75 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)20 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)19 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)17 Test (org.junit.Test)16 Instance (com.alibaba.nacos.api.naming.pojo.Instance)13 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)13 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)13 NacosException (com.alibaba.nacos.api.exception.NacosException)11 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)11 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)11 HashSet (java.util.HashSet)11 Secured (com.alibaba.nacos.auth.annotation.Secured)10 Before (org.junit.Before)10 Client (com.alibaba.nacos.naming.core.v2.client.Client)9 ClientOperationEvent (com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent)8 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)8 InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)8 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)7 Set (java.util.Set)7