Search in sources :

Example 1 with MicroserviceVersion

use of org.apache.servicecomb.registry.consumer.MicroserviceVersion in project java-chassis by ServiceComb.

the class ServiceRegistryListener method onCreateMicroserviceVersion.

@EnableExceptionPropagation
@SubscriberOrder(-1000)
@Subscribe
public void onCreateMicroserviceVersion(CreateMicroserviceVersionEvent event) {
    // TODO:如果失败,应该标记出错,以便删除MicroserviceVersions
    MicroserviceVersion microserviceVersion = event.getMicroserviceVersion();
    Microservice microservice = microserviceVersion.getMicroservice();
    // not shortName, to support cross app invoke
    String microserviceName = microserviceVersion.getMicroserviceName();
    MicroserviceMeta microserviceMeta = new MicroserviceMeta(scbEngine, microserviceName, true);
    microserviceMeta.setHandlerChain(scbEngine.getConsumerHandlerManager().getOrCreate(microserviceName));
    microserviceMeta.setFilterChain(scbEngine.getFilterChainsManager().findConsumerChain(microserviceName));
    MicroserviceVersions microserviceVersions = microserviceVersion.getMicroserviceVersions();
    microserviceMeta.setMicroserviceVersionsMeta(getMicroserviceVersionsMeta(microserviceVersions));
    boolean isServiceCenter = DefinitionConst.REGISTRY_APP_ID.equals(microservice.getAppId()) && DefinitionConst.REGISTRY_SERVICE_NAME.equals(microservice.getServiceName());
    // service center better to resolve the problem.
    if (!isServiceCenter) {
        for (String schemaId : microservice.getSchemas()) {
            Swagger swagger = scbEngine.getSwaggerLoader().loadSwagger(microservice, microserviceVersion.getInstances(), schemaId);
            // non java-chassis framework.
            if (swagger != null) {
                microserviceMeta.registerSchemaMeta(schemaId, swagger);
            }
        }
    }
    microserviceMeta.putExtData(CORE_MICROSERVICE_VERSION, microserviceVersion);
    microserviceVersion.getVendorExtensions().put(CORE_MICROSERVICE_META, microserviceMeta);
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) Swagger(io.swagger.models.Swagger) MicroserviceVersions(org.apache.servicecomb.registry.consumer.MicroserviceVersions) EnableExceptionPropagation(org.apache.servicecomb.foundation.common.event.EnableExceptionPropagation) SubscriberOrder(org.apache.servicecomb.foundation.common.event.SubscriberOrder) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with MicroserviceVersion

use of org.apache.servicecomb.registry.consumer.MicroserviceVersion in project java-chassis by ServiceComb.

the class TestConsumers method getOrCreateMicroserviceVersionRule.

@Test
public void getOrCreateMicroserviceVersionRule() {
    MicroserviceVersionRule microserviceVersionRule = appManager.getOrCreateMicroserviceVersionRule(appId, serviceName, versionRule);
    Assert.assertEquals("0.0.0.0+", microserviceVersionRule.getVersionRule().getVersionRule());
    Assert.assertEquals(1, microserviceManager.getVersionsByName().size());
    MicroserviceVersion microserviceVersion = microserviceVersionRule.getLatestMicroserviceVersion();
    Assert.assertEquals(serviceName, microserviceVersion.getMicroserviceName());
    Assert.assertEquals(serviceId, microserviceVersion.getMicroserviceId());
    Assert.assertEquals(version, microserviceVersion.getVersion().getVersion());
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) Test(org.junit.Test)

Example 3 with MicroserviceVersion

use of org.apache.servicecomb.registry.consumer.MicroserviceVersion in project java-chassis by ServiceComb.

the class ITSCBAsyncRestTemplate method ensureProviderBasePath.

private void ensureProviderBasePath(String producerName) {
    MicroserviceManager microserviceManager = DiscoveryManager.INSTANCE.getAppManager().getOrCreateMicroserviceManager(RegistrationManager.INSTANCE.getMicroservice().getAppId());
    MicroserviceVersions producerMicroserviceVersions = microserviceManager.getOrCreateMicroserviceVersions(producerName);
    Optional<MicroserviceVersion> latestMicroserviceVersion = producerMicroserviceVersions.getVersions().values().stream().max(Comparator.comparing(MicroserviceVersion::getVersion));
    latestMicroserviceVersion.ifPresent(microserviceVersion -> {
        MicroserviceMeta microserviceMeta = microserviceVersion.getVendorExtensions().get(CORE_MICROSERVICE_META);
        SchemaMeta schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId);
        basePath = schemaMeta.getSwagger().getBasePath();
    });
    latestMicroserviceVersion.orElseThrow(() -> new IllegalStateException("cannot find producer: " + producerName));
}
Also used : MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) MicroserviceManager(org.apache.servicecomb.registry.consumer.MicroserviceManager) MicroserviceVersions(org.apache.servicecomb.registry.consumer.MicroserviceVersions)

Example 4 with MicroserviceVersion

use of org.apache.servicecomb.registry.consumer.MicroserviceVersion in project java-chassis by ServiceComb.

the class OperationInstancesDiscoveryFilter method init.

@Override
public void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    Invocation invocation = context.getInputParameters();
    // sort versions
    List<MicroserviceVersion> microserviceVersions = sortedMicroserviceVersion(invocation, parent.data());
    Map<String, DiscoveryTreeNode> operationNodes = new ConcurrentHashMapEx<>();
    for (MicroserviceVersion microserviceVersion : microserviceVersions) {
        MicroserviceMeta microserviceMeta = CoreMetaUtils.getMicroserviceMeta(microserviceVersion);
        for (OperationMeta operationMeta : microserviceMeta.getOperations()) {
            DiscoveryTreeNode node = operationNodes.get(operationMeta.getMicroserviceQualifiedName());
            if (node == null) {
                Map<String, MicroserviceInstance> instanceMap = microserviceVersion.getInstances().stream().collect(Collectors.toMap(MicroserviceInstance::getInstanceId, Function.identity()));
                DiscoveryTreeNode tempNode = createOperationNode(parent, microserviceVersion);
                tempNode.data(instanceMap);
                operationNodes.put(operationMeta.getMicroserviceQualifiedName(), tempNode);
                continue;
            }
            // exist, append instances
            microserviceVersion.getInstances().forEach(microserviceInstance -> node.mapData().put(microserviceInstance.getInstanceId(), microserviceInstance));
        }
    }
    parent.children(operationNodes);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) ConcurrentHashMapEx(org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx)

Example 5 with MicroserviceVersion

use of org.apache.servicecomb.registry.consumer.MicroserviceVersion in project java-chassis by ServiceComb.

the class OperationInstancesDiscoveryFilter method sortedMicroserviceVersion.

protected List<MicroserviceVersion> sortedMicroserviceVersion(Invocation invocation, Map<String, MicroserviceInstance> instances) {
    OperationMeta latestOperationMeta = invocation.getOperationMeta();
    MicroserviceMeta latestMicroserviceMeta = latestOperationMeta.getSchemaMeta().getMicroserviceMeta();
    AppManager appManager = DiscoveryManager.INSTANCE.getAppManager();
    MicroserviceVersions microserviceVersions = appManager.getOrCreateMicroserviceVersions(latestMicroserviceMeta.getAppId(), latestMicroserviceMeta.getMicroserviceName());
    Map<String, MicroserviceVersion> uniqueMicroserviceVersion = new HashMap<>();
    for (MicroserviceInstance instance : instances.values()) {
        MicroserviceVersion microserviceVersion = microserviceVersions.getVersion(instance.getServiceId());
        uniqueMicroserviceVersion.put(instance.getServiceId(), microserviceVersion);
    }
    return uniqueMicroserviceVersion.values().stream().sorted(Comparator.comparing(MicroserviceVersion::getVersion)).collect(Collectors.toList());
}
Also used : HashMap(java.util.HashMap) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) AppManager(org.apache.servicecomb.registry.consumer.AppManager) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) MicroserviceVersions(org.apache.servicecomb.registry.consumer.MicroserviceVersions)

Aggregations

MicroserviceVersion (org.apache.servicecomb.registry.consumer.MicroserviceVersion)6 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)4 MicroserviceVersions (org.apache.servicecomb.registry.consumer.MicroserviceVersions)3 Subscribe (com.google.common.eventbus.Subscribe)2 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 EnableExceptionPropagation (org.apache.servicecomb.foundation.common.event.EnableExceptionPropagation)2 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)2 Swagger (io.swagger.models.Swagger)1 HashMap (java.util.HashMap)1 Invocation (org.apache.servicecomb.core.Invocation)1 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)1 ConcurrentHashMapEx (org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx)1 SubscriberOrder (org.apache.servicecomb.foundation.common.event.SubscriberOrder)1 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)1 AppManager (org.apache.servicecomb.registry.consumer.AppManager)1 MicroserviceManager (org.apache.servicecomb.registry.consumer.MicroserviceManager)1 MicroserviceVersionRule (org.apache.servicecomb.registry.consumer.MicroserviceVersionRule)1 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)1 Test (org.junit.Test)1