You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ya...@apache.org on 2020/03/09 16:14:52 UTC
[servicecomb-java-chassis] 03/19: [SCB-1691] add name for
ServiceRegistry
This is an automated email from the ASF dual-hosted git repository.
yaohaishi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit bf3292b0c8e36c5cf698ba9328e149dab49d1c2c
Author: yhs0092 <yh...@163.com>
AuthorDate: Sat Jan 4 15:46:37 2020 +0800
[SCB-1691] add name for ServiceRegistry
---
.../servicecomb/serviceregistry/RegistryUtils.java | 17 ++++++
.../serviceregistry/ServiceRegistry.java | 11 ++++
.../config/ServiceRegistryConfig.java | 13 ++++-
.../registry/AbstractServiceRegistry.java | 13 +++++
.../registry/RemoteServiceRegistry.java | 3 +-
.../serviceregistry/ServiceRegistryTest.java | 68 ++++++++++++++++++++++
.../registry/TestRemoteServiceRegistry.java | 2 +
7 files changed, 125 insertions(+), 2 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 400adc5..46de489 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
@@ -22,6 +22,8 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.regex.Matcher;
import org.apache.http.client.utils.URIBuilder;
import org.apache.servicecomb.config.ConfigUtil;
@@ -259,4 +261,19 @@ public final class RegistryUtils {
public static Microservice getAggregatedRemoteMicroservice(String microserviceId) {
return serviceRegistry.getAggregatedRemoteMicroservice(microserviceId);
}
+
+ /**
+ * To validate whether the name is legal value.
+ * @param name name of the {@link ServiceRegistry}
+ * @throws IllegalArgumentException the input value is illegal
+ */
+ public static void validateRegistryName(String name) {
+ Objects.requireNonNull(name, "null value is not allowed for the name of ServiceRegistry");
+ Matcher checkMatcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher(name);
+ boolean isNameValid = checkMatcher.matches();
+ if (!isNameValid) {
+ throw new IllegalArgumentException(
+ "Illegal registry name, the format should be " + ServiceRegistry.REGISTRY_NAME_FORMAT);
+ }
+ }
}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
index d119cb8..0e6e1a2 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.serviceregistry;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
@@ -28,6 +29,16 @@ import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
import com.google.common.eventbus.EventBus;
public interface ServiceRegistry {
+ String DEFAULT_REGISTRY_NAME = "Default";
+ String REGISTRY_NAME_FORMAT = "[a-zA-Z]([-_]?[a-zA-Z0-9])+";
+ Pattern REGISTRY_NAME_PATTERN = Pattern.compile(REGISTRY_NAME_FORMAT);
+
+ /**
+ * Get a name representing this ServiceRegistry instance.
+ * The name should be unique.
+ */
+ String getName();
+
void init();
void run();
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
index 2badf45..9d5e440 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
@@ -26,6 +26,7 @@ import org.apache.servicecomb.deployment.Deployment;
import org.apache.servicecomb.deployment.DeploymentProvider;
import org.apache.servicecomb.foundation.common.net.IpPort;
import org.apache.servicecomb.foundation.common.net.NetUtils;
+import org.apache.servicecomb.serviceregistry.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -98,6 +99,8 @@ public final class ServiceRegistryConfig {
public static final String WORKER_POOL_NAME = "registry-vert.x-worker-thread";
+ private String registryName = ServiceRegistry.DEFAULT_REGISTRY_NAME;
+
private ServiceRegistryConfig() {
}
@@ -124,7 +127,6 @@ public final class ServiceRegistryConfig {
return deployInstances;
}
-
public boolean isSsl() {
getIpPort();
return this.ssl;
@@ -317,6 +319,15 @@ public final class ServiceRegistryConfig {
return passwd;
}
+ public String getRegistryName() {
+ return registryName;
+ }
+
+ public ServiceRegistryConfig setRegistryName(String registryName) {
+ this.registryName = registryName;
+ return this;
+ }
+
private String getProperty(String defaultValue, String... keys) {
String property = null;
for (String key : keys) {
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
index a842db5..baf25dc 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
@@ -81,8 +81,11 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry {
protected ExecutorService executorService = MoreExecutors.newDirectExecutorService();
+ private String name;
+
public AbstractServiceRegistry(EventBus eventBus, ServiceRegistryConfig serviceRegistryConfig,
MicroserviceDefinition microserviceDefinition) {
+ setName(serviceRegistryConfig.getRegistryName());
this.eventBus = eventBus;
this.serviceRegistryConfig = serviceRegistryConfig;
this.microserviceDefinition = microserviceDefinition;
@@ -312,6 +315,16 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry {
registerMicroserviceMapping(microserviceName, version, microserviceInstances, schemaIntfCls);
}
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ void setName(String name) {
+ RegistryUtils.validateRegistryName(name);
+ this.name = name;
+ }
+
@Subscribe
public void onShutdown(ShutdownEvent event) {
LOGGER.info("service center task is shutdown.");
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
index 123017c..dfeee99 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
@@ -58,7 +58,8 @@ public class RemoteServiceRegistry extends AbstractServiceRegistry {
@Override
public Thread newThread(Runnable r) {
- Thread thread = new Thread(r, "Service Center Task [" + (taskId++) + "]");
+ Thread thread = new Thread(r,
+ RemoteServiceRegistry.super.getName() + " Service Center Task [" + (taskId++) + "]");
thread.setUncaughtExceptionHandler(
(t, e) -> LOGGER.error("Service Center Task Thread is terminated! thread: [{}]", t, e));
return thread;
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/ServiceRegistryTest.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/ServiceRegistryTest.java
new file mode 100644
index 0000000..3477fb6
--- /dev/null
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/ServiceRegistryTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.regex.Matcher;
+
+import org.junit.Test;
+
+public class ServiceRegistryTest {
+ @Test
+ public void testNameFormat() {
+ Matcher matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc");
+ assertTrue(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc00");
+ assertTrue(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ABC");
+ assertTrue(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("A2BC");
+ assertTrue(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc-ABC");
+ assertTrue(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc_ABC");
+ assertTrue(matcher.matches());
+
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("-abc");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc-");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("_abc");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("abc_");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("0abc");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab.c");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab?c");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab#c");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab&c");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab*c");
+ assertFalse(matcher.matches());
+ matcher = ServiceRegistry.REGISTRY_NAME_PATTERN.matcher("ab@c");
+ assertFalse(matcher.matches());
+ }
+}
\ No newline at end of file
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
index d82fc75..c035aae 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
@@ -90,6 +90,8 @@ public class TestRemoteServiceRegistry {
result = 30;
config.isWatch();
result = false;
+ config.getRegistryName();
+ result = "TestRegistry";
SPIServiceUtils.getOrLoadSortedService(ServiceRegistryTaskInitializer.class);
result = Arrays.asList(initializer);
}