Search in sources :

Example 1 with MicroserviceVersionRule

use of org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule in project incubator-servicecomb-java-chassis by apache.

the class ConsumerSchemaFactory method getOrCreateMicroserviceMeta.

// 透明rpc场景,因为每次指定schema调用,所以可以懒加载
// 而rest场景,是根据path反向查找schema,所以必须所有的schema都存在才行
// 两种场景可能都会来初始化,为避免管理复杂度,改为全部全量初始化
// 
// 用于rest consumer注册的场景,此时启动流程已经完成,需要主动通知listener
// microserviceName可能是本app内的微服务
// 也可能是appid:name形式的其他app的微服务
public MicroserviceMeta getOrCreateMicroserviceMeta(String microserviceName, String versionRule) {
    MicroserviceMeta microserviceMeta = microserviceMetaManager.findValue(microserviceName);
    if (microserviceMeta != null) {
        return microserviceMeta;
    }
    // 极小概率事件,不必做microservice级的锁分解了
    synchronized (lock) {
        microserviceMeta = microserviceMetaManager.findValue(microserviceName);
        if (microserviceMeta != null) {
            return microserviceMeta;
        }
        microserviceMeta = new MicroserviceMeta(microserviceName);
        if (appManager == null) {
            appManager = RegistryUtils.getServiceRegistry().getAppManager();
        }
        MicroserviceVersionRule microserviceVersionRule = appManager.getOrCreateMicroserviceVersionRule(microserviceMeta.getAppId(), microserviceName, versionRule);
        MicroserviceVersion microserviceVersion = microserviceVersionRule.getLatestMicroserviceVersion();
        if (microserviceVersion == null) {
            throw new IllegalStateException(String.format("Probably invoke a service before it is registered, appId=%s, name=%s", microserviceMeta.getAppId(), microserviceName));
        }
        if (MicroserviceVersionMeta.class.isInstance(microserviceVersion)) {
            microserviceMeta = ((MicroserviceVersionMeta) microserviceVersion).getMicroserviceMeta();
        } else {
            getOrCreateConsumerSchema(microserviceMeta, microserviceVersion.getMicroservice());
            schemaListenerManager.notifySchemaListener(microserviceMeta);
        }
        LOGGER.info("Found schema ids from service center, {}:{}:{}:{}", microserviceMeta.getAppId(), microserviceName, versionRule, microserviceVersion.getMicroservice().getSchemas());
        microserviceMetaManager.register(microserviceName, microserviceMeta);
        return microserviceMeta;
    }
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule) MicroserviceVersion(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta)

Example 2 with MicroserviceVersionRule

use of org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule in project incubator-servicecomb-java-chassis by apache.

the class TestOperationInstancesDiscoveryFilter method discovery_v1_0_0.

@Test
public void discovery_v1_0_0() {
    regMicroservice("1", "1.0.0", V1_0_0.class, 2);
    MicroserviceVersions MicroserviceVersions = serviceRegistry.getAppManager().getOrCreateMicroserviceVersions(appId, microserviceName);
    MicroserviceVersions.submitPull();
    MicroserviceVersionRule microserviceVersionRule = MicroserviceVersions.getOrCreateMicroserviceVersionRule(DefinitionConst.VERSION_RULE_ALL);
    MicroserviceVersionMeta latestMicroserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion();
    latestOperationMeta = latestMicroserviceVersionMeta.getMicroserviceMeta().ensureFindOperation("sid.add");
    DiscoveryTreeNode parent = new DiscoveryTreeNode().fromCache(microserviceVersionRule.getVersionedCache());
    setupOnChange();
    result = filter.discovery(context, parent);
    Assert.assertEquals(2, result.mapData().size());
    result.mapData().values().forEach(instance -> {
        Assert.assertEquals("1", ((MicroserviceInstance) instance).getServiceId());
    });
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule) MicroserviceVersionMeta(org.apache.servicecomb.core.definition.MicroserviceVersionMeta) DiscoveryTreeNode(org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode) MicroserviceVersions(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions) Test(org.junit.Test)

Example 3 with MicroserviceVersionRule

use of org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule in project incubator-servicecomb-java-chassis by apache.

the class TestOperationInstancesDiscoveryFilter method discovery_v1_1_0_dec.

@Test
public void discovery_v1_1_0_dec() {
    regMicroservice("1", "1.0.0", V1_0_0.class, 2);
    regMicroservice("2", "1.1.0", V1_1_0.class, 2);
    MicroserviceVersions MicroserviceVersions = serviceRegistry.getAppManager().getOrCreateMicroserviceVersions(appId, microserviceName);
    MicroserviceVersions.submitPull();
    MicroserviceVersionRule microserviceVersionRule = MicroserviceVersions.getOrCreateMicroserviceVersionRule(DefinitionConst.VERSION_RULE_ALL);
    MicroserviceVersionMeta latestMicroserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion();
    latestOperationMeta = latestMicroserviceVersionMeta.getMicroserviceMeta().ensureFindOperation("sid.dec");
    DiscoveryTreeNode parent = new DiscoveryTreeNode().fromCache(microserviceVersionRule.getVersionedCache());
    setupOnChange();
    result = filter.discovery(context, parent);
    Assert.assertEquals(2, result.mapData().size());
    Set<String> ids = new HashSet<>();
    result.mapData().values().forEach(instance -> {
        ids.add(((MicroserviceInstance) instance).getServiceId());
    });
    Assert.assertThat(ids, Matchers.containsInAnyOrder("2"));
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule) MicroserviceVersionMeta(org.apache.servicecomb.core.definition.MicroserviceVersionMeta) DiscoveryTreeNode(org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode) MicroserviceVersions(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with MicroserviceVersionRule

use of org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule in project incubator-servicecomb-java-chassis by apache.

the class TestOperationInstancesDiscoveryFilter method discovery_v1_1_0_add.

@Test
public void discovery_v1_1_0_add() {
    regMicroservice("1", "1.0.0", V1_0_0.class, 2);
    regMicroservice("2", "1.1.0", V1_1_0.class, 2);
    MicroserviceVersions MicroserviceVersions = serviceRegistry.getAppManager().getOrCreateMicroserviceVersions(appId, microserviceName);
    MicroserviceVersions.submitPull();
    MicroserviceVersionRule microserviceVersionRule = MicroserviceVersions.getOrCreateMicroserviceVersionRule(DefinitionConst.VERSION_RULE_ALL);
    MicroserviceVersionMeta latestMicroserviceVersionMeta = microserviceVersionRule.getLatestMicroserviceVersion();
    latestOperationMeta = latestMicroserviceVersionMeta.getMicroserviceMeta().ensureFindOperation("sid.add");
    DiscoveryTreeNode parent = new DiscoveryTreeNode().fromCache(microserviceVersionRule.getVersionedCache());
    setupOnChange();
    result = filter.discovery(context, parent);
    Assert.assertEquals(4, result.mapData().size());
    Set<String> ids = new HashSet<>();
    result.mapData().values().forEach(instance -> {
        ids.add(((MicroserviceInstance) instance).getServiceId());
    });
    Assert.assertThat(ids, Matchers.containsInAnyOrder("1", "2"));
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule) MicroserviceVersionMeta(org.apache.servicecomb.core.definition.MicroserviceVersionMeta) DiscoveryTreeNode(org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode) MicroserviceVersions(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MicroserviceVersionRule (org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule)4 MicroserviceVersionMeta (org.apache.servicecomb.core.definition.MicroserviceVersionMeta)3 MicroserviceVersions (org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions)3 DiscoveryTreeNode (org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)1 MicroserviceVersion (org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion)1