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