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 2018/06/11 08:06:28 UTC

[incubator-servicecomb-java-chassis] branch master updated (4d11c63 -> 0a7966e)

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git.


    from 4d11c63  [SCB-656]edge service should return back the providers error code
     new bf70ac3  [SCB-652] change environment configuration key to service_description.environment and mark @Deprecated onto microserviceInstance.environment
     new 0a7966e  [SCB-652] refactor schema register logic

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../common/base/ServiceCombConstants.java          |   4 +
 .../servicecomb/serviceregistry/RegistryUtils.java |   6 +
 .../api/registry/MicroserviceInstance.java         |   4 +
 .../client/LocalServiceRegistryClientImpl.java     |   9 +-
 .../client/ServiceRegistryClient.java              |   3 +-
 .../serviceregistry/client/http/Holder.java        |  56 +++--
 .../client/http/ServiceRegistryClientImpl.java     |  24 +-
 .../task/MicroserviceRegisterTask.java             | 246 ++++++++++++++++-----
 .../client/LocalServiceRegistryClientImplTest.java |  10 +
 .../client/http/TestServiceRegistryClientImpl.java |  38 +++-
 .../task/TestMicroserviceRegisterTask.java         | 186 ++++++++++++++--
 11 files changed, 470 insertions(+), 116 deletions(-)
 copy foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpStatus.java => service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java (52%)

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 02/02: [SCB-652] refactor schema register logic

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 0a7966e1149452d64900639cbd229efbb32f5e59
Author: yaohaishi <ya...@huawei.com>
AuthorDate: Thu Jun 7 01:08:09 2018 +0800

    [SCB-652] refactor schema register logic
---
 .../servicecomb/serviceregistry/RegistryUtils.java |   6 +
 .../client/LocalServiceRegistryClientImpl.java     |   9 +-
 .../client/ServiceRegistryClient.java              |   3 +-
 .../serviceregistry/client/http/Holder.java        |  67 ++++++
 .../client/http/ServiceRegistryClientImpl.java     |  24 +-
 .../task/MicroserviceRegisterTask.java             | 247 ++++++++++++++++-----
 .../client/LocalServiceRegistryClientImplTest.java |  10 +
 .../client/http/TestServiceRegistryClientImpl.java |  38 +++-
 .../task/TestMicroserviceRegisterTask.java         | 180 +++++++++++++--
 9 files changed, 491 insertions(+), 93 deletions(-)

diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
index 321ff24..2030678 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
@@ -40,6 +40,8 @@ import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Charsets;
+import com.google.common.hash.Hashing;
 import com.netflix.config.DynamicPropertyFactory;
 
 public final class RegistryUtils {
@@ -221,4 +223,8 @@ public final class RegistryUtils {
       String versionRule, String revision) {
     return serviceRegistry.findServiceInstances(appId, serviceName, versionRule, revision);
   }
+
+  public static String calcSchemaSummary(String schemaContent) {
+    return Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString();
+  }
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
index 5e78abc..84c1516 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
@@ -31,6 +31,8 @@ import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import javax.ws.rs.core.Response.Status;
+
 import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
@@ -40,6 +42,7 @@ import org.apache.servicecomb.serviceregistry.api.response.FindInstancesResponse
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
+import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
 import org.apache.servicecomb.serviceregistry.version.Version;
 import org.apache.servicecomb.serviceregistry.version.VersionRule;
@@ -342,7 +345,7 @@ public class LocalServiceRegistryClientImpl implements ServiceRegistryClient {
   }
 
   @Override
-  public List<GetSchemaResponse> getSchemas(String microserviceId) {
+  public Holder<List<GetSchemaResponse>> getSchemas(String microserviceId) {
     Microservice microservice = microserviceIdMap.get(microserviceId);
     if (microservice == null) {
       throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
@@ -355,7 +358,9 @@ public class LocalServiceRegistryClientImpl implements ServiceRegistryClient {
       schema.setSummary(Hashing.sha256().newHasher().putString(val, Charsets.UTF_8).hash().toString());
       schemas.add(schema);
     });
-    return schemas;
+    Holder<List<GetSchemaResponse>> resultHolder = new Holder<>();
+    resultHolder.setStatusCode(Status.OK.getStatusCode()).setValue(schemas);
+    return resultHolder;
   }
 
   @Override
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
index 9b2e100..4af6d70 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
@@ -27,6 +27,7 @@ import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
+import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
 
 public interface ServiceRegistryClient {
@@ -82,7 +83,7 @@ public interface ServiceRegistryClient {
    *
    * 批量获取schemas内容
    */
-  List<GetSchemaResponse> getSchemas(String microserviceId);
+  Holder<List<GetSchemaResponse>> getSchemas(String microserviceId);
 
   /**
    *
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java
new file mode 100644
index 0000000..e63911a
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java
@@ -0,0 +1,67 @@
+/*
+ * 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.serviceregistry.client.http;
+
+/**
+ * To carry the rest response information.
+ * @param <T> Type of response body
+ */
+public class Holder<T> {
+  T value;
+
+  int statusCode;
+
+  Throwable throwable;
+
+  public T getValue() {
+    return value;
+  }
+
+  public Holder<T> setValue(T value) {
+    this.value = value;
+    return this;
+  }
+
+  public int getStatusCode() {
+    return statusCode;
+  }
+
+  public Holder<T> setStatusCode(int statusCode) {
+    this.statusCode = statusCode;
+    return this;
+  }
+
+  public Throwable getThrowable() {
+    return throwable;
+  }
+
+  public Holder<T> setThrowable(Throwable throwable) {
+    this.throwable = throwable;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    final StringBuilder sb = new StringBuilder("Holder{");
+    sb.append("value=").append(value);
+    sb.append(", statusCode=").append(statusCode);
+    sb.append(", throwable=").append(throwable);
+    sb.append('}');
+    return sb.toString();
+  }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index f379d93..30f4200 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -27,11 +27,11 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 
 import javax.ws.rs.core.Response.Status;
-import javax.xml.ws.Holder;
 
 import org.apache.servicecomb.foundation.common.net.IpPort;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.Const;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
@@ -60,8 +60,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.hash.Hashing;
 
 import io.netty.handler.codec.http.HttpStatusClass;
 import io.vertx.core.Handler;
@@ -108,6 +106,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
         }
         return;
       }
+      holder.setStatusCode(response.statusCode());
       response.bodyHandler(
           bodyBuffer -> {
             if (cls.getName().equals(HttpClientResponse.class.getName())) {
@@ -115,6 +114,11 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
               countDownLatch.countDown();
               return;
             }
+            if (cls.equals(String.class)) {
+              holder.setValue((T) bodyBuffer.toString());
+              countDownLatch.countDown();
+              return;
+            }
 
             // no need to support 304 in this place
             if (!HttpStatusClass.SUCCESS.equals(HttpStatusClass.valueOf(response.statusCode()))) {
@@ -131,6 +135,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
               holder.value =
                   JsonUtils.readValue(bodyBuffer.getBytes(), cls);
             } catch (Exception e) {
+              holder.setStatusCode(0).setThrowable(e);
               LOGGER.warn("read value failed and response message is {}",
                   bodyBuffer.toString());
             }
@@ -291,7 +296,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
     try {
       CreateSchemaRequest request = new CreateSchemaRequest();
       request.setSchema(schemaContent);
-      request.setSummary(Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString());
+      request.setSummary(RegistryUtils.calcSchemaSummary(schemaContent));
       byte[] body = JsonUtils.writeValueAsBytes(request);
 
       CountDownLatch countDownLatch = new CountDownLatch(1);
@@ -355,9 +360,10 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
   }
 
   @Override
-  public List<GetSchemaResponse> getSchemas(String microserviceId) {
+  public Holder<List<GetSchemaResponse>> getSchemas(String microserviceId) {
     Holder<GetSchemasResponse> holder = new Holder<>();
     IpPort ipPort = ipPortManager.getAvailableAddress();
+    Holder<List<GetSchemaResponse>> resultHolder = new Holder<>();
 
     CountDownLatch countDownLatch = new CountDownLatch(1);
     // default not return schema content, just return summary!
@@ -372,11 +378,15 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
           microserviceId,
           e);
     }
+    resultHolder.setStatusCode(holder.getStatusCode()).setThrowable(holder.getThrowable());
     if (holder.value != null) {
-      return holder.value.getSchema() != null ? holder.value.getSchema() : holder.value.getSchemas();
+      return resultHolder.setValue(
+          holder.value.getSchema() != null ?
+              holder.value.getSchema() :
+              holder.value.getSchemas());
     }
 
-    return null;
+    return resultHolder;
   }
 
   @Override
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 497c014..9e7be8a 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -16,23 +16,27 @@
  */
 package org.apache.servicecomb.serviceregistry.task;
 
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import javax.ws.rs.core.Response.Status;
+
 import org.apache.servicecomb.foundation.common.base.ServiceCombConstants;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
+import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.StringUtils;
 
-import com.google.common.base.Charsets;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
-import com.google.common.hash.Hashing;
 
 public class MicroserviceRegisterTask extends AbstractRegisterTask {
   private static final Logger LOGGER = LoggerFactory.getLogger(MicroserviceRegisterTask.class);
@@ -74,14 +78,15 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
         microservice.getVersion(),
         microservice.getEnvironment());
     if (!StringUtils.isEmpty(serviceId)) {
-      // 已经注册过了,不需要重新注册
+      // This microservice has been registered, so we just use the serviceId gotten from service center
       microservice.setServiceId(serviceId);
       LOGGER.info(
-          "Microservice exists in service center, no need to register. id={} appId={}, name={}, version={}",
+          "Microservice exists in service center, no need to register. id=[{}] appId=[{}], name=[{}], version=[{}], env=[{}]",
           serviceId,
           microservice.getAppId(),
           microservice.getServiceName(),
-          microservice.getVersion());
+          microservice.getVersion(),
+          microservice.getEnvironment());
 
       if (!checkSchemaIdSet()) {
         return false;
@@ -90,24 +95,25 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
       serviceId = srClient.registerMicroservice(microservice);
       if (StringUtils.isEmpty(serviceId)) {
         LOGGER.error(
-            "Registry microservice failed. appId={}, name={}, version={}",
+            "Registry microservice failed. appId=[{}], name=[{}], version=[{}], env=[{}]",
             microservice.getAppId(),
             microservice.getServiceName(),
-            microservice.getVersion());
+            microservice.getVersion(),
+            microservice.getEnvironment());
         return false;
       }
 
-      schemaIdSetMatch = true;
-      // 重新注册服务场景下,instanceId不应该缓存
+      // In re-register microservice case, the old instanceId should not be cached
       microservice.getInstance().setInstanceId(null);
 
       LOGGER.info(
-          "Registry Microservice successfully. id={} appId={}, name={}, version={}, schemaIds={}",
+          "Registry Microservice successfully. id=[{}] appId=[{}], name=[{}], version=[{}], schemaIds={}, env=[{}]",
           serviceId,
           microservice.getAppId(),
           microservice.getServiceName(),
           microservice.getVersion(),
-          microservice.getSchemas());
+          microservice.getSchemas(),
+          microservice.getEnvironment());
     }
 
     microservice.setServiceId(serviceId);
@@ -127,76 +133,207 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
     schemaIdSetMatch = existSchemas.equals(localSchemas);
 
     if (!schemaIdSetMatch) {
-      LOGGER.error(
-          "SchemaIds is different between local and service center. Please change microservice version. "
-              + "id={} appId={}, name={}, version={}, local schemaIds={}, service center schemaIds={}",
+      LOGGER.warn(
+          "SchemaIds is different between local and service center. "
+              + "serviceId=[{}] appId=[{}], name=[{}], version=[{}], env=[{}], local schemaIds={}, service center schemaIds={}",
           microservice.getServiceId(),
           microservice.getAppId(),
           microservice.getServiceName(),
           microservice.getVersion(),
+          microservice.getEnvironment(),
           localSchemas,
           existSchemas);
       return true;
     }
 
     LOGGER.info(
-        "SchemaIds is equals to service center. id={} appId={}, name={}, version={}, schemaIds={}",
+        "SchemaIds are equals to service center. serviceId=[{}], appId=[{}], name=[{}], version=[{}], env=[{}], schemaIds={}",
         microservice.getServiceId(),
         microservice.getAppId(),
         microservice.getServiceName(),
         microservice.getVersion(),
+        microservice.getEnvironment(),
         localSchemas);
     return true;
   }
 
   private boolean registerSchemas() {
-    List<GetSchemaResponse> existSchemas = srClient.getSchemas(microservice.getServiceId());
-    for (Entry<String, String> entry : microservice.getSchemaMap().entrySet()) {
-      String schemaId = entry.getKey();
-      String content = entry.getValue();
-      GetSchemaResponse existSchema = extractSchema(schemaId, existSchemas);
-      boolean exists = existSchema != null && existSchema.getSummary() != null;
-      LOGGER.info("schemaId [{}] exists {}", schemaId, exists);
-      if (!exists) {
-        if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) {
-          return false;
-        }
-      } else {
-        String curSchemaSummary = existSchema.getSummary();
-        String schemaSummary = Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString();
-        if (!schemaSummary.equals(curSchemaSummary)) {
-          if (microservice.getEnvironment().equalsIgnoreCase(ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV)) {
-            LOGGER.info(
-                "schemaId [{}]'s content changes and the current environment is {}, so re-register it!",
-                schemaId, ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV);
-            if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) {
-              return false;
-            }
-          } else {
-            throw new IllegalStateException("schemaId [" + schemaId
-                + "] exists in service center, but the content does not match the local content that means there are interface change "
-                + "and you need to increment microservice version before deploying. "
-                + "Or you can configure service_description.environment="
-                + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV
-                + " to work in development environment and ignore this error");
-          }
-        }
+    Holder<List<GetSchemaResponse>> scSchemaHolder = srClient.getSchemas(microservice.getServiceId());
+    if (Status.OK.getStatusCode() != scSchemaHolder.getStatusCode()) {
+      LOGGER.error("failed to get schemas from service center, statusCode = [{}]", scSchemaHolder.getStatusCode());
+      return false;
+    }
+
+    Map<String, GetSchemaResponse> scSchemaMap = convertScSchemaMap(scSchemaHolder);
+    // CHECK: local > sc, local != sc
+    for (Entry<String, String> localSchemaEntry : microservice.getSchemaMap().entrySet()) {
+      if (!registerSchema(scSchemaMap, localSchemaEntry)) {
+        return false;
+      }
+    }
+
+    // CHECK: local < sc
+    checkRemainingSchema(scSchemaMap);
+
+    schemaIdSetMatch = true;
+    return true;
+  }
+
+  /**
+   * Check whether a local schema is equal to a sc schema.
+   * @return true if the local schema is equal to a sc schema, or be registered to sc successfully;
+   * false if schema is registered to sc but failed.
+   * @throws IllegalStateException The environment is not modifiable, and the local schema is different from sc schema
+   * or not exist in sc.
+   */
+  private boolean registerSchema(Map<String, GetSchemaResponse> scSchemaMap,
+      Entry<String, String> localSchemaEntry) {
+    GetSchemaResponse scSchema = scSchemaMap.get(localSchemaEntry.getKey());
+
+    boolean onlineSchemaExists = scSchema != null;
+    LOGGER.info("schemaId [{}] exists [{}], summary exists [{}]", localSchemaEntry.getKey(), onlineSchemaExists,
+        scSchema != null && scSchema.getSummary() != null);
+    if (!onlineSchemaExists) {
+      // local > sc
+      return registerNewSchema(localSchemaEntry);
+    }
+
+    scSchemaMap.remove(localSchemaEntry.getKey());
+
+    // local != sc
+    return compareAndReRegisterSchema(localSchemaEntry, scSchema);
+  }
+
+  /**
+   * Try to register a new schema to service center, or throw exception if cannot register.
+   * @param localSchemaEntry local schema to be registered.
+   * @return whether local schema is registered successfully.
+   * @throws IllegalStateException The environment is unmodifiable.
+   */
+  private boolean registerNewSchema(Entry<String, String> localSchemaEntry) {
+    // The ids of schemas are contained by microservice registry request, which means once a microservice
+    // is registered in the service center, the schemas that it contains are determined.
+    // If we get a microservice but cannot find the given schemaId in it's schemaId list, this means that
+    // the schemas of this microservice has been changed, and we should decide whether to register this new
+    // schema according to it's environment configuration.
+    if (onlineSchemaIsModifiable()) {
+      return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue());
+    }
+
+    throw new IllegalStateException(
+        "There is a schema only existing in local microservice: [" + localSchemaEntry.getKey()
+            + "], which means there are interfaces changed. "
+            + "You need to increment microservice version before deploying, "
+            + "or you can configure service_description.environment="
+            + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV
+            + " to work in development environment and ignore this error");
+  }
+
+  /**
+   * Compare schema summary and determine whether to re-register schema or throw exception.
+   * @param localSchemaEntry local schema
+   * @param scSchema schema in service center
+   * @return true if the two copies of schema are the same, or local schema is re-registered successfully,
+   * false if the local schema is re-registered to service center but failed.
+   * @throws IllegalStateException The two copies of schema are different and the environment is not modifiable.
+   */
+  private boolean compareAndReRegisterSchema(Entry<String, String> localSchemaEntry, GetSchemaResponse scSchema) {
+    String scSchemaSummary = getScSchemaSummary(scSchema);
+
+    if (null == scSchemaSummary) {
+      // cannot get scSchemaSummary, which means there is no schema content in sc, register schema directly
+      return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue());
+    }
+
+    String localSchemaSummary = RegistryUtils.calcSchemaSummary(localSchemaEntry.getValue());
+    if (!localSchemaSummary.equals(scSchemaSummary)) {
+      if (onlineSchemaIsModifiable()) {
+        LOGGER.info(
+            "schema[{}]'s content is changed and the current environment is [{}], so re-register it!",
+            localSchemaEntry.getKey(), ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV);
+        return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue());
       }
+
+      // env is not development, throw an exception and break the init procedure
+      throw new IllegalStateException(
+          "The schema(id=[" + localSchemaEntry.getKey()
+              + "]) content held by this instance and the service center is different. "
+              + "You need to increment microservice version before deploying. "
+              + "Or you can configure service_description.environment="
+              + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV
+              + " to work in development environment and ignore this error");
     }
