Search in sources :

Example 16 with SchemaMeta

use of io.servicecomb.core.definition.SchemaMeta in project java-chassis by ServiceComb.

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);
        if (mgr.isSchemaExists(schemaMeta.getSchemaId())) {
            LOGGER.info("on schema loaded, exists schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
            continue;
        }
        LOGGER.info("on schema loaded, new schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
        mgr.addSchema(schemaMeta.getSchemaId());
        for (OperationMeta operationMeta : schemaMeta.getOperations()) {
            RestOperationMeta restOperationMeta = new RestOperationMeta();
            restOperationMeta.init(operationMeta);
            operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
            mgr.addResource(restOperationMeta);
        }
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.printService();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 17 with SchemaMeta

use of io.servicecomb.core.definition.SchemaMeta in project java-chassis by ServiceComb.

the class DynamicSchemaLoader method registerSchemas.

/**
     * 动态注册指定目录下的schema契约到指定服务
     * @param microserviceName name of microservice
     * @param schemaLocation eg. "classpath*:schemas/*.yaml"
     */
public void registerSchemas(String microserviceName, String schemaLocation) {
    LOGGER.info("dynamic register schemas for {} in {}", microserviceName, schemaLocation);
    List<SchemaMeta> schemaMetaList = new ArrayList<>();
    Resource[] resArr = PaaSResourceUtils.getResources(schemaLocation);
    for (Resource resource : resArr) {
        SchemaMeta schemaMeta = CseContext.getInstance().getSchemaLoader().registerSchema(microserviceName, resource);
        schemaMetaList.add(schemaMeta);
    }
    CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(schemaMetaList);
}
Also used : SchemaMeta(io.servicecomb.core.definition.SchemaMeta) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource)

Example 18 with SchemaMeta

use of io.servicecomb.core.definition.SchemaMeta in project java-chassis by ServiceComb.

the class SchemaLoader method registerSchema.

public SchemaMeta registerSchema(MicroserviceMeta microserviceMeta, String schemaId, Swagger swagger) {
    String microserviceName = microserviceMeta.getName();
    LOGGER.info("register schema {}/{}/{}", microserviceMeta.getAppId(), microserviceName, schemaId);
    SchemaMeta schemaMeta = new SchemaMeta(swagger, microserviceMeta, schemaId);
    List<Handler> producerHandlerChain = ProducerHandlerManager.INSTANCE.getOrCreate(microserviceName);
    schemaMeta.setProviderHandlerChain(producerHandlerChain);
    List<Handler> consumerHandlerChain = ConsumerHandlerManager.INSTANCE.getOrCreate(microserviceName);
    schemaMeta.setConsumerHandlerChain(consumerHandlerChain);
    microserviceMeta.regSchemaMeta(schemaMeta);
    putSelfBasePathIfAbsent(microserviceName, swagger.getBasePath());
    return schemaMeta;
}
Also used : SchemaMeta(io.servicecomb.core.definition.SchemaMeta) Handler(io.servicecomb.core.Handler)

Example 19 with SchemaMeta

use of io.servicecomb.core.definition.SchemaMeta in project java-chassis by ServiceComb.

the class TestSchemaListenerManager method testInitializationListener.

@Test
public void testInitializationListener() {
    SchemaListener listener = new SchemaListener() {

        @Override
        public void onSchemaLoaded(SchemaMeta... schemaMetas) {
            Assert.assertEquals(1, schemaMetas.length);
            Assert.assertEquals("test", schemaMetas[0].getSchemaId());
        }
    };
    SchemaListenerManager mgr = new SchemaListenerManager();
    mgr.setSchemaListenerList(Arrays.asList(listener));
    mgr.setMicroserviceMetaManager(new MicroserviceMetaManager());
    mgr.notifySchemaListener(schemaMeta);
}
Also used : MicroserviceMetaManager(io.servicecomb.core.definition.MicroserviceMetaManager) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) Test(org.junit.Test)

Example 20 with SchemaMeta

use of io.servicecomb.core.definition.SchemaMeta in project java-chassis by ServiceComb.

the class TestConsumerSchemaFactory method init.

@BeforeClass
public static void init() {
    ReflectUtils.setField(RegistryClientFactory.class, null, "registryClient", registryClient);
    SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
    schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
    ConsumerResponseMapperFactory responseMapperFactory = new ConsumerResponseMapperFactory();
    responseMapperFactory.setMapperList(Arrays.asList(new ConsumerResponseSame()));
    ConsumerArgumentsMapperFactory consumerArgsMapperFactory = new ConsumerArgumentsMapperFactory();
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader();
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    ReflectUtils.setField(consumerSchemaFactory, "schemaListenerManager", schemaListenerManager);
    ReflectUtils.setField(consumerSchemaFactory, "consumerArgsMapperFactory", consumerArgsMapperFactory);
    ReflectUtils.setField(consumerSchemaFactory, "responseMapperFactory", responseMapperFactory);
    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());
    Mockito.when(registryClient.getMicroserviceId("app", "ms", "latest")).thenReturn("0");
    Mockito.when(registryClient.getSchema("0", "schema")).thenReturn(content);
    Microservice microservice = new Microservice();
    microservice.setAppId("app");
    microservice.setServiceId("0");
    microservice.addSchema("schema", content);
    Mockito.when(registryClient.getMicroservice("0")).thenReturn(microservice);
}
Also used : ConsumerResponseSame(io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseSame) MicroserviceMetaManager(io.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(io.servicecomb.core.definition.loader.SchemaLoader) Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) UnitTestMeta(io.servicecomb.core.unittest.UnitTestMeta) CompositeSwaggerGeneratorContext(io.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) SchemaListenerManager(io.servicecomb.core.definition.loader.SchemaListenerManager) ConsumerResponseMapperFactory(io.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory) ConsumerArgumentsMapperFactory(io.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

SchemaMeta (io.servicecomb.core.definition.SchemaMeta)26 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)11 Test (org.junit.Test)10 OperationMeta (io.servicecomb.core.definition.OperationMeta)8 ArrayList (java.util.ArrayList)4 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)3 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)3 Invocation (io.servicecomb.core.Invocation)3 HashMap (java.util.HashMap)3 MockUp (mockit.MockUp)3 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)2 MicroserviceMetaManager (io.servicecomb.core.definition.MicroserviceMetaManager)2 ConsumerOperationMeta (io.servicecomb.core.provider.consumer.ConsumerOperationMeta)2 UnitTestMeta (io.servicecomb.core.unittest.UnitTestMeta)2 Swagger (io.swagger.models.Swagger)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 Executor (java.util.concurrent.Executor)2 ArgsNotWrapSchema (io.servicecomb.codec.protobuf.utils.schema.ArgsNotWrapSchema)1