Search in sources :

Example 91 with MicroserviceInstance

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

the class AbstractEndpointDiscoveryFilter method createDiscoveryTreeNode.

@SuppressWarnings("unchecked")
protected DiscoveryTreeNode createDiscoveryTreeNode(String expectTransportName, DiscoveryContext context, DiscoveryTreeNode parent) {
    List<Object> endpoints = new ArrayList<>();
    for (MicroserviceInstance instance : ((Map<String, MicroserviceInstance>) parent.data()).values()) {
        for (String endpoint : instance.getEndpoints()) {
            try {
                URI uri = URI.create(endpoint);
                String transportName = uri.getScheme();
                if (!isTransportNameMatch(transportName, expectTransportName)) {
                    continue;
                }
                Object objEndpoint = createEndpoint(context, transportName, endpoint, instance);
                if (objEndpoint == null) {
                    continue;
                }
                endpoints.add(objEndpoint);
            } catch (Exception e) {
                LOGGER.warn("unrecognized address find, ignore {}.", endpoint);
            }
        }
    }
    return new DiscoveryTreeNode().subName(parent, expectTransportName).data(endpoints);
}
Also used : ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Map(java.util.Map) URI(java.net.URI)

Example 92 with MicroserviceInstance

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

the class GateRestTemplate method getUrlPrefix.

private String getUrlPrefix(String gateName, String producerName, String schemaId) {
    MicroserviceVersionRule microserviceVersionRule = DiscoveryManager.INSTANCE.getAppManager().getOrCreateMicroserviceVersionRule(RegistrationManager.INSTANCE.getMicroservice().getAppId(), gateName, DefinitionConst.VERSION_RULE_ALL);
    MicroserviceInstance microserviceInstance = microserviceVersionRule.getInstances().values().stream().findFirst().get();
    URIEndpointObject edgeAddress = new URIEndpointObject(microserviceInstance.getEndpoints().get(0));
    String urlSchema = "http";
    if (edgeAddress.isSslEnabled()) {
        urlSchema = "https";
    }
    MicroserviceReferenceConfig microserviceReferenceConfig = SCBEngine.getInstance().createMicroserviceReferenceConfig(producerName);
    MicroserviceMeta microserviceMeta = microserviceReferenceConfig.getLatestMicroserviceMeta();
    SchemaMeta schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId);
    return String.format("%s://%s:%d/rest/%s%s", urlSchema, edgeAddress.getHostOrIp(), edgeAddress.getPort(), producerName, schemaMeta.getSwagger().getBasePath());
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) MicroserviceReferenceConfig(org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 93 with MicroserviceInstance

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

the class TestSimpleLoadBalanceHandler method handle.

@Test
public void handle(@Mocked Transport transport) throws Exception {
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("id");
    instance.getEndpoints().add("rest://localhost:8080");
    instance.getEndpoints().add("highway://localhost:8081");
    instanceVersionedCache.data(Collections.singletonMap("id", instance)).autoCacheVersion().name("vr");
    new Expectations(scbEngine.getTransportManager()) {

        {
            SCBEngine.getInstance().getTransportManager().findTransport(anyString);
            result = transport;
            invocation.getConfigTransportName();
            result = "";
        }
    };
    handler.handle(invocation, ar);
    AtomicInteger idx = indexMap.values().iterator().next();
    Assert.assertEquals(1, idx.get());
    handler.handle(invocation, ar);
    Assert.assertEquals(2, idx.get());
}
Also used : Expectations(mockit.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 94 with MicroserviceInstance

use of org.apache.servicecomb.registry.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");
            @SuppressWarnings("unchecked") List<String> schemas = (List<String>) serviceConfig.get("schemaIds");
            Microservice microservice = new Microservice();
            microservice.setAppId(appId == null ? BootStrapProperties.DEFAULT_APPLICATION : appId);
            microservice.setServiceName(name);
            microservice.setVersion(version);
            microservice.setServiceId(serviceId == null ? UUID.randomUUID().toString() : serviceId);
            microserviceIdMap.put(microservice.getServiceId(), microservice);
            if (schemas != null) {
                microservice.setSchemas(schemas);
            }
            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.registry.api.registry.MicroserviceInstance) Microservice(org.apache.servicecomb.registry.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 95 with MicroserviceInstance

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

the class LocalServiceRegistryClientImpl method updateMicroserviceInstanceStatus.

@Override
public boolean updateMicroserviceInstanceStatus(String microserviceId, String instanceId, MicroserviceInstanceStatus status) {
    if (null == status) {
        throw new IllegalArgumentException("null status is now allowed");
    }
    Map<String, MicroserviceInstance> instanceMap = microserviceInstanceMap.get(microserviceId);
    if (instanceMap == null) {
        throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
    }
    MicroserviceInstance microserviceInstance = instanceMap.get(instanceId);
    if (microserviceInstance == null) {
        throw new IllegalArgumentException(String.format("Invalid argument. microserviceId=%s, instanceId=%s.", microserviceId, instanceId));
    }
    microserviceInstance.setStatus(status);
    return true;
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12