+
+    // summaries are the same
     return true;
   }
 
-  private GetSchemaResponse extractSchema(String schemaId, List<GetSchemaResponse> schemas) {
-    if (schemas == null || schemas.isEmpty()) {
-      return null;
+  /**
+   * Try to get or calculate scSchema summary.
+   * @return summary of scSchema,
+   * or null if there is no schema content in service center
+   */
+  private String getScSchemaSummary(GetSchemaResponse scSchema) {
+    String scSchemaSummary = scSchema.getSummary();
+    if (null != scSchemaSummary) {
+      return scSchemaSummary;
     }
-    GetSchemaResponse schema = null;
-    for (GetSchemaResponse tempSchema : schemas) {
-      if (tempSchema.getSchemaId().equals(schemaId)) {
-        schema = tempSchema;
-        break;
+
+    // if there is no online summery, query online schema content directly and calculate summary
+    String onlineSchemaContent = srClient.getSchema(microservice.getServiceId(), scSchema.getSchemaId());
+    if (null != onlineSchemaContent) {
+      scSchemaSummary = RegistryUtils.calcSchemaSummary(onlineSchemaContent);
+    }
+
+    return scSchemaSummary;
+  }
+
+  /**
+   * Check whether there are schemas remaining in service center but not exist in local microservice.
+   * @throws IllegalStateException There are schemas only existing in service center, and the environment is unmodifiable.
+   */
+  private void checkRemainingSchema(Map<String, GetSchemaResponse> scSchemaMap) {
+    if (!scSchemaMap.isEmpty()) {
+      // there are some schemas only exist in service center
+      if (!onlineSchemaIsModifiable()) {
+        // env is not development, throw an exception and break the init procedure
+        throw new IllegalStateException("There are schemas only existing in service center: " + scSchemaMap.keySet()
+            + ", which means there are interfaces changed. "
+            + "You need to increment microservice version before deploying, "
+            + "or if service_description.environment="
+            + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV
+            + ", you can delete microservice information in service center and restart this instance.");
       }
+
+      // Currently nothing to do but print a warning
+      LOGGER.warn("There are schemas only existing in service center: {}, which means there are interfaces changed. "
+          + "It's recommended to increment microservice version before deploying.", scSchemaMap.keySet());
     }
-    return schema;
+  }
+
+  private boolean onlineSchemaIsModifiable() {
+    return ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV.equalsIgnoreCase(microservice.getEnvironment());
+  }
+
+  /**
+   * Register a schema directly.
+   * @return true if register success, otherwise false
+   */
+  private boolean registerSingleSchema(String schemaId, String content) {
+    return srClient.registerSchema(microservice.getServiceId(), schemaId, content);
+  }
+
+  private Map<String, GetSchemaResponse> convertScSchemaMap(Holder<List<GetSchemaResponse>> scSchemaHolder) {
+    Map<String, GetSchemaResponse> scSchemaMap = new HashMap<>();
+    List<GetSchemaResponse> scSchemaList = scSchemaHolder.getValue();
+    if (null == scSchemaList) {
+      return scSchemaMap;
+    }
+
+    for (GetSchemaResponse scSchema : scSchemaList) {
+      scSchemaMap.put(scSchema.getSchemaId(), scSchema);
+    }
+
+    return scSchemaMap;
   }
 }
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
index 88ba8bf..095aa8b 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
@@ -23,6 +23,8 @@ import java.util.List;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
+import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
 import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
 import org.hamcrest.Matchers;
@@ -181,4 +183,12 @@ public class LocalServiceRegistryClientImplTest {
     ServiceCenterInfo serviceCenterInfo = registryClient.getServiceCenterInfo();
     Assert.assertEquals("1.0.0", serviceCenterInfo.getVersion());
   }
+
+  @Test
+  public void testGetSchemas() {
+    Holder<List<GetSchemaResponse>> schemasHolder = registryClient.getSchemas("001");
+    Assert.assertEquals(200, schemasHolder.getStatusCode());
+    Assert.assertTrue(schemasHolder.getValue().isEmpty());
+  }
 }
+
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
index dd370e7..cd796fd 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
 import javax.ws.rs.core.Response.Status;
-import javax.xml.ws.Holder;
 
 import org.apache.log4j.Appender;
 import org.apache.log4j.Logger;
@@ -75,7 +74,7 @@ public class TestServiceRegistryClientImpl {
 
     new MockUp<CountDownLatch>() {
       @Mock
-      public void await() throws InterruptedException {
+      public void await() {
       }
     };
   }
@@ -310,12 +309,15 @@ public class TestServiceRegistryClientImpl {
             "{\"schema\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"},"
                 + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}",
             GetSchemasResponse.class);
+        holder.statusCode = 200;
         holder.value = schemasResp;
       }
     };
