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/02/08 08:27:08 UTC

[incubator-servicecomb-java-chassis] 03/03: use SPI to read version, so can read version from cse or spring cloud too.

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 7b99da6f3cf6fd08d42866ac149371488cc1d3d2
Author: laijianbin <la...@huawei.com>
AuthorDate: Wed Feb 7 22:11:31 2018 +0800

    use SPI to read version,so can read version from cse or spring cloud too.
---
 pom.xml                                            | 24 ++++++------
 .../servicecomb/serviceregistry/api/Versions.java  | 23 ++++++++++++
 .../api/registry/FrameworkVersions.java            | 43 ++++++++++++++++++++++
 .../api/registry/MicroserviceFactory.java          |  2 +-
 .../api/registry/ServiceCombVersion.java           | 33 +++++++++++++++++
 ...apache.servicecomb.serviceregistry.api.Versions | 18 +++++++++
 .../api/registry/TestFrameworkVersions.java        | 29 +++++++++++++++
 .../api/registry/TestServiceCombVersion.java       | 30 +++++++++++++++
 8 files changed, 189 insertions(+), 13 deletions(-)

diff --git a/pom.xml b/pom.xml
index 51090e5..f0e1171 100644
--- a/pom.xml
+++ b/pom.xml
@@ -253,6 +253,18 @@
           <excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <configuration>
+          <archive>
+            <manifest>
+              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
+            </manifest>
+          </archive>
+        </configuration>
+      </plugin>
     </plugins>
     <pluginManagement>
       <plugins>
@@ -266,18 +278,6 @@
             </jacocoReports>
           </configuration>
         </plugin>
-        <plugin>
-          <groupId>org.apache.maven.plugins</groupId>
-          <artifactId>maven-jar-plugin</artifactId>
-          <configuration>
-            <archive>
-              <manifest>
-                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
-                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
-              </manifest>
-            </archive>
-          </configuration>
-        </plugin>
       </plugins>
     </pluginManagement>
   </build>
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Versions.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Versions.java
new file mode 100644
index 0000000..5e4adfb
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Versions.java
@@ -0,0 +1,23 @@
+/*
+ * 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.api;
+
+import java.util.Map;
+
+public interface Versions {
+  public Map<String, String> loadVersion();
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java
new file mode 100644
index 0000000..5c87855
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java
@@ -0,0 +1,43 @@
+/*
+ * 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.api.registry;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.ServiceLoader;
+
+import org.apache.servicecomb.serviceregistry.api.Versions;
+
+public class FrameworkVersions {
+  private static final ServiceLoader<Versions> frameworkVersions = ServiceLoader.load(Versions.class);
+
+  public static String allVersions() {
+    Map<String, String> versions = new HashMap<>();
+    Entry<String, String> entry;
+    StringBuffer sb = new StringBuffer();
+
+    frameworkVersions.forEach(version -> versions.putAll(version.loadVersion()));
+    for (Iterator<Entry<String, String>> iterator = versions.entrySet().iterator(); iterator.hasNext();) {
+      entry = (Entry<String, String>) iterator.next();
+      sb.append(entry.getKey()).append(":").append(entry.getValue())
+        .append(iterator.hasNext() ? ";" : "");
+    }
+    return sb.toString();
+  }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceFactory.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceFactory.java
index 82a8f09..5c0c973 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceFactory.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceFactory.java
@@ -70,7 +70,7 @@ public class MicroserviceFactory {
     // use default values, we can add configure item in future.
     Framework framework = new Framework();
     framework.setName(CONFIG_FRAMEWORK_DEFAULT_NAME);
-    framework.setVersion("ServiceComb:" + MicroserviceFactory.class.getPackage().getImplementationVersion());
+    framework.setVersion(FrameworkVersions.allVersions());
     microservice.setFramework(framework);
     microservice.setRegisterBy(CONFIG_DEFAULT_REGISTER_BY);
 
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/ServiceCombVersion.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/ServiceCombVersion.java
new file mode 100644
index 0000000..1e6aa43
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/ServiceCombVersion.java
@@ -0,0 +1,33 @@
+/*
+ * 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.api.registry;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.servicecomb.serviceregistry.api.Versions;
+
+public class ServiceCombVersion implements Versions {
+
+  @Override
+  public Map<String, String> loadVersion() {
+    Map<String, String> map = new HashMap<>();
+    map.put("ServiceComb", this.getClass().getPackage().getImplementationVersion());
+
+    return map;
+  }
+}
diff --git a/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.api.Versions b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.api.Versions
new file mode 100644
index 0000000..006f229
--- /dev/null
+++ b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.api.Versions
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.serviceregistry.api.registry.ServiceCombVersion
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestFrameworkVersions.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestFrameworkVersions.java
new file mode 100644
index 0000000..d02bd55
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestFrameworkVersions.java
@@ -0,0 +1,29 @@
+/*
+ * 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.api.registry;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestFrameworkVersions {
+
+  @Test
+  public void testFrameworkVersions() {
+    Assert.assertEquals("ServiceComb:null", FrameworkVersions.allVersions());
+  }
+}
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCombVersion.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCombVersion.java
new file mode 100644
index 0000000..62e722c
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCombVersion.java
@@ -0,0 +1,30 @@
+/*
+ * 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.api.registry;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestServiceCombVersion {
+
+  @Test
+  public void testServiceCombVersion() {
+    ServiceCombVersion version = new ServiceCombVersion();
+    Assert.assertEquals("{ServiceComb=null}", version.loadVersion().toString());
+  }
+}

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