Search in sources :

Example 1 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
    // must register self first, and then invoke findServiceInstances
    if (consumerId == null) {
        LOGGER.error("find microservice instance {}/{}/{} failed, not registered to serviceCenter.", appId, serviceName, versionRule);
        return null;
    }
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RequestParam requestParam = new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("version", versionRule).addHeader("X-ConsumerId", consumerId);
    if (revision != null) {
        requestParam.addQueryParam("rev", revision);
    }
    RestUtils.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances));
    try {
        countDownLatch.await();
        if (!microserviceInstances.isNeedRefresh()) {
            return microserviceInstances;
        }
        if (microserviceInstances.getInstancesResponse() == null) {
            // error
            return null;
        }
        List<MicroserviceInstance> list = microserviceInstances.getInstancesResponse().getInstances();
        if (list == null) {
            microserviceInstances.getInstancesResponse().setInstances(new ArrayList<>());
        }
        return microserviceInstances;
    } catch (Exception e) {
        LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
    }
    return null;
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 2 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class LocalServiceRegistryClientImpl method updateInstanceProperties.

@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId, Map<String, String> instanceProperties) {
    Map<String, MicroserviceInstance> instanceMap = microserviceInstanceMap.get(microserviceId);
    if (instanceMap == null) {
        throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
    }
    MicroserviceInstance microserviceInstance = instanceMap.get(microserviceInstanceId);
    if (microserviceInstance == null) {
        throw new IllegalArgumentException(String.format("Invalid argument. microserviceId=%s, microserviceInstanceId=%s.", microserviceId, microserviceInstanceId));
    }
    if (instanceProperties != null) {
        microserviceInstance.getProperties().putAll(instanceProperties);
    }
    return true;
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 3 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class LocalServiceRegistryClientImpl method initFromData.

private void initFromData(Map<String, Object> data) {
    for (Entry<String, Object> entry : data.entrySet()) {
        String name = entry.getKey();
        @SuppressWarnings("unchecked") List<Map<String, Object>> serviceConfigs = (List<Map<String, Object>>) entry.getValue();
        for (Map<String, Object> serviceConfig : serviceConfigs) {
            @SuppressWarnings("unchecked") List<Map<String, Object>> instancesConfig = (List<Map<String, Object>>) serviceConfig.get("instances");
            String appId = (String) serviceConfig.get("appid");
            String version = (String) serviceConfig.get("version");
            String serviceId = (String) serviceConfig.get("id");
            Microservice microservice = new Microservice();
            microservice.setAppId(appId == null ? DEFAULT_APPLICATION_ID : appId);
            microservice.setServiceName(name);
            microservice.setVersion(version);
            microservice.setServiceId(serviceId == null ? UUID.randomUUID().toString() : serviceId);
            microserviceIdMap.put(microservice.getServiceId(), microservice);
            Map<String, MicroserviceInstance> instanceMap = new ConcurrentHashMap<>();
            for (Map<String, Object> instanceConfig : instancesConfig) {
                @SuppressWarnings("unchecked") List<String> endpoints = (List<String>) instanceConfig.get("endpoints");
                MicroserviceInstance instance = new MicroserviceInstance();
                instance.setInstanceId(UUID.randomUUID().toString());
                instance.setEndpoints(endpoints);
                instance.setServiceId(microservice.getServiceId());
                instanceMap.put(instance.getInstanceId(), instance);
            }
            microserviceInstanceMap.put(microservice.getServiceId(), instanceMap);
        }
    }
    if (!data.isEmpty()) {
        revision.incrementAndGet();
    }
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class MicroserviceVersions method pullInstances.

public void pullInstances() {
    if (pendingPullCount.decrementAndGet() != 0) {
        return;
    }
    MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances(appId, microserviceName, DefinitionConst.VERSION_RULE_ALL, revision);
    if (microserviceInstances == null) {
        return;
    }
    if (!microserviceInstances.isNeedRefresh()) {
        return;
    }
    List<MicroserviceInstance> pulledInstances = microserviceInstances.getInstancesResponse().getInstances();
    String rev = microserviceInstances.getRevision();
    safeSetInstances(pulledInstances, rev);
}
Also used : MicroserviceInstances(org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 5 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class InstanceCache method createTransportMap.

protected Map<String, List<CacheEndpoint>> createTransportMap() {
    Map<String, List<CacheEndpoint>> transportMap = new HashMap<>();
    for (MicroserviceInstance instance : instanceMap.values()) {
        // 过滤到不可用实例
        if (instance.getStatus() != MicroserviceInstanceStatus.UP) {
            continue;
        }
        for (String endpoint : instance.getEndpoints()) {
            try {
                URI uri = URI.create(endpoint);
                String transportName = uri.getScheme();
                List<CacheEndpoint> cacheEndpointList = transportMap.computeIfAbsent(transportName, k -> new ArrayList<>());
                cacheEndpointList.add(new CacheEndpoint(endpoint, instance));
            } catch (Exception e) {
                LOGGER.warn("unrecognized address find, ignore " + endpoint);
            }
        }
    }
    return transportMap;
}
Also used : HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) List(java.util.List) ArrayList(java.util.ArrayList) URI(java.net.URI)

Aggregations

MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)56 Test (org.junit.Test)30 Expectations (mockit.Expectations)16 ArrayList (java.util.ArrayList)15 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)15 HashMap (java.util.HashMap)11 MicroserviceInstances (org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances)7 Before (org.junit.Before)5 EventBus (com.google.common.eventbus.EventBus)4 Server (com.netflix.loadbalancer.Server)4 BootEvent (org.apache.servicecomb.core.BootListener.BootEvent)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4 Subscribe (com.google.common.eventbus.Subscribe)3 Map (java.util.Map)3 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)3 MicroserviceInstanceRegisterTask (org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask)3 URI (java.net.URI)2 List (java.util.List)2 RSAProviderTokenManager (org.apache.servicecomb.authentication.provider.RSAProviderTokenManager)2 SchemaListenerManager (org.apache.servicecomb.core.definition.loader.SchemaListenerManager)2