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();
}
}
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);
}
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;
}
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);
}
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);
}
Aggregations