protected RequestMeta createRequestMeta(String httpMethod, URI uri) { String microserviceName = uri.getAuthority(); ReferenceConfig referenceConfig = findReferenceConfig(microserviceName); MicroserviceMeta microserviceMeta = referenceConfig.getMicroserviceMeta(); ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager == null) { throw new Error(String.format("no schema defined for %s:%s", microserviceMeta.getAppId(), microserviceMeta.getName())); } OperationLocator locator = servicePathManager.consumerLocateOperation(path, httpMethod); RestOperationMeta swaggerRestOperation = locator.getOperation(); Map<String, String> pathParams = locator.getPathVarMap(); return new RequestMeta(referenceConfig, swaggerRestOperation, pathParams); }
protected ServicePathManager findPathManager(Map<String, ServicePathManager> mgrMap, MicroserviceMeta microserviceMeta) { ServicePathManager mgr = mgrMap.get(microserviceMeta.getName()); if (mgr != null) { return mgr; } mgr = ServicePathManager.getServicePathManager(microserviceMeta); if (mgr == null) { mgr = new ServicePathManager(microserviceMeta); } else { mgr = mgr.cloneServicePathManager(); } mgrMap.put(microserviceMeta.getName(), mgr); return mgr; } }
public void buildProducerPaths() { String urlPrefix = System.getProperty(Const.URL_PREFIX); if (StringUtils.isEmpty(urlPrefix)) { producerPaths = swaggerPaths; producerPaths.printPaths(); return; } producerPaths = new MicroservicePaths(); for (OperationGroup operationGroup : swaggerPaths.getStaticPathOperationMap().values()) { addProducerPaths(urlPrefix, operationGroup.values()); } addProducerPaths(urlPrefix, swaggerPaths.getDynamicPathOperationList()); producerPaths.printPaths(); }
@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(); } }
@Override public void onBootEvent(BootEvent event) { if (!event.getEventType().equals(EventType.BEFORE_REGISTRY)) { return; } MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta(); ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager != null) { servicePathManager.buildProducerPaths(); } }
protected void findRestOperation(MicroserviceMeta microserviceMeta) { ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager == null) { LOGGER.error("No schema defined for {}:{}.", microserviceMeta.getAppId(), microserviceMeta.getName()); throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase()); } OperationLocator locator = locateOperation(servicePathManager); requestEx.setAttribute(RestConst.PATH_PARAMETERS, locator.getPathVarMap()); this.restOperationMeta = locator.getOperation(); }
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()); }
@Override protected OperationLocator locateOperation(ServicePathManager servicePathManager) { return servicePathManager.consumerLocateOperation(requestEx.getRequestURI(), requestEx.getMethod()); }
public ServicePathManager cloneServicePathManager() { ServicePathManager mgr = new ServicePathManager(microserviceMeta); swaggerPaths.cloneTo(mgr.swaggerPaths); mgr.schemaIdSet.addAll(schemaIdSet); return mgr; }
@Override protected OperationLocator locateOperation(ServicePathManager servicePathManager) { return servicePathManager.producerLocateOperation(requestEx.getRequestURI(), requestEx.getMethod()); }
@Override public void onBootEvent(BootEvent event) { if (!event.getEventType().equals(EventType.BEFORE_REGISTRY)) { return; } MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta(); ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager != null) { servicePathManager.buildProducerPaths(); } }
@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(); } }
protected void findRestOperation(MicroserviceMeta microserviceMeta) { ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager == null) { LOGGER.error("No schema defined for {}:{}.", microserviceMeta.getAppId(), microserviceMeta.getName()); throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase()); } OperationLocator locator = locateOperation(servicePathManager); requestEx.setAttribute(RestConst.PATH_PARAMETERS, locator.getPathVarMap()); this.restOperationMeta = locator.getOperation(); }
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 ServicePathManager cloneServicePathManager() { ServicePathManager mgr = new ServicePathManager(microserviceMeta); swaggerPaths.cloneTo(mgr.swaggerPaths); mgr.schemaIdSet.addAll(schemaIdSet); return mgr; }
@Override protected OperationLocator locateOperation(ServicePathManager servicePathManager) { return servicePathManager.producerLocateOperation(requestEx.getRequestURI(), requestEx.getMethod()); }
protected RequestMeta createRequestMeta(String httpMethod, URI uri) { String microserviceName = uri.getAuthority(); ReferenceConfig referenceConfig = findReferenceConfig(microserviceName); MicroserviceMeta microserviceMeta = referenceConfig.getMicroserviceMeta(); ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager == null) { throw new Error(String.format("no schema defined for %s:%s", microserviceMeta.getAppId(), microserviceMeta.getName())); } OperationLocator locator = servicePathManager.consumerLocateOperation(path, httpMethod); RestOperationMeta swaggerRestOperation = locator.getOperation(); Map<String, String> pathParams = locator.getPathVarMap(); return new RequestMeta(referenceConfig, swaggerRestOperation, pathParams); }
protected ServicePathManager findPathManager(Map<String, ServicePathManager> mgrMap, MicroserviceMeta microserviceMeta) { ServicePathManager mgr = mgrMap.get(microserviceMeta.getName()); if (mgr != null) { return mgr; } mgr = ServicePathManager.getServicePathManager(microserviceMeta); if (mgr == null) { mgr = new ServicePathManager(microserviceMeta); } else { mgr = mgr.cloneServicePathManager(); } mgrMap.put(microserviceMeta.getName(), mgr); return mgr; } }
public void buildProducerPaths() { String urlPrefix = System.getProperty(Const.URL_PREFIX); if (StringUtils.isEmpty(urlPrefix)) { producerPaths = swaggerPaths; producerPaths.printPaths(); return; } producerPaths = new MicroservicePaths(); for (OperationGroup operationGroup : swaggerPaths.getStaticPathOperationMap().values()) { addProducerPaths(urlPrefix, operationGroup.values()); } addProducerPaths(urlPrefix, swaggerPaths.getDynamicPathOperationList()); producerPaths.printPaths(); }