Search in sources :

Example 1 with Microservice

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

the class LocalServiceRegistryClientImpl method findLatest.

protected Microservice findLatest(String appId, String serviceName, VersionRule versionRule) {
    Version latestVersion = null;
    Microservice latest = null;
    for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
        Microservice microservice = entry.getValue();
        if (!isSameMicroservice(microservice, appId, serviceName)) {
            continue;
        }
        Version version = VersionUtils.getOrCreate(microservice.getVersion());
        if (!versionRule.isAccept(version)) {
            continue;
        }
        if (latestVersion == null || version.compareTo(latestVersion) > 0) {
            latestVersion = version;
            latest = microservice;
        }
    }
    return latest;
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) Version(org.apache.servicecomb.serviceregistry.version.Version)

Example 2 with Microservice

use of org.apache.servicecomb.serviceregistry.api.registry.Microservice 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 3 with Microservice

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

the class LocalServiceRegistryClientImpl method registerSchema.

@Override
public boolean registerSchema(String microserviceId, String schemaId, String schemaContent) {
    Microservice microservice = microserviceIdMap.get(microserviceId);
    if (microservice == null) {
        throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
    }
    microservice.getSchemaMap().put(schemaId, schemaContent);
    return true;
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice)

Example 4 with Microservice

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

the class TestConsumerSchemaFactory method init.

@BeforeClass
public static void init() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader() {

        @Override
        public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
        }
    };
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
    ReflectUtils.setField(consumerSchemaFactory, "microserviceMetaManager", microserviceMetaManager);
    ReflectUtils.setField(consumerSchemaFactory, "schemaLoader", schemaLoader);
    ReflectUtils.setField(consumerSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext);
    SchemaMeta schemaMeta = new UnitTestMeta().getOrCreateSchemaMeta(TestConsumerSchemaFactoryImpl.class);
    String content = UnitTestSwaggerUtils.pretty(schemaMeta.getSwagger());
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceId("0");
    microservice.setServiceName("ms");
    microservice.setVersion("1.0.0");
    microservice.addSchema("schema", content);
    serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setServiceId("0");
    instance.setInstanceId("0");
    serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
}
Also used : MicroserviceMetaManager(org.apache.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(org.apache.servicecomb.core.definition.loader.SchemaLoader) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) SchemaListenerManager(org.apache.servicecomb.core.definition.loader.SchemaListenerManager) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) BeforeClass(org.junit.BeforeClass)

Example 5 with Microservice

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

the class TestMicroserviceVersionMeta method construct.

@Test
public void construct() {
    String microserviceName = "app:ms";
    String microserviceId = "id";
    Microservice microservice = new Microservice();
    microservice.setVersion("1.0.0");
    new Expectations(RegistryUtils.class) {

        {
            RegistryUtils.getMicroservice(microserviceId);
            result = microservice;
        }
    };
    List<String> logs = new ArrayList<>();
    CseContext.getInstance().setConsumerSchemaFactory(new MockUp<ConsumerSchemaFactory>() {

        @Mock
        void getOrCreateConsumerSchema(MicroserviceMeta microserviceMeta, Microservice microservice) {
            logs.add("getOrCreateConsumerSchema");
        }
    }.getMockInstance());
    CseContext.getInstance().setSchemaListenerManager(new MockUp<SchemaListenerManager>() {

        @Mock
        void notifySchemaListener(MicroserviceMeta... microserviceMetas) {
            logs.add("notifySchemaListener");
        }
    }.getMockInstance());
    MicroserviceVersionMeta microserviceVersionMeta = new MicroserviceVersionMeta(microserviceName, microserviceId, PrivateMicroserviceClassLoaderFactory.INSTANCE);
    Assert.assertThat(logs, Matchers.contains("getOrCreateConsumerSchema", "notifySchemaListener"));
    Assert.assertEquals(microserviceName, microserviceVersionMeta.getMicroserviceMeta().getName());
    Assert.assertThat(microserviceVersionMeta.getMicroserviceMeta().getClassLoader(), Matchers.instanceOf(MicroserviceClassLoader.class));
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) MicroserviceClassLoader(org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoader) ArrayList(java.util.ArrayList) MockUp(mockit.MockUp) Test(org.junit.Test)

Aggregations

Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)47 Test (org.junit.Test)24 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)15 Expectations (mockit.Expectations)11 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 EventBus (com.google.common.eventbus.EventBus)4 Before (org.junit.Before)4 Subscribe (com.google.common.eventbus.Subscribe)3 MockUp (mockit.MockUp)3 ServiceRegistry (org.apache.servicecomb.serviceregistry.ServiceRegistry)3 MicroserviceFactory (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory)3 MicroserviceInstances (org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances)3 Endpoint (org.apache.servicecomb.core.Endpoint)2 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)2 ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)2 Version (org.apache.servicecomb.serviceregistry.version.Version)2 VersionRule (org.apache.servicecomb.serviceregistry.version.VersionRule)2 Handler (io.vertx.core.Handler)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1