-    List<GetSchemaResponse> abc = oClient.getSchemas(microserviceId);
-    Assert.assertEquals(3, abc.size());
-    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary());
+    Holder<List<GetSchemaResponse>> schemasHolder = oClient.getSchemas(microserviceId);
+    List<GetSchemaResponse> schemaResponses = schemasHolder.getValue();
+    Assert.assertEquals(200, schemasHolder.getStatusCode());
+    Assert.assertEquals(3, schemaResponses.size());
+    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", schemaResponses.get(1).getSummary());
   }
 
   @Test
@@ -330,12 +332,32 @@ public class TestServiceRegistryClientImpl {
             "{\"schemas\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"},"
                 + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}",
             GetSchemasResponse.class);
+        holder.statusCode = 200;
         holder.value = schemasResp;
       }
     };
-    List<GetSchemaResponse> abc = oClient.getSchemas(microserviceId);
-    Assert.assertEquals(3, abc.size());
-    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary());
+    Holder<List<GetSchemaResponse>> schemasHolder = oClient.getSchemas(microserviceId);
+    List<GetSchemaResponse> schemas = schemasHolder.getValue();
+    Assert.assertEquals(200, schemasHolder.getStatusCode());
+    Assert.assertEquals(3, schemas.size());
+    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", schemas.get(1).getSummary());
+  }
+
+  @Test
+  public void getSchemasFailed() {
+    String microserviceId = "msId";
+
+    new MockUp<RestUtils>() {
+      @Mock
+      void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
+        Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
+        holder.setStatusCode(Status.NOT_FOUND.getStatusCode());
+      }
+    };
+    Holder<List<GetSchemaResponse>> schemasHolder = oClient.getSchemas(microserviceId);
+    List<GetSchemaResponse> schemaResponses = schemasHolder.getValue();
+    Assert.assertEquals(404, schemasHolder.getStatusCode());
+    Assert.assertNull(schemaResponses);
   }
 
   @Test
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index e85af2e..a7944c8 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -24,6 +24,7 @@ import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
+import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -82,12 +83,16 @@ public class TestMicroserviceRegisterTask {
 
   @Test
   public void testNewRegisterSuccess(@Mocked ServiceRegistryClient srClient) {
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200);
     new Expectations() {
       {
         srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
         result = null;
         srClient.registerMicroservice((Microservice) any);
         result = "serviceId";
+        srClient.getSchemas("serviceId");
+        this.result = onlineSchemasHolder;
       }
     };
 
@@ -104,10 +109,23 @@ public class TestMicroserviceRegisterTask {
     Assert.assertEquals(1, taskList.size());
   }
 
+  /**
+   * Local schemaId set is consistent with online schemaId set, and schema contents are not registered.
+   * This service instance try to register schema content but failed.
+   */
   @Test
   public void testRegisterSchemaFailed(@Mocked ServiceRegistryClient srClient) {
     microservice.addSchema("s1", "");
     microservice.addSchema("exist", "");
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200);
+    ArrayList<GetSchemaResponse> schemaResponses = new ArrayList<>();
+    onlineSchemasHolder.setValue(schemaResponses);
+    GetSchemaResponse schemaResponse = new GetSchemaResponse();
+    schemaResponse.setSchemaId("s1");
+    schemaResponses.add(schemaResponse);
+    schemaResponse.setSchemaId("exist");
+    schemaResponses.add(schemaResponse);
     new Expectations() {
       {
         srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
@@ -116,6 +134,8 @@ public class TestMicroserviceRegisterTask {
         result = "serviceId";
         srClient.registerSchema(anyString, anyString, anyString);
         result = false;
+        srClient.getSchemas("serviceId");
+        this.result = onlineSchemasHolder;
       }
     };
 
@@ -123,23 +143,35 @@ public class TestMicroserviceRegisterTask {
     registerTask.run();
 
     Assert.assertEquals(false, registerTask.isRegistered());
-    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals(false, registerTask.isSchemaIdSetMatch());
     Assert.assertEquals("serviceId", microservice.getServiceId());
     Assert.assertEquals("serviceId", microservice.getInstance().getServiceId());
     Assert.assertEquals(1, taskList.size());
   }
 
+  /**
+   * There is no microservice information in service center.
+   */
   @Test
   public void testRegisterSchemaSuccess(@Mocked ServiceRegistryClient srClient) {
-    microservice.addSchema("s1", "");
+    microservice.addSchema("s1", "s1Content");
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200);
+    ArrayList<GetSchemaResponse> schemaResponseList = new ArrayList<>();
+    onlineSchemasHolder.setValue(schemaResponseList);
+    GetSchemaResponse schemaResponse = new GetSchemaResponse();
+    schemaResponseList.add(schemaResponse);
+    schemaResponse.setSchemaId("s1");
     new Expectations() {
       {
         srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
         result = null;
         srClient.registerMicroservice((Microservice) any);
         result = "serviceId";
-        srClient.registerSchema(anyString, anyString, anyString);
-        result = true;
+        srClient.getSchema("serviceId", "s1");
+        result = "s1Content";
+        srClient.getSchemas("serviceId");
+        result = onlineSchemasHolder;
       }
     };
 
@@ -155,12 +187,16 @@ public class TestMicroserviceRegisterTask {
 
   @Test
   public void testAlreadyRegisteredSchemaIdSetMatch(@Mocked ServiceRegistryClient srClient) {
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200);
     new Expectations() {
       {
         srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
         result = "serviceId";
         srClient.getMicroservice(anyString);
         result = microservice;
+        srClient.getSchemas("serviceId");
+        result = onlineSchemasHolder;
       }
     };
 
@@ -175,30 +211,33 @@ public class TestMicroserviceRegisterTask {
     Assert.assertEquals(1, taskList.size());
   }
 
-  @Test
+  @Test(expected = IllegalStateException.class)
   public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClient srClient) {
     Microservice otherMicroservice = new Microservice();
     otherMicroservice.setAppId(microservice.getAppId());
     otherMicroservice.setServiceName("ms1");
     otherMicroservice.addSchema("s1", "");
-
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200);
+    ArrayList<GetSchemaResponse> schemaResponseList = new ArrayList<>();
+    onlineSchemasHolder.setValue(schemaResponseList);
+    GetSchemaResponse schemaResponse = new GetSchemaResponse();
+
+    schemaResponseList.add(schemaResponse);
+    schemaResponse.setSchemaId("s1");
     new Expectations() {
       {
         srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
         result = "serviceId";
         srClient.getMicroservice(anyString);
         result = otherMicroservice;
+        srClient.getSchemas("serviceId");
+        result = onlineSchemasHolder;
       }
     };
 
     MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
     registerTask.run();
-
-    Assert.assertEquals(true, registerTask.isRegistered());
-    Assert.assertEquals(false, registerTask.isSchemaIdSetMatch());
-    Assert.assertEquals("serviceId", microservice.getServiceId());
-    Assert.assertEquals("serviceId", microservice.getInstance().getServiceId());
-    Assert.assertEquals(1, taskList.size());
   }
 
   @Test
@@ -240,6 +279,8 @@ public class TestMicroserviceRegisterTask {
     resp.setSchemaId("s1");
     resp.setSummary("c1188d709631a9038874f9efc6eb894f");
     list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setStatusCode(200).setValue(list);
 
     new Expectations() {
       {
@@ -248,7 +289,7 @@ public class TestMicroserviceRegisterTask {
         srClient.getMicroservice(anyString);
         result = otherMicroservice;
         srClient.getSchemas(anyString);
-        result = list;
+        result = onlineSchemasHolder;
         srClient.registerSchema(microservice.getServiceId(), anyString, anyString);
         result = true;
       }
@@ -265,18 +306,22 @@ public class TestMicroserviceRegisterTask {
     Assert.assertEquals(1, taskList.size());
   }
 
+  /**
+   * There is microservice information but no schema in service center.
+   */
   @Test
   public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) {
     Microservice otherMicroservice = new Microservice();
     otherMicroservice.setAppId(microservice.getAppId());
     otherMicroservice.setServiceName("ms1");
-    otherMicroservice.addSchema("s1", "");
+    otherMicroservice.addSchema("s1", null);
 
     List<GetSchemaResponse> list = new ArrayList<>();
     GetSchemaResponse resp = new GetSchemaResponse();
     resp.setSchemaId("s1");
-    resp.setSummary(null);
     list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setValue(list).setStatusCode(200);
 
     new Expectations() {
       {
@@ -285,14 +330,16 @@ public class TestMicroserviceRegisterTask {
         srClient.getMicroservice(anyString);
         result = otherMicroservice;
         srClient.getSchemas(anyString);
-        result = list;
-        srClient.registerSchema(microservice.getServiceId(), anyString, anyString);
+        result = onlineSchemasHolder;
+        srClient.getSchema("serviceId", "s1");
+        result = null;
+        srClient.registerSchema("serviceId", "s1", "s1Content");
         result = true;
       }
     };
 
-    microservice.addSchema("s1", "");
-    microservice.getInstance().setEnvironment("production");
+    microservice.addSchema("s1", "s1Content");
+    microservice.setEnvironment("production");
     MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
     registerTask.run();
 
@@ -302,6 +349,9 @@ public class TestMicroserviceRegisterTask {
     Assert.assertEquals(1, taskList.size());
   }
 
+  /**
+   * There is schema in service center which is different from local schema.
+   */
   @Test(expected = IllegalStateException.class)
   public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) {
     Microservice otherMicroservice = new Microservice();
@@ -314,6 +364,8 @@ public class TestMicroserviceRegisterTask {
     resp.setSchemaId("s1");
     resp.setSummary("c1188d709631a9038874f9efc6eb894f");
     list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setValue(list).setStatusCode(200);
 
     new Expectations() {
       {
@@ -322,7 +374,7 @@ public class TestMicroserviceRegisterTask {
         srClient.getMicroservice(anyString);
         result = otherMicroservice;
         srClient.getSchemas(anyString);
-        result = list;
+        result = onlineSchemasHolder;
       }
     };
 
@@ -331,4 +383,92 @@ public class TestMicroserviceRegisterTask {
     MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
     registerTask.run();
   }
+
+  /**
+   * env = production and there are schemas only existing in service center
+   */
+  @Test(expected = IllegalStateException.class)
+  public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) {
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", null);
+    otherMicroservice.addSchema("s2", null);
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    list.add(resp);
+    resp = new GetSchemaResponse();
+    resp.setSchemaId("s2");
+    list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setValue(list).setStatusCode(200);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = onlineSchemasHolder;
+        srClient.getSchema("serviceId", "s1");
+        result = null;
+        srClient.registerSchema("serviceId", "s1", "s1Content");
+        result = true;
+      }
+    };
+
+    microservice.addSchema("s1", "s1Content");
+    microservice.setEnvironment("production");
+    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+  }
+
+  @Test
+  public void testReRegisterForDevAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) {
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", null);
+    otherMicroservice.addSchema("s2", null);
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    list.add(resp);
+    resp = new GetSchemaResponse();
+    resp.setSchemaId("s2");
+    list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setValue(list).setStatusCode(200);
+    Holder<String> removeSchemaResult = new Holder<>();
+    removeSchemaResult.setStatusCode(200);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = onlineSchemasHolder;
+        srClient.getSchema("serviceId", "s1");
+        result = null;
+        srClient.registerSchema("serviceId", "s1", "s1Content");
+        result = true;
+      }
+    };
+
+    microservice.addSchema("s1", "s1Content");
+    microservice.setEnvironment("development");
+    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+
+    Assert.assertEquals(true, registerTask.isRegistered());
+    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals("serviceId", microservice.getServiceId());
+    Assert.assertEquals(1, taskList.size());
+  }
 }

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 01/02: [SCB-652] change environment configuration key to service_description.environment and mark @Deprecated onto microserviceInstance.environment

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit bf70ac3dbf0db4b0d2484daf5b322e4ecd8f3999
Author: yaohaishi <ya...@huawei.com>
AuthorDate: Fri Jun 1 16:50:29 2018 +0800

    [SCB-652] change environment configuration key to service_description.environment and mark @Deprecated onto microserviceInstance.environment
