Search in sources :

Example 36 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestLocalServiceRegistry method testSchema.

@Test
public void testSchema() {
    ServiceRegistry serviceRegistry = LocalServiceRegistryFactory.createLocal();
    Microservice microservice = serviceRegistry.getMicroservice();
    microservice.addSchema("s1", "s1-content");
    serviceRegistry.init();
    serviceRegistry.run();
    try {
        serviceRegistry.getServiceRegistryClient().isSchemaExist("notExist", "s1");
        Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("Invalid serviceId, serviceId=notExist", e.getMessage());
    }
    try {
        serviceRegistry.getServiceRegistryClient().getSchema("notExist", "s1");
        Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("Invalid serviceId, serviceId=notExist", e.getMessage());
    }
    Assert.assertEquals(true, serviceRegistry.getServiceRegistryClient().isSchemaExist(microservice.getServiceId(), "s1"));
    String content = serviceRegistry.getServiceRegistryClient().getSchema(microservice.getServiceId(), "s1");
    Assert.assertEquals("s1-content", content);
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 37 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestLocalServiceRegistry method testUpdateProperties.

@Test
public void testUpdateProperties() {
    ServiceRegistry serviceRegistry = LocalServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    serviceRegistry.run();
    Microservice microservice = serviceRegistry.getMicroservice();
    Map<String, String> properties = new HashMap<>();
    properties.put("k", "v");
    try {
        serviceRegistry.getServiceRegistryClient().updateInstanceProperties(microservice.getServiceId(), "notExist", properties);
        Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("Invalid argument. microserviceId=" + microservice.getServiceId() + ", microserviceInstanceId=notExist.", e.getMessage());
    }
    serviceRegistry.updateMicroserviceProperties(properties);
    Assert.assertEquals(properties, microservice.getProperties());
    serviceRegistry.updateInstanceProperties(properties);
    Assert.assertEquals(properties, microservice.getInstance().getProperties());
    properties.put("k1", "v1");
    serviceRegistry.updateMicroserviceProperties(properties);
    Assert.assertEquals(properties, microservice.getProperties());
    serviceRegistry.updateInstanceProperties(properties);
    Assert.assertEquals(properties, microservice.getInstance().getProperties());
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) HashMap(java.util.HashMap) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 38 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class RefreshableServiceRegistryCacheTest method setUp.

@Before
public void setUp() throws Exception {
    serviceRegistryCache = new RefreshableServiceRegistryCache(consumerService, null) {

        @Override
        RefreshableMicroserviceCache createMicroserviceCache(MicroserviceCacheKey microserviceCacheKey) {
            return new RefreshableMicroserviceCache(consumerService, microserviceCacheKey, null, false) {

                @Override
                MicroserviceInstances pullInstanceFromServiceCenter(String revisionId) {
                    return pullInstanceFromServiceCenterLogic.value.apply(revisionId);
                }
            };
        }
    };
    consumerService = new Microservice();
    consumerService.setServiceId("testConsumer");
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) Before(org.junit.Before)

Example 39 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

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 40 with Microservice

use of org.apache.servicecomb.registry.api.registry.Microservice in project java-chassis by ServiceComb.

the class LocalServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String selfMicroserviceId, String appId, String serviceName, String strVersionRule, String revision) {
    int currentRevision = this.revision.get();
    List<MicroserviceInstance> allInstances = new ArrayList<>();
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    FindInstancesResponse response = new FindInstancesResponse();
    if (revision != null && currentRevision == Integer.parseInt(revision)) {
        microserviceInstances.setNeedRefresh(false);
        return microserviceInstances;
    }
    microserviceInstances.setRevision(String.valueOf(currentRevision));
    VersionRule versionRule = VersionRuleUtils.getOrCreate(strVersionRule);
    Microservice latestMicroservice = findLatest(appId, serviceName, versionRule);
    if (latestMicroservice == null) {
        microserviceInstances.setMicroserviceNotExist(true);
        return microserviceInstances;
    }
    Version latestVersion = VersionUtils.getOrCreate(latestMicroservice.getVersion());
    for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
        Microservice microservice = entry.getValue();
        if (!isSameMicroservice(microservice, appId, serviceName)) {
            continue;
        }
        Version version = VersionUtils.getOrCreate(entry.getValue().getVersion());
        if (!versionRule.isMatch(version, latestVersion)) {
            continue;
        }
        Map<String, MicroserviceInstance> instances = microserviceInstanceMap.get(entry.getValue().getServiceId());
        allInstances.addAll(instances.values());
    }
    response.setInstances(allInstances);
    microserviceInstances.setInstancesResponse(response);
    return microserviceInstances;
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) VersionRule(org.apache.servicecomb.registry.version.VersionRule)

Aggregations

Microservice (org.apache.servicecomb.registry.api.registry.Microservice)86 Test (org.junit.Test)53 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)20 Expectations (mockit.Expectations)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 GetSchemaResponse (org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse)10 Holder (org.apache.servicecomb.serviceregistry.client.http.Holder)10 HashMap (java.util.HashMap)9 MicroserviceFactory (org.apache.servicecomb.registry.api.registry.MicroserviceFactory)9 Swagger (io.swagger.models.Swagger)8 Before (org.junit.Before)8 MockUp (mockit.MockUp)6 Configuration (org.apache.commons.configuration.Configuration)6 AccessController (org.apache.servicecomb.authentication.provider.AccessController)6 Subscribe (com.google.common.eventbus.Subscribe)5 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)5 EventBus (com.google.common.eventbus.EventBus)4 Version (org.apache.servicecomb.foundation.common.Version)4 HashSet (java.util.HashSet)3