
io.servicecomb.core.definition.MicroserviceMeta Maven / Gradle / Ivy
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicecomb.core.definition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import io.servicecomb.serviceregistry.RegistryUtils;
import io.servicecomb.serviceregistry.api.Const;
import io.servicecomb.foundation.common.RegisterManager;
/**
* 微服务名为microserviceName(app内部)或者appId:microserviceName(跨app)
* operation的查询key为operation的qualifiedName
*/
public class MicroserviceMeta extends CommonService {
private String appId;
// 不包括appId的名字
private String shortName;
// 如果要生成class,在这个loader中创建
private ClassLoader classLoader;
// key为schema id
private RegisterManager idSchemaMetaMgr;
// key为schema interface
// 只有一个interface对应一个schemaMeta时,才允许根据接口查询schema
// 否则直接抛异常,只能显式地指定schemaId来使用
private Map, List> intfSchemaMetaMgr = new ConcurrentHashMap<>();
private final Object intfSchemaLock = new Object();
private Map extData = new ConcurrentHashMap<>();
public MicroserviceMeta(String microserviceName) {
classLoader = Thread.currentThread().getContextClassLoader();
parseMicroserviceName(microserviceName);
createOperationMgr("Operation meta mgr for microservice " + microserviceName);
idSchemaMetaMgr = new RegisterManager<>("Schema meta id mgr for microservice " + microserviceName);
}
public void regSchemaMeta(SchemaMeta schemaMeta) {
idSchemaMetaMgr.register(schemaMeta.getSchemaId(), schemaMeta);
regSchemaAndInterface(schemaMeta);
for (OperationMeta operationMeta : schemaMeta.getOperations()) {
regOperation(operationMeta.getSchemaQualifiedName(), operationMeta);
}
}
private void regSchemaAndInterface(SchemaMeta schemaMeta) {
Class> intf = schemaMeta.getSwaggerIntf();
synchronized (intfSchemaLock) {
List schemaList = intfSchemaMetaMgr.get(intf);
if (schemaList == null) {
schemaList = new ArrayList<>();
intfSchemaMetaMgr.put(intf, schemaList);
}
schemaList.add(schemaMeta);
}
}
public SchemaMeta ensureFindSchemaMeta(String schemaId) {
return idSchemaMetaMgr.ensureFindValue(schemaId);
}
public SchemaMeta findSchemaMeta(String schemaId) {
return idSchemaMetaMgr.findValue(schemaId);
}
public SchemaMeta ensureFindSchemaMeta(Class> schemaIntf) {
SchemaMeta schemaMeta = findSchemaMeta(schemaIntf);
if (schemaMeta == null) {
String msg =
String.format("No schema interface is %s.", schemaIntf.getName());
throw new Error(msg);
}
return schemaMeta;
}
public SchemaMeta findSchemaMeta(Class> schemaIntf) {
List schemaList = intfSchemaMetaMgr.get(schemaIntf);
if (schemaList == null) {
return null;
}
if (schemaList.size() > 1) {
String msg =
String.format("More than one schema interface is %s, please use schemaId to choose a schema.",
schemaIntf.getName());
throw new Error(msg);
}
synchronized (intfSchemaLock) {
return schemaList.get(0);
}
}
public Collection getSchemaMetas() {
return idSchemaMetaMgr.values();
}
public void putExtData(String key, Object data) {
extData.put(key, data);
}
@SuppressWarnings("unchecked")
public T getExtData(String key) {
return (T) extData.get(key);
}
public String getAppId() {
return appId;
}
public String getShortName() {
return shortName;
}
public ClassLoader getClassLoader() {
return classLoader;
}
protected void parseMicroserviceName(String microserviceName) {
int idxAt = microserviceName.indexOf(Const.APP_SERVICE_SEPARATOR);
if (idxAt == -1) {
appId = RegistryUtils.getAppId();
name = microserviceName;
shortName = name;
return;
}
appId = microserviceName.substring(0, idxAt);
name = microserviceName;
shortName = microserviceName.substring(idxAt + 1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy