You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2019/07/08 06:56:28 UTC
[servicecomb-java-chassis] 02/02: [SCB-1348[WIP][WEAK] delete
SchemaLoader
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch weak-contract-type
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit b79f06f67e1b0f1d49baae7347763be0f2b03084
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Jul 7 01:51:55 2019 +0800
[SCB-1348[WIP][WEAK] delete SchemaLoader
---
.../definition/loader/DynamicSchemaLoader.java | 81 -------------
.../core/definition/loader/SchemaListener.java | 25 ----
.../definition/loader/SchemaListenerManager.java | 77 ------------
.../core/definition/loader/SchemaLoader.java | 133 ---------------------
4 files changed, 316 deletions(-)
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/loader/DynamicSchemaLoader.java b/core/src/main/java/org/apache/servicecomb/core/definition/loader/DynamicSchemaLoader.java
deleted file mode 100644
index 2e782ed..0000000
--- a/core/src/main/java/org/apache/servicecomb/core/definition/loader/DynamicSchemaLoader.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.core.definition.loader;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.servicecomb.core.CseContext;
-import org.apache.servicecomb.core.definition.SchemaMeta;
-import org.apache.servicecomb.foundation.common.config.PaaSResourceUtils;
-import org.apache.servicecomb.serviceregistry.RegistryUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.core.io.Resource;
-
-/*
- * 场景:
- * 1.consumer
- * 网管调用产品
- * 网管事先不知道产品对应的微服务名
- * 产品注册到网管后,网管根据注册信息,进行契约注册
- * 2.producer
- * 需要支持在不同的产品中部署为不同的微服务名
- * 微服务名是由环境变量等等方式注入的
- * 此时可以在BootListener中进行注册(必须在producer初始化之前注册契约)
- *
- * @Deprecated This class is deprecated because when making calls to a provider, schemas will be downloaded from service enter.
- * And at provider, schemas will register to service center when starting up.
- */
-@Deprecated
-public class DynamicSchemaLoader {
- private static final Logger LOGGER = LoggerFactory.getLogger(DynamicSchemaLoader.class);
-
- public static final DynamicSchemaLoader INSTANCE = new DynamicSchemaLoader();
-
- private DynamicSchemaLoader() {
- }
-
- /**
- * 动态注册指定目录下的schema契约到当前服务
- * @param schemaLocation eg. "classpath*:schemas/*.yaml"
- */
- public void registerSchemas(String schemaLocation) {
- registerSchemas(RegistryUtils.getMicroservice().getServiceName(), schemaLocation);
- }
-
- /**
- * 动态注册指定目录下的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);
- }
-}
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListener.java b/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListener.java
deleted file mode 100644
index 9166ee4..0000000
--- a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListener.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.core.definition.loader;
-
-import org.apache.servicecomb.core.definition.SchemaMeta;
-
-public interface SchemaListener {
- // 不必考虑多线程并发
- void onSchemaLoaded(SchemaMeta... schemaMetas);
-}
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListenerManager.java b/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListenerManager.java
deleted file mode 100644
index e58c0f4..0000000
--- a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaListenerManager.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.core.definition.loader;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.apache.servicecomb.core.SCBEngine;
-import org.apache.servicecomb.core.definition.MicroserviceMeta;
-import org.apache.servicecomb.core.definition.SchemaMeta;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-/**
- * key为microserviceName
- */
-@Component
-public class SchemaListenerManager {
-
- @Autowired(required = false)
- private List<SchemaListener> schemaListenerList = new ArrayList<>();
-
- public void setSchemaListenerList(List<SchemaListener> schemaListenerList) {
- this.schemaListenerList = schemaListenerList;
- }
-
- public void notifySchemaListener(MicroserviceMeta... microserviceMetas) {
- List<SchemaMeta> schemaMetaList = new ArrayList<>();
- for (MicroserviceMeta microserviceMeta : microserviceMetas) {
- schemaMetaList.addAll(microserviceMeta.getSchemaMetas());
- }
- notifySchemaListener(schemaMetaList.toArray(new SchemaMeta[schemaMetaList.size()]));
- }
-
- public void notifySchemaListener() {
- //only one instance
- MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta();
- notifySchemaListener(microserviceMeta);
- }
-
- public void notifySchemaListener(SchemaMeta... schemaMetas) {
- for (SchemaListener listener : schemaListenerList) {
- listener.onSchemaLoaded(schemaMetas);
- }
- }
-
- public void notifySchemaListener(List<SchemaMeta> schemaMetaList) {
- SchemaMeta[] schemaMetas = schemaMetaList.toArray(new SchemaMeta[schemaMetaList.size()]);
- notifySchemaListener(schemaMetas);
- }
-
- public SchemaMeta ensureFindSchemaMeta(String schemaId) {
- MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta();
- return microserviceMeta.ensureFindSchemaMeta(schemaId);
- }
-
- public Collection<SchemaMeta> getAllSchemaMeta() {
- MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta();
- return microserviceMeta.getSchemaMetas();
- }
-}
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaLoader.java b/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaLoader.java
deleted file mode 100644
index 3fc4d62..0000000
--- a/core/src/main/java/org/apache/servicecomb/core/definition/loader/SchemaLoader.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.servicecomb.core.definition.loader;
-
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.servicecomb.core.Handler;
-import org.apache.servicecomb.core.SCBEngine;
-import org.apache.servicecomb.core.definition.MicroserviceMeta;
-import org.apache.servicecomb.core.definition.SchemaMeta;
-import org.apache.servicecomb.core.definition.SchemaUtils;
-import org.apache.servicecomb.core.handler.ConsumerHandlerManager;
-import org.apache.servicecomb.core.handler.ProducerHandlerManager;
-import org.apache.servicecomb.serviceregistry.RegistryUtils;
-import org.apache.servicecomb.serviceregistry.api.Const;
-import org.apache.servicecomb.serviceregistry.api.registry.BasePath;
-import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.core.io.Resource;
-import org.springframework.stereotype.Component;
-import org.springframework.util.StringUtils;
-
-import com.netflix.config.DynamicPropertyFactory;
-
-import io.swagger.models.Swagger;
-
-@Component
-public class SchemaLoader {
- private static final Logger LOGGER = LoggerFactory.getLogger(SchemaLoader.class);
-
- /*
- * resource的路径格式,至少是以这个形式结尾:schemaId.yaml
- */
- public SchemaMeta registerSchema(String microserviceName, Resource resource) {
- try {
- String schemaId = FilenameUtils.getBaseName(resource.getFilename());
-
- String swaggerContent = IOUtils.toString(resource.getURL(), StandardCharsets.UTF_8);
- SchemaMeta schemaMeta = registerSchema(microserviceName, schemaId, swaggerContent);
-
- return schemaMeta;
- } catch (Throwable e) {
- throw new Error(e);
- }
- }
-
- public SchemaMeta registerSchema(String microserviceName, String schemaId, String swaggerContent) {
- Swagger swagger = SchemaUtils.parseSwagger(swaggerContent);
- if (swagger == null) {
- throw new Error(String.format("Parse the swagger for %s:%s failed", microserviceName, schemaId));
- }
-
- return registerSchema(schemaId, swagger);
- }
-
- public SchemaMeta registerSchema(String schemaId,
- Swagger swagger) {
- MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta();
- return registerSchema(microserviceMeta, schemaId, swagger);
- }
-
- 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);
-
- addSchemaPath2Microservice(microserviceName, swagger.getBasePath());
-
- return schemaMeta;
- }
-
- void addSchemaPath2Microservice(String microserviceName, String basePath) {
- if (DynamicPropertyFactory.getInstance().getBooleanProperty(Const.REGISTER_SERVICE_PATH, false).get()) {
- putSelfBasePathIfAbsent(microserviceName, basePath);
- }
- }
-
- void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
- if (basePath == null || basePath.length() == 0) {
- return;
- }
-
- Microservice microservice = RegistryUtils.getMicroservice();
- if (!microservice.getServiceName().equals(microserviceName)) {
- return;
- }
-
- String urlPrefix = System.getProperty(Const.URL_PREFIX);
- if (!StringUtils.isEmpty(urlPrefix) && !basePath.startsWith(urlPrefix)) {
- basePath = urlPrefix + basePath;
- }
-
- List<BasePath> paths = microservice.getPaths();
- for (BasePath path : paths) {
- if (path.getPath().equals(basePath)) {
- return;
- }
- }
-
- BasePath basePathObj = new BasePath();
- basePathObj.setPath(basePath);
- paths.add(basePathObj);
- }
-}