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 2021/02/08 01:06:54 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2184]common http client api refactor to support users need (#2231)

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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 45d89bc  [SCB-2184]common http client api refactor to support users need (#2231)
45d89bc is described below

commit 45d89bc9e5b6898440a401f5413a4fb126bb2c7e
Author: bao liu <bi...@qq.com>
AuthorDate: Mon Feb 8 09:06:46 2021 +0800

    [SCB-2184]common http client api refactor to support users need (#2231)
---
 clients/config-center-client/pom.xml               |  5 ++
 .../config/center/client/ConfigCenterManager.java  | 20 ++++--
 .../center/client/ConfigurationChangedEvent.java   | 37 ----------
 .../{config-kie-client => config-common}/pom.xml   |  4 +-
 .../config/common/ConfigurationChangedEvent.java   | 82 ++++++++++++++++++++++
 .../common/TestConfigurationChangedEvent.java}     | 32 +++++----
 clients/config-kie-client/pom.xml                  |  4 ++
 .../config/kie/client/KieConfigManager.java        | 18 ++++-
 clients/pom.xml                                    |  3 +-
 .../center/client/ServiceCenterRegistration.java   | 15 +++-
 coverage-reports/pom.xml                           |  4 ++
 dependencies/bom/pom.xml                           |  5 ++
 distribution/pom.xml                               |  4 ++
 .../properties/GovernanceProperties.java           |  2 +-
 .../governance/GovernancePropertiesTest.java       |  1 +
 15 files changed, 173 insertions(+), 63 deletions(-)

diff --git a/clients/config-center-client/pom.xml b/clients/config-center-client/pom.xml
index 4fdf213..382ccc5 100644
--- a/clients/config-center-client/pom.xml
+++ b/clients/config-center-client/pom.xml
@@ -38,6 +38,11 @@
     </dependency>
 
     <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>config-clients-common</artifactId>
+    </dependency>
+
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>
diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
index 32edd16..ab63198 100644
--- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
+++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
@@ -17,15 +17,19 @@
 
 package org.apache.servicecomb.config.center.client;
 
+import java.util.Collections;
+import java.util.Map;
+
 import org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest;
 import org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse;
+import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
 import org.apache.servicecomb.http.client.task.AbstractTask;
 import org.apache.servicecomb.http.client.task.Task;
-
-import com.google.common.eventbus.EventBus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.eventbus.EventBus;
+
 public class ConfigCenterManager extends AbstractTask {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCenterManager.class);
@@ -38,10 +42,18 @@ public class ConfigCenterManager extends AbstractTask {
 
   private QueryConfigurationsRequest queryConfigurationsRequest;
 
+  private Map<String, Object> lastConfiguration;
+
   public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus) {
+    this(configCenterClient, eventBus, Collections.emptyMap());
+  }
+
+  public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus,
+      Map<String, Object> lastConfiguration) {
     super("config-center-configuration-task");
     this.configCenterClient = configCenterClient;
     this.eventBus = eventBus;
+    this.lastConfiguration = lastConfiguration;
   }
 
   public void setQueryConfigurationsRequest(QueryConfigurationsRequest queryConfigurationsRequest) {
@@ -65,7 +77,8 @@ public class ConfigCenterManager extends AbstractTask {
         QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest);
         if (response.isChanged()) {
           queryConfigurationsRequest.setRevision(response.getRevision());
-          eventBus.post(new ConfigurationChangedEvent(response.getConfigurations()));
+          eventBus.post(ConfigurationChangedEvent.createIncremental(response.getConfigurations(), lastConfiguration));
+          lastConfiguration = response.getConfigurations();
         }
         startTask(new BackOffSleepTask(POLL_INTERVAL, new PollConfigurationTask(0)));
       } catch (Exception e) {
@@ -73,6 +86,5 @@ public class ConfigCenterManager extends AbstractTask {
         startTask(new BackOffSleepTask(failCount + 1, new PollConfigurationTask(failCount + 1)));
       }
     }
-
   }
 }
diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigurationChangedEvent.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigurationChangedEvent.java
deleted file mode 100644
index 235c6b1..0000000
--- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigurationChangedEvent.java
+++ /dev/null
@@ -1,37 +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.config.center.client;
-
-import java.util.Map;
-
-public class ConfigurationChangedEvent {
-  private Map<String, Object> configurations;
-
-  public ConfigurationChangedEvent(Map<String, Object> configurations) {
-    this.configurations = configurations;
-  }
-
-  public Map<String, Object> getConfigurations() {
-    return configurations;
-  }
-
-  public ConfigurationChangedEvent setConfigurations(Map<String, Object> configurations) {
-    this.configurations = configurations;
-    return this;
-  }
-}
diff --git a/clients/config-kie-client/pom.xml b/clients/config-common/pom.xml
similarity index 93%
copy from clients/config-kie-client/pom.xml
copy to clients/config-common/pom.xml
index 21c6898..f284d6c 100644
--- a/clients/config-kie-client/pom.xml
+++ b/clients/config-common/pom.xml
@@ -26,8 +26,8 @@
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
-    <artifactId>config-kie-client</artifactId>
-    <name>ServiceComb::Clients::Kie Client</name>
+    <artifactId>config-clients-common</artifactId>
+    <name>ServiceComb::Clients::Config Common</name>
 
     <dependencies>
         <dependency>
