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 2a355c5 [SCB-1844] refactor: modify config-cc module to use the new
mechanism to intialize client
2a355c5 is described below
commit 2a355c5c2c8a4097ccf065914f4293991108ade7
Author: liubao <[email protected]>
AuthorDate: Thu Apr 2 09:31:24 2020 +0800
[SCB-1844] refactor: modify config-cc module to use the new mechanism to
intialize client
---
.../core}/ConfigurationSpringInitializer.java | 11 +-
.../servicecomb/core/CseApplicationListener.java | 4 +
.../org/apache/servicecomb/core/SCBEngine.java | 3 +-
.../main/resources/META-INF/spring/cse.bean.xml | 6 +-
.../core}/TestConfigurationSpringInitializer.java | 3 +-
.../test/resources/META-INF/spring/cse.bean.xml | 3 -
core/src/test/resources/microservice.yaml | 4 +
.../config/client/ConfigCenterClient.java | 80 ++------------
.../config/client/ConfigCenterConfig.java | 22 +++-
.../client/ConfigCenterHttpClientOptionsSPI.java | 119 +++++++++++++++++++++
.../config/client/TestConfigCenterClient.java | 44 ++++----
.../main/resources/META-INF/spring/cse.bean.xml | 1 -
.../foundation/vertx/client/http/HttpClients.java | 25 ++++-
.../config/ServiceRegistryConfigBuilder.java | 6 --
14 files changed, 213 insertions(+), 118 deletions(-)
diff --git
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationSpringInitializer.java
b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
similarity index 95%
rename from
foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationSpringInitializer.java
rename to
core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
index bf380f6..612e995 100644
---
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationSpringInitializer.java
+++
b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.servicecomb.config;
+package org.apache.servicecomb.core;
import java.io.IOException;
import java.util.HashMap;
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Properties;
import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.servicecomb.config.ConfigUtil;
import org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.slf4j.Logger;
@@ -203,7 +204,9 @@ public class ConfigurationSpringInitializer extends
PropertyPlaceholderConfigure
if (ignoreResolveFailure()) {
LOGGER.warn("set up spring property source failed.", e);
} else {
- throw new RuntimeException("set up spring property source
failed.If you still want to start up the application and ignore errors, you can
set servicecomb.config.ignoreResolveFailure to true.", e);
+ throw new RuntimeException(
+ "set up spring property source failed.If you still want to
start up the application and ignore errors, you can set
servicecomb.config.ignoreResolveFailure to true.",
+ e);
}
}
}
@@ -215,7 +218,7 @@ public class ConfigurationSpringInitializer extends
PropertyPlaceholderConfigure
private boolean ignoreResolveFailure() {
return ConfigUtil
- .createLocalConfig()
- .getBoolean("servicecomb.config.ignoreResolveFailure", false);
+ .createLocalConfig()
+ .getBoolean("servicecomb.config.ignoreResolveFailure", false);
}
}
diff --git
a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
index a3153ff..b840d8a 100644
--- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
+++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
@@ -38,6 +38,10 @@ public class CseApplicationListener
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
+ if(this.applicationContext == applicationContext) {
+ // same object. avoid initialize many times.
+ return;
+ }
this.applicationContext = applicationContext;
BeanUtils.setContext(applicationContext);
HttpClients.load();
diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index 04234c2..3f2a2fc 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -420,8 +420,7 @@ public class SCBEngine {
priorityPropertyManager.close();
//Step 6: Stop vertx to prevent blocking exit
- // delete the following two lines when every refactor is done.
- VertxUtils.blockCloseVertxByName("config-center");
+ // delete the following one line when every refactor is done.
VertxUtils.blockCloseVertxByName("transport");
HttpClients.destroy();
diff --git a/core/src/main/resources/META-INF/spring/cse.bean.xml
b/core/src/main/resources/META-INF/spring/cse.bean.xml
index 44d8881..86568ec 100644
--- a/core/src/main/resources/META-INF/spring/cse.bean.xml
+++ b/core/src/main/resources/META-INF/spring/cse.bean.xml
@@ -25,8 +25,10 @@
<!-- <context:spring-configured /> -->
<context:component-scan
base-package="${scb-scan-package:org.apache.servicecomb}"/>
- <bean class="org.apache.servicecomb.core.CseApplicationListener">
- </bean>
+ <!-- initializer doing before any bean -->
+ <bean class="org.apache.servicecomb.core.ConfigurationSpringInitializer"/>
+ <!-- initializer after any bean initialized -->
+ <bean class="org.apache.servicecomb.core.CseApplicationListener"/>
<bean id="cse.executor.groupThreadPool"
class="org.apache.servicecomb.core.executor.GroupExecutor"
init-method="init"/>
diff --git
a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigurationSpringInitializer.java
b/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java
similarity index 99%
rename from
foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigurationSpringInitializer.java
rename to
core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java
index ac20e23..b98d582 100644
---
a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/TestConfigurationSpringInitializer.java
+++
b/core/src/test/java/org/apache/servicecomb/core/TestConfigurationSpringInitializer.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.servicecomb.config;
+package org.apache.servicecomb.core;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -30,6 +30,7 @@ import java.util.Map.Entry;
import org.apache.commons.configuration.Configuration;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import org.apache.servicecomb.config.ConfigUtil;
import org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader;
import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import org.junit.After;
diff --git a/core/src/test/resources/META-INF/spring/cse.bean.xml
b/core/src/test/resources/META-INF/spring/cse.bean.xml
index 3040f28..a977418 100644
--- a/core/src/test/resources/META-INF/spring/cse.bean.xml
+++ b/core/src/test/resources/META-INF/spring/cse.bean.xml
@@ -20,9 +20,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean class="org.apache.servicecomb.core.CseApplicationListener">
- </bean>
-
<bean id="servicecomb.executor.default"
class="org.apache.servicecomb.core.executor.GroupExecutor"></bean>
<bean id="servicecomb.executor.reactive"
class="org.apache.servicecomb.core.executor.ReactiveExecutor"></bean>
</beans>
diff --git a/core/src/test/resources/microservice.yaml
b/core/src/test/resources/microservice.yaml
index 3a7fef8..0baa251 100644
--- a/core/src/test/resources/microservice.yaml
+++ b/core/src/test/resources/microservice.yaml
@@ -27,3 +27,7 @@ servicecomb:
chain:
Consumer:
default: simpleLB
+zq:
+ - tlist: a
+ - tlist: b
+ - tlist: 1
\ No newline at end of file
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
index 8ad91bd..397a3b1 100644
---
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
@@ -41,43 +41,25 @@ import org.apache.commons.lang.StringUtils;
import
org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl;
import org.apache.servicecomb.foundation.auth.AuthHeaderProvider;
import org.apache.servicecomb.foundation.auth.SignRequest;
-import org.apache.servicecomb.foundation.common.encrypt.Encryptions;
import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.common.net.IpPort;
import org.apache.servicecomb.foundation.common.net.NetUtils;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
-import org.apache.servicecomb.foundation.ssl.SSLCustom;
-import org.apache.servicecomb.foundation.ssl.SSLOption;
-import org.apache.servicecomb.foundation.ssl.SSLOptionFactory;
-import org.apache.servicecomb.foundation.vertx.AddressResolverConfig;
-import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder;
-import org.apache.servicecomb.foundation.vertx.VertxUtils;
-import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager;
-import org.apache.servicecomb.foundation.vertx.client.ClientVerticle;
-import
org.apache.servicecomb.foundation.vertx.client.http.HttpClientPoolFactory;
import
org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
+import org.apache.servicecomb.foundation.vertx.client.http.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import io.netty.handler.codec.http.HttpResponseStatus;
-import io.vertx.core.DeploymentOptions;
-import io.vertx.core.Vertx;
-import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.CaseInsensitiveHeaders;
-import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.WebSocket;
import io.vertx.core.http.WebSocketConnectOptions;
import io.vertx.core.http.impl.FrameType;
import io.vertx.core.http.impl.ws.WebSocketFrameImpl;
-import io.vertx.core.net.ProxyOptions;
-
-/**
- * Created by on 2016/5/17.
- */
public class ConfigCenterClient {
@@ -85,10 +67,6 @@ public class ConfigCenterClient {
private static final ConfigCenterConfig CONFIG_CENTER_CONFIG =
ConfigCenterConfig.INSTANCE;
- private static final String SSL_KEY = "cc.consumer";
-
- public static final String PROXY_KEY = "cc.consumer";
-
private static final long HEARTBEAT_INTERVAL = 30000;
private static final long BOOTUP_WAIT_TIME = 10;
@@ -115,8 +93,6 @@ public class ConfigCenterClient {
private ConfigCenterConfigurationSourceImpl.UpdateHandler updateHandler;
- private ClientPoolManager<HttpClientWithContext> clientMgr;
-
private boolean isWatching = false;
private final ServiceLoader<AuthHeaderProvider> authHeaderProviders =
@@ -125,6 +101,7 @@ public class ConfigCenterClient {
private URIConst uriConst = new URIConst();
public ConfigCenterClient(ConfigCenterConfigurationSourceImpl.UpdateHandler
updateHandler) {
+ HttpClients.addNewClientPoolManager(new
ConfigCenterHttpClientOptionsSPI());
this.updateHandler = updateHandler;
}
@@ -134,11 +111,6 @@ public class ConfigCenterClient {
return;
}
ParseConfigUtils.getInstance().initWithUpdateHandler(updateHandler);
- try {
- deployConfigClient();
- } catch (InterruptedException e) {
- throw new IllegalStateException(e);
- }
refreshMembers(memberDiscovery);
ConfigRefresh refreshTask = new
ConfigRefresh(ParseConfigUtils.getInstance(), memberDiscovery);
refreshTask.run(true);
@@ -164,7 +136,7 @@ public class ConfigCenterClient {
if (CONFIG_CENTER_CONFIG.getAutoDiscoveryEnabled()) {
String configCenter = memberDiscovery.getConfigServer();
IpPort ipPort = NetUtils.parseIpPortFromURI(configCenter);
- clientMgr.findThreadBindClientPool().runOnContext(client -> {
+
HttpClients.getClient(ConfigCenterHttpClientOptionsSPI.CLIENT_NAME).runOnContext(client
-> {
@SuppressWarnings("deprecation")
HttpClientRequest request =
client.get(ipPort.getPort(), ipPort.getHostOrIp(),
uriConst.MEMBERS, rsp -> {
@@ -192,47 +164,6 @@ public class ConfigCenterClient {
}
}
- private void deployConfigClient() throws InterruptedException {
- VertxOptions vertxOptions = new VertxOptions();
-
vertxOptions.setAddressResolverOptions(AddressResolverConfig.getAddressResover(SSL_KEY,
- ConfigCenterConfig.INSTANCE.getConcurrentCompositeConfiguration()));
- Vertx vertx = VertxUtils.getOrCreateVertxByName("config-center",
vertxOptions);
-
- HttpClientOptions httpClientOptions = createHttpClientOptions();
- clientMgr = new ClientPoolManager<>(vertx, new
HttpClientPoolFactory(httpClientOptions));
-
- DeploymentOptions deployOptions =
VertxUtils.createClientDeployOptions(clientMgr, 1);
- VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions);
- }
-
- private HttpClientOptions createHttpClientOptions() {
- HttpClientOptions httpClientOptions = new HttpClientOptions();
- if (ConfigCenterConfig.INSTANCE.isProxyEnable()) {
- ProxyOptions proxy = new ProxyOptions()
- .setHost(ConfigCenterConfig.INSTANCE.getProxyHost())
- .setPort(ConfigCenterConfig.INSTANCE.getProxyPort())
- .setUsername(ConfigCenterConfig.INSTANCE.getProxyUsername())
-
.setPassword(Encryptions.decode(ConfigCenterConfig.INSTANCE.getProxyPasswd(),
PROXY_KEY));
- httpClientOptions.setProxyOptions(proxy);
- }
-
httpClientOptions.setConnectTimeout(CONFIG_CENTER_CONFIG.getConnectionTimeout());
- if
(this.memberDiscovery.getConfigServer().toLowerCase().startsWith("https")) {
- LOGGER.debug("config center client performs requests over TLS");
- SSLOptionFactory factory =
SSLOptionFactory.createSSLOptionFactory(SSL_KEY,
- ConfigCenterConfig.INSTANCE.getConcurrentCompositeConfiguration());
- SSLOption sslOption;
- if (factory == null) {
- sslOption = SSLOption.buildFromYaml(SSL_KEY,
- ConfigCenterConfig.INSTANCE.getConcurrentCompositeConfiguration());
- } else {
- sslOption = factory.createSSLOption();
- }
- SSLCustom sslCustom =
SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
- VertxTLSBuilder.buildHttpClientOptions(sslOption, sslCustom,
httpClientOptions);
- }
- return httpClientOptions;
- }
-
class ConfigRefresh implements Runnable {
private ParseConfigUtils parseConfigUtils;
@@ -281,7 +212,8 @@ public class ConfigCenterClient {
}
headers.put("x-environment", environment);
- HttpClientWithContext vertxHttpClient =
clientMgr.findThreadBindClientPool();
+ HttpClientWithContext vertxHttpClient =
HttpClients.getClient(ConfigCenterHttpClientOptionsSPI.CLIENT_NAME);
+
vertxHttpClient.runOnContext(client -> {
Map<String, String> authHeaders = new HashMap<>();
authHeaderProviders.forEach(provider ->
authHeaders.putAll(provider.getSignAuthHeaders(
@@ -377,7 +309,7 @@ public class ConfigCenterClient {
}
String path = uriConst.ITEMS + "?dimensionsInfo=" + encodeServiceName +
"&revision="
+ ParseConfigUtils.getInstance().getCurrentVersionInfo();
- clientMgr.findThreadBindClientPool().runOnContext(client -> {
+
HttpClients.getClient(ConfigCenterHttpClientOptionsSPI.CLIENT_NAME).runOnContext(client
-> {
IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter);
@SuppressWarnings("deprecation")
HttpClientRequest request = client.get(ipPort.getPort(),
ipPort.getHostOrIp(), path, rsp -> {
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterConfig.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterConfig.java
index fa49000..e71e548 100644
---
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterConfig.java
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterConfig.java
@@ -71,6 +71,14 @@ public final class ConfigCenterConfig {
public static final String PROXY_PASSWD = PROXY_PRE_NAME + "passwd";
+ public static final String CONNECTION_TIME_OUT =
"servicecomb.config.client.timeout.connection";
+
+ public static final String EVENT_LOOP_SIZE =
"servicecomb.config.client.eventLoopSize";
+
+ public static final String VERTICAL_INSTANCE_COUNT =
"servicecomb.config.client.verticalInstanceCount";
+
+ public static final String IDLE_TIMEOUT_IN_SECONDES =
"servicecomb.config.client.idleTimeoutInSeconds";
+
private static final int DEFAULT_REFRESH_MODE = 0;
private static final int DEFAULT_REFRESH_PORT = 30104;
@@ -180,7 +188,19 @@ public final class ConfigCenterConfig {
}
public int getConnectionTimeout() {
- return finalConfig.getInt("servicecomb.config.client.timeout.connection",
DEFAULT_TIMEOUT_IN_MS);
+ return finalConfig.getInt(CONNECTION_TIME_OUT, 1000);
+ }
+
+ public int getEventLoopSize() {
+ return finalConfig.getInt(EVENT_LOOP_SIZE, 2);
+ }
+
+ public int getVerticalInstanceCount() {
+ return finalConfig.getInt(VERTICAL_INSTANCE_COUNT, 1);
+ }
+
+ public int getIdleTimeoutInSeconds() {
+ return finalConfig.getInt(IDLE_TIMEOUT_IN_SECONDES, 60);
}
public String getEnvironment() {
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterHttpClientOptionsSPI.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterHttpClientOptionsSPI.java
new file mode 100644
index 0000000..1c78058
--- /dev/null
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterHttpClientOptionsSPI.java
@@ -0,0 +1,119 @@
+/*
+ * 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.client;
+
+import org.apache.servicecomb.deployment.Deployment;
+import org.apache.servicecomb.deployment.DeploymentProvider;
+import
org.apache.servicecomb.foundation.vertx.client.http.HttpClientOptionsSPI;
+
+import io.vertx.core.VertxOptions;
+import io.vertx.core.http.HttpVersion;
+
+public class ConfigCenterHttpClientOptionsSPI implements HttpClientOptionsSPI {
+ public static final String CLIENT_NAME = "config-center";
+
+ @Override
+ public String clientName() {
+ return CLIENT_NAME;
+ }
+
+ @Override
+ public int getOrder() {
+ return -100;
+ }
+
+ @Override
+ public boolean enabled() {
+ return
Deployment.getSystemBootStrapInfo(DeploymentProvider.SYSTEM_KEY_CONFIG_CENTER)
!= null;
+ }
+
+ @Override
+ public String getConfigTag() {
+ return "cc.consumer";
+ }
+
+ @Override
+ public int getEventLoopPoolSize() {
+ return ConfigCenterConfig.INSTANCE.getEventLoopSize();
+ }
+
+ @Override
+ public int getInstanceCount() {
+ return ConfigCenterConfig.INSTANCE.getVerticalInstanceCount();
+ }
+
+ @Override
+ public boolean isWorker() {
+ return false;
+ }
+
+ @Override
+ public String getWorkerPoolName() {
+ return null;
+ }
+
+ @Override
+ public int getWorkerPoolSize() {
+ return VertxOptions.DEFAULT_WORKER_POOL_SIZE;
+ }
+
+ @Override
+ public HttpVersion getHttpVersion() {
+ return HttpVersion.HTTP_1_1;
+ }
+
+ @Override
+ public int getConnectTimeoutInMillis() {
+ return ConfigCenterConfig.INSTANCE.getConnectionTimeout();
+ }
+
+ @Override
+ public int getIdleTimeoutInSeconds() {
+ return ConfigCenterConfig.INSTANCE.getIdleTimeoutInSeconds();
+ }
+
+ @Override
+ public boolean isProxyEnable() {
+ return ConfigCenterConfig.INSTANCE.isProxyEnable();
+ }
+
+ @Override
+ public String getProxyHost() {
+ return ConfigCenterConfig.INSTANCE.getProxyHost();
+ }
+
+ @Override
+ public int getProxyPort() {
+ return ConfigCenterConfig.INSTANCE.getProxyPort();
+ }
+
+ @Override
+ public String getProxyUsername() {
+ return ConfigCenterConfig.INSTANCE.getProxyUsername();
+ }
+
+ @Override
+ public String getProxyPassword() {
+ return ConfigCenterConfig.INSTANCE.getProxyPasswd();
+ }
+
+ @Override
+ public boolean isSsl() {
+ return
ConfigCenterConfig.INSTANCE.getServerUri().get(0).startsWith("https");
+ }
+}
diff --git
a/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
index 46ec9b4..efbd2d3 100644
---
a/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
+++
b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/client/TestConfigCenterClient.java
@@ -31,6 +31,7 @@ import
org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager;
import
org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
import
org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler;
+import org.apache.servicecomb.foundation.vertx.client.http.HttpClients;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -62,7 +63,9 @@ public class TestConfigCenterClient {
@SuppressWarnings("unchecked")
@Test
- public void testConnectServer() {
+ public void testConnectServer(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr) {
+ HttpClients.mockClientPoolManager("config-center", clientMgr);
+
HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
Mockito.when(request.method()).thenReturn(HttpMethod.GET);
Mockito.when(request.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
@@ -114,23 +117,22 @@ public class TestConfigCenterClient {
Assert.assertFalse(status);
}
+ @SuppressWarnings({"unchecked", "rawtypes"})
@Test
- public void testConfigRefreshModeOne(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,
- @Mocked HttpClientWithContext httpClientWithContext) {
- String version1 = refreshAndGetCurrentRevision(clientMgr,
httpClientWithContext, 200, "huawei");
+ public void testConfigRefreshModeOne(@Mocked ClientPoolManager clientMgr) {
+ String version1 = refreshAndGetCurrentRevision(clientMgr, 200, "huawei");
//test the sdk get and change the latestRevision
Assert.assertEquals("huawei", version1);
- String version2 = refreshAndGetCurrentRevision(clientMgr,
httpClientWithContext, 304, "rkd");
+ String version2 = refreshAndGetCurrentRevision(clientMgr, 304, "rkd");
//test that when return code is 304, the sdk do not change the
latestRevision
Assert.assertNotEquals("rkd", version2);
- String version3 = refreshAndGetCurrentRevision(clientMgr,
httpClientWithContext, 200, "");
+ String version3 = refreshAndGetCurrentRevision(clientMgr, 200, "");
//make sure the current version is not ""
Assert.assertNotEquals("", version3);
}
- @SuppressWarnings("unchecked")
- private String
refreshAndGetCurrentRevision(ClientPoolManager<HttpClientWithContext> clientMgr,
- HttpClientWithContext httpClientWithContext, int statusCode, String
version) {
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private String refreshAndGetCurrentRevision(ClientPoolManager clientMgr, int
statusCode, String version) {
ConfigCenterConfigurationSourceImpl impl = new
ConfigCenterConfigurationSourceImpl();
UpdateHandler updateHandler = impl.new UpdateHandler();
@@ -159,12 +161,15 @@ public class TestConfigCenterClient {
return request;
});
- new MockUp<HttpClientWithContext>() {
+ HttpClientWithContext httpClientWithContext = new
MockUp<HttpClientWithContext>() {
@Mock
public void runOnContext(RunHandler handler) {
handler.run(httpClient);
}
- };
+ }.getMockInstance();
+
+ HttpClients.mockClientPoolManager("config-center", clientMgr);
+
new Expectations() {
{
clientMgr.findThreadBindClientPool();
@@ -173,7 +178,6 @@ public class TestConfigCenterClient {
};
ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
- Deencapsulation.setField(cc, "clientMgr", clientMgr);
ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
MemberDiscovery memberdis = new
MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
@@ -183,10 +187,9 @@ public class TestConfigCenterClient {
return currentVersionInfo;
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "rawtypes"})
@Test
- public void testConfigRefreshModeZero(@Mocked
ClientPoolManager<HttpClientWithContext> clientMgr,
- @Mocked HttpClientWithContext httpClientWithContext) {
+ public void testConfigRefreshModeZero(@Mocked ClientPoolManager clientMgr) {
ConfigCenterConfigurationSourceImpl impl = new
ConfigCenterConfigurationSourceImpl();
UpdateHandler updateHandler = impl.new UpdateHandler();
HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
@@ -231,12 +234,16 @@ public class TestConfigCenterClient {
handler.handle(websocket);
return null;
});
- new MockUp<HttpClientWithContext>() {
+
+ HttpClientWithContext httpClientWithContext = new
MockUp<HttpClientWithContext>() {
@Mock
public void runOnContext(RunHandler handler) {
handler.run(httpClient);
}
- };
+ }.getMockInstance();
+
+ HttpClients.mockClientPoolManager("config-center", clientMgr);
+
new Expectations() {
{
clientMgr.findThreadBindClientPool();
@@ -245,7 +252,6 @@ public class TestConfigCenterClient {
};
ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
- Deencapsulation.setField(cc, "clientMgr", clientMgr);
ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
MemberDiscovery memberdis = new
MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
@@ -319,7 +325,7 @@ public class TestConfigCenterClient {
}
};
ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
- Deencapsulation.setField(cc, "clientMgr", clientMgr);
+ HttpClients.mockClientPoolManager("config-center", clientMgr);
ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
MemberDiscovery memberdis = new
MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
diff --git
a/foundations/foundation-config/src/main/resources/META-INF/spring/cse.bean.xml
b/foundations/foundation-config/src/main/resources/META-INF/spring/cse.bean.xml
index 952cfc0..6a794b6 100644
---
a/foundations/foundation-config/src/main/resources/META-INF/spring/cse.bean.xml
+++
b/foundations/foundation-config/src/main/resources/META-INF/spring/cse.bean.xml
@@ -20,6 +20,5 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean class="org.apache.servicecomb.config.ConfigurationSpringInitializer"/>
<bean class="org.apache.servicecomb.config.DynamicPropertiesImpl"/>
</beans>
diff --git
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
index 2ddbd6b..79b18ff 100644
---
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
+++
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
@@ -26,6 +26,10 @@ import
org.apache.servicecomb.foundation.vertx.AddressResolverConfig;
import org.apache.servicecomb.foundation.vertx.VertxUtils;
import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager;
import org.apache.servicecomb.foundation.vertx.client.ClientVerticle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
@@ -35,27 +39,38 @@ import io.vertx.core.VertxOptions;
* load and manages a set of HttpClient at boot up.
*/
public class HttpClients {
- private static Map<String, ClientPoolManager<HttpClientWithContext>>
httpClients;
+ private static final Logger LOGGER =
LoggerFactory.getLogger(HttpClients.class);
+
+ private static final Map<String, ClientPoolManager<HttpClientWithContext>>
httpClients = new HashMap<>();
/* load at boot up, call this method once and only once. */
public static void load() {
- httpClients = new HashMap<>();
List<HttpClientOptionsSPI> clientOptionsList =
SPIServiceUtils.getOrLoadSortedService(HttpClientOptionsSPI.class);
clientOptionsList.forEach(option -> {
if (option.enabled()) {
ClientPoolManager<HttpClientWithContext> clientPoolManager =
httpClients.get(option.clientName());
if (clientPoolManager != null) {
- throw new IllegalStateException(
- "Can not configure two http client with the same name: " +
option.clientName());
+ LOGGER.warn("client pool {} initialized again.",
option.clientName());
}
httpClients.put(option.clientName(), createClientPoolManager(option));
}
});
}
+ @VisibleForTesting
+ public static void mockClientPoolManager(String name,
ClientPoolManager<HttpClientWithContext> clientPool) {
+ httpClients.put(name, clientPool);
+ }
+
+ /* used for configurations module: these module must be boot before other
HttpClients is initialized. so can
+ * not load by SPI, must add manually */
+ public static void addNewClientPoolManager(HttpClientOptionsSPI option) {
+ httpClients.put(option.clientName(), createClientPoolManager(option));
+ }
+
/* destroy at shutdown. */
public static void destroy() {
- httpClients = null;
+ httpClients.clear();
List<HttpClientOptionsSPI> clientOptionsList =
SPIServiceUtils.getOrLoadSortedService(HttpClientOptionsSPI.class);
clientOptionsList.forEach(option -> {
VertxUtils.blockCloseVertxByName(option.clientName());
diff --git
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
index 1651532..edb615f 100644
---
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
+++
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java
@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
-import org.apache.servicecomb.config.ConfigUtil;
import org.apache.servicecomb.deployment.Deployment;
import org.apache.servicecomb.deployment.DeploymentProvider;
import org.apache.servicecomb.foundation.auth.AuthHeaderProvider;
@@ -42,11 +41,6 @@ import io.vertx.core.http.HttpVersion;
class ServiceRegistryConfigBuilder {
private static final Logger LOGGER =
LoggerFactory.getLogger(ServiceRegistryConfigBuilder.class);
- static {
- // ensure configurations are loaded properly
- ConfigUtil.installDynamicConfig();
- }
-
private boolean ssl;
public ServiceRegistryConfig build() {