/** * consumer端使用,schemaMeta级别的缓存,每次调用根据operationName来执行 */ public static Invocation forConsumer(ReferenceConfig referenceConfig, SchemaMeta schemaMeta, String operationName, Object[] swaggerArguments) { OperationMeta operationMeta = schemaMeta.ensureFindOperation(operationName); return forConsumer(referenceConfig, operationMeta, swaggerArguments); }
public void init(SchemaMeta schemaMeta, Method method, String operationPath, String httpMethod, Operation swaggerOperation) { this.schemaMeta = schemaMeta; schemaQualifiedName = schemaMeta.getSchemaId() + "." + method.getName(); microserviceQualifiedName = schemaMeta.getMicroserviceName() + "." + schemaQualifiedName; this.operationPath = operationPath; this.method = method; this.httpMethod = httpMethod.toUpperCase(Locale.US); this.swaggerOperation = swaggerOperation; executor = ExecutorManager.findExecutor(this); collectMethodType(); responsesMeta.init(schemaMeta.getMicroserviceMeta().getClassLoader(), schemaMeta.getPackageName(), schemaMeta.getSwagger(), swaggerOperation, method.getGenericReturnType()); }
public void addSchema(SchemaMeta schemaMeta) { if (isSchemaExists(schemaMeta.getSchemaId())) { return; } schemaIdSet.add(schemaMeta.getSchemaId()); for (OperationMeta operationMeta : schemaMeta.getOperations()) { RestOperationMeta restOperationMeta = new RestOperationMeta(); restOperationMeta.init(operationMeta); operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta); addResource(restOperationMeta); } LOGGER.info("add schema to service paths. {}:{}:{}.", schemaMeta.getMicroserviceMeta().getAppId(), schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId()); }
public List<Handler> getHandlerChain() { return (InvocationType.CONSUMER.equals(invocationType)) ? schemaMeta.getConsumerHandlerChain() : schemaMeta.getProviderHandlerChain(); }
public void regSchemaMeta(SchemaMeta schemaMeta) { idSchemaMetaMgr.register(schemaMeta.getSchemaId(), schemaMeta); regSchemaAndInterface(schemaMeta); for (OperationMeta operationMeta : schemaMeta.getOperations()) { regOperation(operationMeta.getSchemaQualifiedName(), operationMeta); } }
public void init() throws Exception { for (ProducerProvider provider : producerProviderList) { provider.init(); } Microservice microservice = RegistryUtils.getMicroservice(); MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta(microservice); for (SchemaMeta schemaMeta : microserviceMeta.getSchemaMetas()) { String content = SchemaUtils.swaggerToString(schemaMeta.getSwagger()); microservice.addSchema(schemaMeta.getSchemaId(), content); } } }
public SchemaMeta getOrCreateProducerSchema(String microserviceName, String schemaId, Class<?> producerClass, Object producerInstance) { MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta(microserviceName); ProducerSchemaContext context = new ProducerSchemaContext(); context.setMicroserviceMeta(microserviceMeta); context.setSchemaId(schemaId); context.setProviderClass(producerClass); context.setProducerInstance(producerInstance); SchemaMeta schemaMeta = getOrCreateSchema(context); SwaggerProducer producer = swaggerEnv.createProducer(producerInstance, schemaMeta.getSwagger()); for (OperationMeta operationMeta : schemaMeta.getOperations()) { SwaggerProducerOperation producerOperation = producer.findOperation(operationMeta.getOperationId()); operationMeta.putExtData(Const.PRODUCER_OPERATION, producerOperation); } return schemaMeta; }
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; }
private void regSchemaAndInterface(SchemaMeta schemaMeta) { Class<?> intf = schemaMeta.getSwaggerIntf(); synchronized (intfSchemaLock) { List<SchemaMeta> schemaList = intfSchemaMetaMgr.computeIfAbsent(intf, k -> new ArrayList<>()); schemaList.add(schemaMeta); } }
public String getSchemaId() { return schemaMeta.getSchemaId(); }
public String getMicroserviceName() { return schemaMeta.getMicroserviceName(); }
public String getAppId() { return schemaMeta.getMicroserviceMeta().getAppId(); }
public SchemaMeta(Swagger swagger, MicroserviceMeta microserviceMeta, String schemaId) { this.packageName = SchemaUtils.generatePackageName(microserviceMeta, schemaId); this.swagger = swagger; this.name = schemaId; this.microserviceMeta = microserviceMeta; this.microserviceQualifiedName = microserviceMeta.getName() + "." + schemaId; // 确保swagger对应的接口是存在的 swaggerIntf = ClassUtils.getOrCreateInterface(swagger, microserviceMeta.getClassLoader(), packageName); createOperationMgr("schemaMeta " + schemaId + " operation mgr"); operationMgr.setRegisterErrorFmt("Operation name repeat, schema=%s, operation=%s"); initOperations(); }
public void init(OperationMeta operationMeta) { this.operationMeta = operationMeta; Swagger swagger = operationMeta.getSchemaMeta().getSwagger(); Operation operation = operationMeta.getSwaggerOperation(); this.produces = operation.getProduces(); if (produces == null) { this.produces = swagger.getProduces(); } this.createProduceProcessors(); Method method = operationMeta.getMethod(); Type[] genericParamTypes = method.getGenericParameterTypes(); if (genericParamTypes.length != operation.getParameters().size()) { throw new Error("Param count is not equal between swagger and method, path=" + absolutePath); } // 初始化所有rest param for (int idx = 0; idx < genericParamTypes.length; idx++) { Parameter parameter = operation.getParameters().get(idx); Type genericParamType = genericParamTypes[idx]; if ("formData".equals(parameter.getIn())) { formData = true; } RestParam param = new RestParam(idx, parameter, genericParamType); addParam(param); } setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath())); }
protected void prepare() { referenceConfig = ReferenceConfigUtils.getForInvoke(microserviceName); MicroserviceMeta microserviceMeta = referenceConfig.getMicroserviceMeta(); if (StringUtils.isEmpty(schemaId)) { // 未指定schemaId,看看consumer接口是否等于契约接口 schemaMeta = microserviceMeta.findSchemaMeta(consumerIntf); if (schemaMeta == null) { // 尝试用consumer接口名作为schemaId schemaId = consumerIntf.getName(); schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId); } } else { schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId); } this.swaggerConsumer = CseContext.getInstance().getSwaggerEnvironment().createConsumer(consumerIntf, schemaMeta.getSwaggerIntf()); }
Operation operation = operationEntry.getValue(); if (operation.getOperationId() == null) { throw ExceptionUtils.operationIdInvalid(getSchemaId(), strPath); operation.getOperationId(), swaggerIntf.getName(), getSchemaId()); continue;
public String getMicroserviceName() { return schemaMeta.getMicroserviceName(); }
@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(); } }
private void doInit(RoutingContext routingContext) throws Exception { String schemaId = routingContext.pathParam("schema"); String operationName = routingContext.pathParam("operation"); MicroserviceMeta microserviceMeta = microserviceMetaManager.ensureFindValue(routingContext.request().getHeader(Const.DEST_MICROSERVICE)); SchemaMeta schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId); this.routingContext = routingContext; this.operationMeta = schemaMeta.ensureFindOperation(operationName); this.operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta); }