Search in sources :

Example 1 with MicroserviceMeta

use of org.apache.servicecomb.core.definition.MicroserviceMeta 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 MicroserviceMeta

use of org.apache.servicecomb.core.definition.MicroserviceMeta in project incubator-servicecomb-java-chassis by apache.

the class UnitTestMeta method getOrCreateSchemaMeta.

public SchemaMeta getOrCreateSchemaMeta(String appId, String microserviceName, String schemaId, Class<?> impl) {
    String longName = appId + ":" + microserviceName;
    MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta(longName);
    SchemaMeta schemaMeta = microserviceMeta.findSchemaMeta(schemaId);
    if (schemaMeta != null) {
        return schemaMeta;
    }
    Swagger swagger = UnitTestSwaggerUtils.generateSwagger(impl).getSwagger();
    return schemaLoader.registerSchema(microserviceMeta, schemaId, swagger);
}
Also used : SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta)

Example 3 with MicroserviceMeta

use of org.apache.servicecomb.core.definition.MicroserviceMeta in project incubator-servicecomb-java-chassis by apache.

the class TestConsumerSchemaFactory method testGetOrCreateConsumer.

@Test
public void testGetOrCreateConsumer() {
    MicroserviceMeta microserviceMeta = consumerSchemaFactory.getOrCreateMicroserviceMeta("ms", "latest");
    OperationMeta operationMeta = microserviceMeta.ensureFindOperation("schema.add");
    Assert.assertEquals("add", operationMeta.getOperationId());
}
Also used : MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 4 with MicroserviceMeta

use of org.apache.servicecomb.core.definition.MicroserviceMeta in project incubator-servicecomb-java-chassis by apache.

the class TestRestEngineSchemaListener method test.

@Test
public void test() {
    BeanUtils.setContext(Mockito.mock(ApplicationContext.class));
    MicroserviceMeta mm = new MicroserviceMeta("app:ms");
    List<SchemaMeta> smList = new ArrayList<>();
    SwaggerGenerator generator = new SwaggerGenerator(context, TestRestEngineSchemaListenerSchemaImpl.class);
    Swagger swagger = generator.generate();
    swagger.setBasePath("");
    SchemaMeta sm1 = new SchemaMeta(swagger, mm, "sid1");
    smList.add(sm1);
    RestEngineSchemaListener listener = new RestEngineSchemaListener();
    SchemaMeta[] smArr = smList.toArray(new SchemaMeta[smList.size()]);
    listener.onSchemaLoaded(smArr);
    // 重复调用,不应该出异常
    listener.onSchemaLoaded(smArr);
    ServicePathManager spm = ServicePathManager.getServicePathManager(mm);
    Assert.assertEquals(mm, spm.getMicroserviceMeta());
    Assert.assertSame(sm1, spm.consumerLocateOperation("/add/", "POST").getOperation().getOperationMeta().getSchemaMeta());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ArrayList(java.util.ArrayList) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Example 5 with MicroserviceMeta

use of org.apache.servicecomb.core.definition.MicroserviceMeta in project incubator-servicecomb-java-chassis by apache.

the class RestEngineSchemaListener method onSchemaLoaded.

@Override
public void onSchemaLoaded(SchemaMeta... schemaMetas) {
    // 此时相应的ServicePathManager可能正在被使用,为避免太高的复杂度,使用copy on write逻辑
    Map<String, ServicePathManager> mgrMap = new HashMap<>();
    for (SchemaMeta schemaMeta : schemaMetas) {
        MicroserviceMeta microserviceMeta = schemaMeta.getMicroserviceMeta();
        ServicePathManager mgr = findPathManager(mgrMap, microserviceMeta);
        mgr.addSchema(schemaMeta);
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Aggregations

MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)20 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)10 ServicePathManager (org.apache.servicecomb.common.rest.locator.ServicePathManager)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 Swagger (io.swagger.models.Swagger)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)2 Path (io.swagger.models.Path)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 Mock (mockit.Mock)1 MockUp (mockit.MockUp)1 OperationLocator (org.apache.servicecomb.common.rest.locator.OperationLocator)1 ProducerProvider (org.apache.servicecomb.core.ProducerProvider)1 MicroserviceVersionMeta (org.apache.servicecomb.core.definition.MicroserviceVersionMeta)1 ConsumerSchemaFactory (org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory)1 ReferenceConfig (org.apache.servicecomb.core.provider.consumer.ReferenceConfig)1