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 2020/08/11 02:28:06 UTC
[servicecomb-java-chassis] branch master updated: [SCB-2063]when
configure multiple service centers,
should support using different http client (#1914)
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 d83164b [SCB-2063]when configure multiple service centers, should support using different http client (#1914)
d83164b is described below
commit d83164b0508ed23d5119531ee5092cd7fc3f7bbc
Author: bao liu <bi...@qq.com>
AuthorDate: Tue Aug 11 10:27:54 2020 +0800
[SCB-2063]when configure multiple service centers, should support using different http client (#1914)
---
.../demo/multiServiceCenterClient/Application.java | 25 +++++++-
.../ServerBRegistryHttpClientOptionsSPI.java | 24 +++++---
.../ServerBRegistryWatchHttpClientOptionsSPI.java | 66 ++++++++++++++++++++++
.../ServerBServiceCenterConfiguration.java | 6 +-
...undation.vertx.client.http.HttpClientOptionsSPI | 19 +++++++
.../servicecomb/serviceregistry/RegistryUtils.java | 9 ---
.../serviceregistry/client/IpPortManager.java | 3 +-
.../client/http/HttpClientPool.java | 8 ++-
.../client/http/RestClientUtil.java | 2 +-
.../client/http/WebsocketClientPool.java | 10 ++--
.../client/http/WebsocketClientUtil.java | 2 +-
.../config/ServiceRegistryConfig.java | 22 ++++++++
.../config/ServiceRegistryConfigBuilder.java | 4 ++
.../config/ServiceRegistryConfigCustomizer.java | 10 ++++
14 files changed, 182 insertions(+), 28 deletions(-)
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/Application.java b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/Application.java
index e0d7d65..6998697 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/Application.java
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/Application.java
@@ -17,6 +17,10 @@
package org.apache.servicecomb.demo.multiServiceCenterClient;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
import org.apache.servicecomb.demo.CategorizedTestCaseRunner;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.springboot2.starter.EnableServiceComb;
@@ -39,10 +43,29 @@ public class Application {
public static void runTest() throws Exception {
CategorizedTestCaseRunner.runCategorizedTestCase("demo-multi-service-center-serverA");
-
+ testRegistryThreads();
TestMgr.summary();
if (!TestMgr.errors().isEmpty()) {
throw new IllegalStateException("tests failed");
}
}
+
+ private static void testRegistryThreads() throws Exception {
+ Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
+ List<String> expectedThread = new ArrayList<>();
+ threadSet.forEach(thread -> {
+ if (thread.getName().contains("registry-")) {
+ expectedThread.add(thread.getName());
+ }
+ });
+ //registry-watch-vert.x-eventloop-thread-1
+ //registry-watch-vert.x-eventloop-thread-0
+ //registry-watch-serverB-vert.x-eventloop-thread-1
+ //registry-watch-serverB-vert.x-eventloop-thread-0
+ //registry-vert.x-eventloop-thread-1
+ //registry-vert.x-eventloop-thread-0
+ //registry-serverB-vert.x-eventloop-thread-1
+ //registry-serverB-vert.x-eventloop-thread-0
+ TestMgr.check(expectedThread.size(), 8);
+ }
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryHttpClientOptionsSPI.java
similarity index 62%
copy from service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java
copy to demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryHttpClientOptionsSPI.java
index 6a17fb3..003a962 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryHttpClientOptionsSPI.java
@@ -15,17 +15,25 @@
* limitations under the License.
*/
-package org.apache.servicecomb.serviceregistry.client.http;
+package org.apache.servicecomb.demo.multiServiceCenterClient;
-public class WebsocketClientPool extends AbstractClientPool {
+import org.apache.servicecomb.serviceregistry.client.http.RegistryHttpClientOptionsSPI;
- /**
- * The default instance, for default sc cluster.
- */
- public static final WebsocketClientPool INSTANCE = new WebsocketClientPool();
+public class ServerBRegistryHttpClientOptionsSPI extends RegistryHttpClientOptionsSPI {
+ public static final String CLIENT_NAME = "registry-serverB";
@Override
- public String getName() {
- return RegistryWatchHttpClientOptionsSPI.CLIENT_NAME;
+ public String clientName() {
+ return CLIENT_NAME;
+ }
+
+ @Override
+ public String getConfigTag() {
+ return "sc.serverB.consumer";
+ }
+
+ @Override
+ public boolean isSsl() {
+ return false;
}
}
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryWatchHttpClientOptionsSPI.java b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryWatchHttpClientOptionsSPI.java
new file mode 100644
index 0000000..5c25d01
--- /dev/null
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBRegistryWatchHttpClientOptionsSPI.java
@@ -0,0 +1,66 @@
+/*
+ * 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.demo.multiServiceCenterClient;
+
+import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+public class ServerBRegistryWatchHttpClientOptionsSPI extends ServerBRegistryHttpClientOptionsSPI {
+ public static final String CLIENT_NAME = "registry-watch-serverB";
+
+ private ServiceRegistryConfig serviceRegistryConfig = ServiceRegistryConfig.INSTANCE;
+
+ @Override
+ public String clientName() {
+ return CLIENT_NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ // low priority than registry
+ return super.getOrder() + 1;
+ }
+
+ @Override
+ public boolean enabled() {
+ return serviceRegistryConfig.isWatch();
+ }
+
+ @Override
+ public boolean isWorker() {
+ return true;
+ }
+
+
+ @Override
+ public String getWorkerPoolName() {
+ return "pool-worker-service-center-watch-serverB";
+ }
+
+ @Override
+ public int getWorkerPoolSize() {
+ return DynamicPropertyFactory.getInstance()
+ .getIntProperty(ServiceRegistryConfig.WORKER_POOL_SIZE, 2).get();
+ }
+
+ @Override
+ public boolean isProxyEnable() {
+ return false;
+ }
+}
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBServiceCenterConfiguration.java b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBServiceCenterConfiguration.java
index 4d37b0e..4c13d9b 100644
--- a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBServiceCenterConfiguration.java
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/java/org/apache/servicecomb/demo/multiServiceCenterClient/ServerBServiceCenterConfiguration.java
@@ -28,6 +28,10 @@ public class ServerBServiceCenterConfiguration {
public ServiceRegistryConfig serverBServiceCenterConfig() {
ServiceRegistryConfig config = ServiceRegistryConfig.buildFromConfiguration();
return ServiceRegistryConfigCustomizer.from(config)
- .addressListFromConfiguration("servicecomb.service.registry-serverB.address").get();
+ .addressListFromConfiguration("servicecomb.service.registry-serverB.address")
+ // use a different http client instance
+ .setClientName("registry-serverB")
+ .setWatchClientName("registry-watch-serverB")
+ .get();
}
}
diff --git a/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.vertx.client.http.HttpClientOptionsSPI b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.vertx.client.http.HttpClientOptionsSPI
new file mode 100644
index 0000000..ea85cbe
--- /dev/null
+++ b/demo/demo-multi-service-center/demo-multi-service-center-client/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.vertx.client.http.HttpClientOptionsSPI
@@ -0,0 +1,19 @@
+#
+# 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.demo.multiServiceCenterClient.ServerBRegistryHttpClientOptionsSPI
+org.apache.servicecomb.demo.multiServiceCenterClient.ServerBRegistryWatchHttpClientOptionsSPI
\ No newline at end of file
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
index 0bc7477..11fcd94 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
@@ -254,15 +254,6 @@ public final class RegistryUtils {
}
/**
- * @throws NullPointerException serviceRegistryConfig is null
- * @throws IllegalArgumentException config value is illegal
- */
- public static void validateRegistryConfig(ServiceRegistryConfig serviceRegistryConfig) {
- Objects.requireNonNull(serviceRegistryConfig);
- validateRegistryName(serviceRegistryConfig.getRegistryName());
- }
-
- /**
* To validate whether the name is legal value.
* @param name name of the {@link ServiceRegistry}
* @throws IllegalArgumentException the input value is illegal
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
index 5d299ad..772f157 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
@@ -31,9 +31,9 @@ import org.apache.servicecomb.registry.cache.CacheEndpoint;
import org.apache.servicecomb.registry.cache.InstanceCache;
import org.apache.servicecomb.registry.cache.InstanceCacheManager;
import org.apache.servicecomb.registry.cache.InstanceCacheManagerNew;
-import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.apache.servicecomb.registry.consumer.AppManager;
import org.apache.servicecomb.registry.definition.DefinitionConst;
+import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -73,6 +73,7 @@ public class IpPortManager {
}
int initialIndex = new Random().nextInt(defaultIpPort.size());
currentAvailableIndex = new AtomicInteger(initialIndex);
+ LOGGER.info("Initial service center address is {}", getAvailableAddress());
maxRetryTimes = defaultIpPort.size();
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/HttpClientPool.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/HttpClientPool.java
index fd29788..2aa7bda 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/HttpClientPool.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/HttpClientPool.java
@@ -18,8 +18,14 @@
package org.apache.servicecomb.serviceregistry.client.http;
class HttpClientPool extends AbstractClientPool {
+ private String clientName;
+
+ HttpClientPool(String clientName) {
+ this.clientName = clientName;
+ }
+
@Override
public String getName() {
- return RegistryHttpClientOptionsSPI.CLIENT_NAME;
+ return this.clientName;
}
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RestClientUtil.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RestClientUtil.java
index bf359c6..57f4a86 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RestClientUtil.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RestClientUtil.java
@@ -61,7 +61,7 @@ final class RestClientUtil {
this.authHeaderProviders = serviceRegistryConfig.getAuthHeaderProviders();
this.requestTimeout = serviceRegistryConfig.getRequestTimeout();
this.tenantName = serviceRegistryConfig.getTenantName();
- this.httpClientPool = new HttpClientPool();
+ this.httpClientPool = new HttpClientPool(serviceRegistryConfig.getClientName());
}
public void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java
index 6a17fb3..edfb41a 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientPool.java
@@ -18,14 +18,14 @@
package org.apache.servicecomb.serviceregistry.client.http;
public class WebsocketClientPool extends AbstractClientPool {
+ private String clientName;
- /**
- * The default instance, for default sc cluster.
- */
- public static final WebsocketClientPool INSTANCE = new WebsocketClientPool();
+ WebsocketClientPool(String clientName) {
+ this.clientName = clientName;
+ }
@Override
public String getName() {
- return RegistryWatchHttpClientOptionsSPI.CLIENT_NAME;
+ return this.clientName;
}
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientUtil.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientUtil.java
index 237b231..1ab17e7 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientUtil.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/WebsocketClientUtil.java
@@ -47,7 +47,7 @@ public final class WebsocketClientUtil {
private List<AuthHeaderProvider> authHeaderProviders;
WebsocketClientUtil(ServiceRegistryConfig serviceRegistryConfig) {
- websocketClientPool = new WebsocketClientPool();
+ websocketClientPool = new WebsocketClientPool(serviceRegistryConfig.getWatchClientName());
authHeaderProviders = serviceRegistryConfig.getAuthHeaderProviders();
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
index 43141bc..c295e9e 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
@@ -73,6 +73,10 @@ public class ServiceRegistryConfig {
// TODO SCB-1691 getter of this field's behavior changed, should check
private boolean ssl = true;
+ private String clientName;
+
+ private String watchClientName;
+
private ArrayList<IpPort> ipPort;
private int connectionTimeout;
@@ -175,6 +179,24 @@ public class ServiceRegistryConfig {
return this;
}
+ public String getClientName() {
+ return this.clientName;
+ }
+
+ public ServiceRegistryConfig setClientName(String clientName) {
+ this.clientName = clientName;
+ return this;
+ }
+
+ public String getWatchClientName() {
+ return this.watchClientName;
+ }
+
+ public ServiceRegistryConfig setWatchClientName(String watchClientName) {
+ this.watchClientName = watchClientName;
+ return this;
+ }
+
public ArrayList<IpPort> getIpPort() {
return ipPort;
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
index de8d13e..14de91a 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
@@ -29,6 +29,8 @@ import org.apache.servicecomb.foundation.common.net.IpPort;
import org.apache.servicecomb.foundation.common.net.NetUtils;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.apache.servicecomb.foundation.vertx.VertxConst;
+import org.apache.servicecomb.serviceregistry.client.http.RegistryHttpClientOptionsSPI;
+import org.apache.servicecomb.serviceregistry.client.http.RegistryWatchHttpClientOptionsSPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,6 +52,8 @@ class ServiceRegistryConfigBuilder {
.setInstances(getInstances())
.setIpPort(getIpPort())
.setSsl(isSsl())
+ .setClientName(RegistryHttpClientOptionsSPI.CLIENT_NAME)
+ .setWatchClientName(RegistryWatchHttpClientOptionsSPI.CLIENT_NAME)
.setConnectionTimeout(getConnectionTimeout())
.setIdleConnectionTimeout(getIdleConnectionTimeout())
.setRequestTimeout(getRequestTimeout())
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigCustomizer.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigCustomizer.java
index b45db80..5ffdd31 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigCustomizer.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigCustomizer.java
@@ -41,6 +41,16 @@ public class ServiceRegistryConfigCustomizer {
return new ServiceRegistryConfigCustomizer(original);
}
+ public ServiceRegistryConfigCustomizer setClientName(String clientName) {
+ this.original.setClientName(clientName);
+ return this;
+ }
+
+ public ServiceRegistryConfigCustomizer setWatchClientName(String watchClientName) {
+ this.original.setWatchClientName(watchClientName);
+ return this;
+ }
+
public ServiceRegistryConfigCustomizer addressListFromConfiguration(String configuration) {
String address = DynamicPropertyFactory.getInstance()
.getStringProperty(configuration, null)