---
 .../foundation/common/base/ServiceCombConstants.java      |  4 ++++
 .../api/registry/MicroserviceInstance.java                |  4 ++++
 .../serviceregistry/task/MicroserviceRegisterTask.java    | 15 +++++++++------
 .../task/TestMicroserviceRegisterTask.java                |  6 +++---
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
index a8bda90..a4f3273 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java
@@ -64,4 +64,8 @@ public interface ServiceCombConstants {
   String SERVICECOMB_ENV = "service_description.environment";
 
   String DEFAULT_SERVICECOMB_ENV = "";
+
+  String DEVELOPMENT_SERVICECOMB_ENV = "development";
+
+  String PRODUCTION_SERVICECOMB_ENV = "production";
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java
index 184aac3..4ce3fb2 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java
@@ -55,6 +55,10 @@ public class MicroserviceInstance {
 
   private HealthCheck healthCheck;
 
+  /**
+   * Will be abandoned, use {@link Microservice#environment} instead
+   */
+  @Deprecated
   private String environment;
 
   private String stage;
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index caa6141..497c014 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -21,6 +21,7 @@ import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.apache.servicecomb.foundation.common.base.ServiceCombConstants;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
@@ -161,13 +162,13 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
           return false;
         }
       } else {
-        String curSchemaSumary = existSchema.getSummary();
+        String curSchemaSummary = existSchema.getSummary();
         String schemaSummary = Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString();
-        if (!schemaSummary.equals(curSchemaSumary)) {
-          if (microservice.getInstance().getEnvironment().equalsIgnoreCase("development")) {
+        if (!schemaSummary.equals(curSchemaSummary)) {
+          if (microservice.getEnvironment().equalsIgnoreCase(ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV)) {
             LOGGER.info(
-                "schemaId [{}]'s content changes and the current enviroment is development, so re-register it!",
-                schemaId);
+                "schemaId [{}]'s content changes and the current environment is {}, so re-register it!",
+                schemaId, ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV);
             if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) {
               return false;
             }
@@ -175,7 +176,9 @@ public class MicroserviceRegisterTask extends AbstractRegisterTask {
             throw new IllegalStateException("schemaId [" + schemaId
                 + "] exists in service center, but the content does not match the local content that means there are interface change "
                 + "and you need to increment microservice version before deploying. "
-                + "Or you can configure instance_description.environment=development to work in development enviroment and ignore this error");
+                + "Or you can configure service_description.environment="
+                + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV
+                + " to work in development environment and ignore this error");
           }
         }
       }
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index 7932d68..e85af2e 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -229,7 +229,7 @@ public class TestMicroserviceRegisterTask {
   @Test
   public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) {
     ArchaiusUtils.resetConfig();
-    ArchaiusUtils.setProperty("instance_description.environment", "development");
+    ArchaiusUtils.setProperty("service_description.environment", "development");
     Microservice otherMicroservice = new Microservice();
     otherMicroservice.setAppId(microservice.getAppId());
     otherMicroservice.setServiceName("ms1");
@@ -255,7 +255,7 @@ public class TestMicroserviceRegisterTask {
     };
 
     microservice.addSchema("s1", "");
-    microservice.getInstance().setEnvironment("development");
+    microservice.setEnvironment("development");
     MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
     registerTask.run();
 
@@ -327,7 +327,7 @@ public class TestMicroserviceRegisterTask {
     };
 
     microservice.addSchema("s1", "");
-    microservice.getInstance().setEnvironment("prod");
+    microservice.setEnvironment("prod");
     MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
     registerTask.run();
   }

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.