diff --git a/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java b/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java
new file mode 100644
index 0000000..6f0ca00
--- /dev/null
+++ b/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java
@@ -0,0 +1,82 @@
+/*
+ * 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.config.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ConfigurationChangedEvent {
+  private final Map<String, Object> added;
+
+  private final Map<String, Object> deleted;
+
+  private final Map<String, Object> updated;
+
+  private ConfigurationChangedEvent(Map<String, Object> added, Map<String, Object> updated,
+      Map<String, Object> deleted) {
+    this.added = added;
+    this.deleted = deleted;
+    this.updated = updated;
+  }
+
+  public static ConfigurationChangedEvent createIncremental(Map<String, Object> latest, Map<String, Object> last) {
+    Map<String, Object> itemsCreated = new HashMap<>();
+    Map<String, Object> itemsDeleted = new HashMap<>();
+    Map<String, Object> itemsModified = new HashMap<>();
+
+    for (String itemKey : latest.keySet()) {
+      if (!last.containsKey(itemKey)) {
+        itemsCreated.put(itemKey, latest.get(itemKey));
+      } else if (!last.get(itemKey).equals(latest.get(itemKey))) {
+        itemsModified.put(itemKey, latest.get(itemKey));
+      }
+    }
+    for (String itemKey : last.keySet()) {
+      if (!latest.containsKey(itemKey)) {
+        itemsDeleted.put(itemKey, "");
+      }
+    }
+    return ConfigurationChangedEvent.createIncremental(itemsCreated, itemsModified, itemsDeleted);
+  }
+
+  public static ConfigurationChangedEvent createIncremental(Map<String, Object> added, Map<String, Object> updated,
+      Map<String, Object> deleted) {
+    return new ConfigurationChangedEvent(added, updated, deleted);
+  }
+
+  public final Map<String, Object> getAdded() {
+    return added;
+  }
+
+
+  public final Map<String, Object> getUpdated() {
+    return updated;
+  }
+
+
+  public final Map<String, Object> getDeleted() {
+    return deleted;
+  }
+
+  public final Map<String, Object> getComplete() {
+    Map<String, Object> result = new HashMap<>(added.size() + updated.size());
+    result.putAll(added);
+    result.putAll(updated);
+    return result;
+  }
+}
diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigChangedEvent.java b/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
similarity index 52%
rename from clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigChangedEvent.java
rename to clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
index ab060d7..7e3e81e 100644
--- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigChangedEvent.java
+++ b/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
@@ -15,23 +15,27 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.config.kie.client;
+package org.apache.servicecomb.config.common;
 
+import java.util.HashMap;
 import java.util.Map;
 
-public class KieConfigChangedEvent {
-  private Map<String, Object> configurations;
+import org.junit.Assert;
+import org.junit.Test;
 
-  public KieConfigChangedEvent(Map<String, Object> configurations) {
-    this.configurations = configurations;
-  }
-
-  public Map<String, Object> getConfigurations() {
-    return configurations;
-  }
-
-  public KieConfigChangedEvent setConfigurations(Map<String, Object> configurations) {
-    this.configurations = configurations;
-    return this;
+public class TestConfigurationChangedEvent {
+  @Test
+  public void testConfigurationChangedEvent() {
+    Map<String, Object> before = new HashMap<>();
+    Map<String, Object> after = new HashMap<>();
+    before.put("updated", "1");
+    before.put("deleted", "1");
+    after.put("added", 1);
+    after.put("updated", 2);
+    ConfigurationChangedEvent event = ConfigurationChangedEvent.createIncremental(after, before);
+    Assert.assertEquals(1, event.getAdded().size());
+    Assert.assertEquals(1, event.getDeleted().size());
+    Assert.assertEquals(1, event.getUpdated().size());
+    Assert.assertEquals(2, event.getComplete().size());
   }
 }
diff --git a/clients/config-kie-client/pom.xml b/clients/config-kie-client/pom.xml
index 21c6898..cee7c13 100644
--- a/clients/config-kie-client/pom.xml
+++ b/clients/config-kie-client/pom.xml
@@ -35,6 +35,10 @@
             <artifactId>http-client-common</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.servicecomb</groupId>
+            <artifactId>config-clients-common</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>
         </dependency>
diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
index 790d2f6..f636ffe 100644
--- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
+++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
@@ -17,7 +17,10 @@
 
 package org.apache.servicecomb.config.kie.client;
 
-import com.google.common.eventbus.EventBus;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
 import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest;
 import org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse;
 import org.apache.servicecomb.http.client.task.AbstractTask;
@@ -25,6 +28,8 @@ import org.apache.servicecomb.http.client.task.Task;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.eventbus.EventBus;
+
 public class KieConfigManager extends AbstractTask {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(KieConfigManager.class);
@@ -37,10 +42,18 @@ public class KieConfigManager extends AbstractTask {
 
   private ConfigurationsRequest configurationsRequest;
 
+  private Map<String, Object> lastConfiguration;
+
   public KieConfigManager(KieConfigOperation configKieClient, EventBus eventBus) {
+    this(configKieClient, eventBus, Collections.emptyMap());
+  }
+
+  public KieConfigManager(KieConfigOperation configKieClient, EventBus eventBus,
+      Map<String, Object> lastConfiguration) {
     super("config-center-configuration-task");
     this.configKieClient = configKieClient;
     this.eventBus = eventBus;
+    this.lastConfiguration = lastConfiguration;
   }
 
   public void setConfigurationsRequest(ConfigurationsRequest configurationsRequest) {
@@ -65,7 +78,8 @@ public class KieConfigManager extends AbstractTask {
         if (response.isChanged()) {
           LOGGER.info("The configurations are change, will refresh local configurations.");
           configurationsRequest.setRevision(response.getRevision());
-          eventBus.post(new KieConfigChangedEvent(response.getConfigurations()));
+          eventBus.post(ConfigurationChangedEvent.createIncremental(response.getConfigurations(), lastConfiguration));
+          lastConfiguration = response.getConfigurations();
         }
         startTask(new BackOffSleepTask(POLL_INTERVAL, new PollConfigurationTask(0)));
       } catch (Exception e) {
diff --git a/clients/pom.xml b/clients/pom.xml
index 8809408..64a6482 100644
--- a/clients/pom.xml
+++ b/clients/pom.xml
@@ -33,8 +33,9 @@
   <packaging>pom</packaging>
   <modules>
     <module>http-client-common</module>
-    <module>config-kie-client</module>
     <module>service-center-client</module>
+    <module>config-common</module>
+    <module>config-kie-client</module>
     <module>config-center-client</module>
   </modules>
 
diff --git a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
index 37c3f80..dbad8f0 100644
--- a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
+++ b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
@@ -95,13 +95,20 @@ public class ServiceCenterRegistration extends AbstractTask {
             eventBus.post(new MicroserviceRegistrationEvent(true));
             startTask(new RegisterSchemaTask(0));
           }
+          return;
         } else {
-          LOGGER.info("service has already registered, will not register schema.");
+          Microservice newMicroservice = serviceCenterClient.getMicroserviceByServiceId(serviceResponse.getServiceId());
+          if (!isListEquals(newMicroservice.getSchemas(), microservice.getSchemas())) {
+            throw new IllegalStateException("Service has already registered, but schema ids not equal, stop register. "
+                + "Change the microservice version or delete the old microservice info and try again.");
+          }
           microservice.setServiceId(serviceResponse.getServiceId());
           microserviceInstance.setServiceId(serviceResponse.getServiceId());
           eventBus.post(new MicroserviceRegistrationEvent(true));
-          startTask(new RegisterMicroserviceInstanceTask(0));
+          startTask(new RegisterSchemaTask(0));
         }
+      } catch (IllegalStateException e) {
+        throw e;
       } catch (Exception e) {
         LOGGER.error("register microservice failed, and will try again.", e);
         eventBus.post(new MicroserviceRegistrationEvent(false));
@@ -110,6 +117,10 @@ public class ServiceCenterRegistration extends AbstractTask {
     }
   }
 
+  private boolean isListEquals(List<String> one, List<String> two) {
+    return one.size() == two.size() && one.containsAll(two) && two.containsAll(one);
+  }
+
   class RegisterSchemaTask implements Task {
     int failedCount;
 
diff --git a/coverage-reports/pom.xml b/coverage-reports/pom.xml
index ccbc87d..e696734 100644
--- a/coverage-reports/pom.xml
+++ b/coverage-reports/pom.xml
@@ -56,6 +56,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
+      <artifactId>config-clients-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
       <artifactId>config-kie-client</artifactId>
     </dependency>
     <dependency>
diff --git a/dependencies/bom/pom.xml b/dependencies/bom/pom.xml
index cff62e3..f00213d 100644
--- a/dependencies/bom/pom.xml
+++ b/dependencies/bom/pom.xml
@@ -64,6 +64,11 @@
       </dependency>
       <dependency>
         <groupId>org.apache.servicecomb</groupId>
+        <artifactId>config-clients-common</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.servicecomb</groupId>
         <artifactId>config-kie-client</artifactId>
         <version>${project.version}</version>
       </dependency>
diff --git a/distribution/pom.xml b/distribution/pom.xml
index f9e2349..607ac29 100644
--- a/distribution/pom.xml
+++ b/distribution/pom.xml
@@ -54,6 +54,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
+      <artifactId>config-clients-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
       <artifactId>config-kie-client</artifactId>
     </dependency>
     <dependency>
diff --git a/governance/src/main/java/org/apache/servicecomb/governance/properties/GovernanceProperties.java b/governance/src/main/java/org/apache/servicecomb/governance/properties/GovernanceProperties.java
index 60423d2..55e14cb 100644
--- a/governance/src/main/java/org/apache/servicecomb/governance/properties/GovernanceProperties.java
+++ b/governance/src/main/java/org/apache/servicecomb/governance/properties/GovernanceProperties.java
@@ -82,7 +82,7 @@ public abstract class GovernanceProperties<T extends Configurable> implements In
       if (key.startsWith(configKey + ".")) {
         String mapKey = key.substring((configKey + ".").length());
         parsedEntity.remove(mapKey);
-        T entityItem = parseEntityItem(key, environment.getProperty(key));
+        T entityItem = parseEntityItem(mapKey, environment.getProperty(key));
         if (entityItem != null) {
           parsedEntity.put(mapKey, entityItem);
         }
diff --git a/governance/src/test/java/org/apache/servicecomb/governance/GovernancePropertiesTest.java b/governance/src/test/java/org/apache/servicecomb/governance/GovernancePropertiesTest.java
index e2a7f7f..89842c9 100644
--- a/governance/src/test/java/org/apache/servicecomb/governance/GovernancePropertiesTest.java
+++ b/governance/src/test/java/org/apache/servicecomb/governance/GovernancePropertiesTest.java
@@ -189,6 +189,7 @@ public class GovernancePropertiesTest {
     policy = policies.get("bulkhead1");
     Assert.assertEquals(3, policy.getMaxConcurrentCalls());
     Assert.assertEquals(3000, policy.getMaxWaitDuration());
+    Assert.assertEquals("bulkhead1", policy.getName());
   }
 
   @Test