Search in sources :

Example 1 with MicroserviceVersion

use of org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion 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 MicroserviceVersion

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

the class TestMicroserviceVersionMetaFactory method create.

@Test
public void create(@Mocked ConsumerSchemaFactory consumerSchemaFactory, @Mocked SchemaListenerManager schemaListenerManager) {
    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;
        }
    };
    CseContext.getInstance().setConsumerSchemaFactory(consumerSchemaFactory);
    CseContext.getInstance().setSchemaListenerManager(schemaListenerManager);
    MicroserviceVersionMetaFactory factory = new MicroserviceVersionMetaFactory();
    MicroserviceVersion microserviceVersion = factory.create(microserviceName, microserviceId);
    Assert.assertThat(microserviceVersion, Matchers.instanceOf(MicroserviceVersionMeta.class));
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) MicroserviceVersion(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion) Test(org.junit.Test)

Example 3 with MicroserviceVersion

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

the class OperationInstancesDiscoveryFilter method initOperationNodes.

protected Map<String, DiscoveryTreeNode> initOperationNodes(DiscoveryTreeNode parent, Map<MicroserviceVersionMeta, Map<String, MicroserviceInstance>> versionMap) {
    Map<String, DiscoveryTreeNode> tmpChildren = new ConcurrentHashMapEx<>();
    versionMap.keySet().stream().sorted(Comparator.comparing(MicroserviceVersion::getVersion)).forEach(meta -> {
        for (OperationMeta operationMeta : meta.getMicroserviceMeta().getOperations()) {
            tmpChildren.computeIfAbsent(operationMeta.getMicroserviceQualifiedName(), qualifiedName -> {
                VersionRule versionRule = VersionRuleUtils.getOrCreate(meta.getVersion().getVersion() + "+");
                return new DiscoveryTreeNode().attribute(VERSION_RULE, versionRule).subName(parent, versionRule.getVersionRule()).data(new HashMap<>());
            });
        }
    });
    return tmpChildren;
}
Also used : MicroserviceVersion(org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion) DiscoveryTreeNode(org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) ConcurrentHashMapEx(org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx) VersionRule(org.apache.servicecomb.serviceregistry.version.VersionRule)

Aggregations

MicroserviceVersion (org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion)3 Expectations (mockit.Expectations)1 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 ConcurrentHashMapEx (org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceVersionRule (org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule)1 DiscoveryTreeNode (org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode)1 VersionRule (org.apache.servicecomb.serviceregistry.version.VersionRule)1 Test (org.junit.Test)1