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 8e1f6bf4a22377b19577b3c8295fc1006fcad987 Author: yhs0092 <[email protected]> AuthorDate: Sun Feb 9 23:48:00 2020 +0800 [SCB-1691] ServiceRegistryConfig just carries config value --- .../config/ServiceRegistryConfig.java | 419 ++++++++++++--------- ...nfig.java => ServiceRegistryConfigBuilder.java} | 198 ++++------ .../config/TestServiceRegistryConfig.java | 12 +- 3 files changed, 331 insertions(+), 298 deletions(-) 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 9d5e440..1596077 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 @@ -17,45 +17,27 @@ package org.apache.servicecomb.serviceregistry.config; -import java.net.URI; import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -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; - -import com.netflix.config.DynamicBooleanProperty; -import com.netflix.config.DynamicIntProperty; -import com.netflix.config.DynamicPropertyFactory; -import com.netflix.config.DynamicStringProperty; import io.vertx.core.http.HttpVersion; -/** - * Created by on 2016/12/23. - */ public final class ServiceRegistryConfig { - private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistryConfig.class); - - public static final ServiceRegistryConfig INSTANCE = new ServiceRegistryConfig(); + public static final ServiceRegistryConfig INSTANCE = buildFromConfiguration(); - private static final int DEFAULT_TIMEOUT_IN_MS = 30000; + public static final int DEFAULT_TIMEOUT_IN_MS = 30000; - private static final int DEFAULT_TIMEOUT_IN_SECONDS = 30; + public static final int DEFAULT_TIMEOUT_IN_SECONDS = 30; - private static final int DEFAULT_REQUEST_TIMEOUT_IN_MS = 30000; + public static final int DEFAULT_REQUEST_TIMEOUT_IN_MS = 30000; - private static final int DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS = 3000; + public static final int DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS = 3000; - private static final int DEFAULT_CHECK_INTERVAL_IN_S = 30; + public static final int DEFAULT_CHECK_INTERVAL_IN_S = 30; - private static final int DEFAULT_CHECK_TIMES = 3; + public static final int DEFAULT_CHECK_TIMES = 3; public static final String AUTH_ENABLED = "servicecomb.auth.enabled"; @@ -73,8 +55,6 @@ public final class ServiceRegistryConfig { public static final String NO_DOMAIN = "default"; - private boolean ssl = true; - public static final String PROXY_PRE_NAME = "servicecomb.proxy."; public static final String PROXY_ENABLE = PROXY_PRE_NAME + "enable"; @@ -101,246 +81,335 @@ public final class ServiceRegistryConfig { private String registryName = ServiceRegistry.DEFAULT_REGISTRY_NAME; - private ServiceRegistryConfig() { + private HttpVersion httpVersion; + + private int instances; + + // TODO SCB-1691 getter of this field's behavior changed, should check + private boolean ssl = true; + + private ArrayList<IpPort> ipPort; + + private int connectionTimeout; + + private int idleConnectionTimeout; + + private int idleWatchTimeout; + + private int requestTimeout; + + //Set the timeout of the heartbeat request + private int heartBeatRequestTimeout; + + private int heartbeatInterval; + + private int instancePullInterval; + + private boolean registryAutoDiscovery; + + private int resendHeartBeatTimes; + + private boolean emptyInstanceProtectionEnabled; + + private boolean alwaysOverrideSchema; + + private boolean preferIpAddress; + + private boolean watch; + + private boolean clientAuthEnabled; + + private String registryApiVersion; + + private String tenantName; + + private String domainName; + + private String accessKey; + + private String secretKey; + + private boolean proxyEnable; + + private String proxyHost; + + private int proxyPort; + + private String proxyUsername; + + private String proxyPasswd; + /** + * Read the service registry related configurations and build the {@link ServiceRegistryConfig} + * object. Since most of the service registry configurations are similar, this method may be + * convenient to construct multiple config objects. + */ + public static ServiceRegistryConfig buildFromConfiguration() { + return new ServiceRegistryConfigBuilder().build(); + } + + public String getTransport() { + return "rest"; + } + + public String getRegistryName() { + return registryName; + } + + public ServiceRegistryConfig setRegistryName(String registryName) { + this.registryName = registryName; + return this; } public HttpVersion getHttpVersion() { - DynamicStringProperty property = - DynamicPropertyFactory.getInstance() - .getStringProperty("servicecomb.service.registry.client.httpVersion", "HTTP_1_1"); - return HttpVersion.valueOf(property.get()); + return httpVersion; + } + + public ServiceRegistryConfig setHttpVersion(HttpVersion httpVersion) { + this.httpVersion = httpVersion; + return this; } public int getInstances() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty(VERTICLE_INSTANCES, 1); - int deployInstances = property.get(); - if (deployInstances <= 0) { - int nAvailableProcessors = Runtime.getRuntime().availableProcessors(); - LOGGER.warn("The property `{}` must be positive integer, fallback to use number of available processors: {}", - VERTICLE_INSTANCES, - nAvailableProcessors); - return nAvailableProcessors; - } - return deployInstances; + return instances; + } + + public ServiceRegistryConfig setInstances(int instances) { + this.instances = instances; + return this; } public boolean isSsl() { - getIpPort(); - return this.ssl; + return ssl; + } + + public ServiceRegistryConfig setSsl(boolean ssl) { + this.ssl = ssl; + return this; } public ArrayList<IpPort> getIpPort() { - List<String> uriList = Deployment.getSystemBootStrapInfo(DeploymentProvider.SYSTEM_KEY_SERVICE_CENTER) - .getAccessURL(); - ArrayList<IpPort> ipPortList = new ArrayList<>(); - uriList.forEach(anUriList -> { - try { - URI uri = new URI(anUriList.trim()); - this.ssl = "https".equals(uri.getScheme()); - ipPortList.add(NetUtils.parseIpPort(uri)); - } catch (Exception e) { - LOGGER.error("servicecomb.service.registry.address invalid : {}", anUriList, e); - } - }); - return ipPortList; + return ipPort; } - public String getTransport() { - return "rest"; + public ServiceRegistryConfig setIpPort(ArrayList<IpPort> ipPort) { + this.ipPort = ipPort; + return this; } public int getConnectionTimeout() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.connection", DEFAULT_TIMEOUT_IN_MS); - int timeout = property.get(); - return timeout < 0 ? DEFAULT_TIMEOUT_IN_MS : timeout; + return connectionTimeout; + } + + public ServiceRegistryConfig setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; } public int getIdleConnectionTimeout() { - // connection pool idle timeout based on client heart beat interval. Heart beat default value is 30. - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.idle", DEFAULT_TIMEOUT_IN_SECONDS * 2); - int timeout = property.get(); - return timeout < 1 ? DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; + return idleConnectionTimeout; + } + + public ServiceRegistryConfig setIdleConnectionTimeout(int idleConnectionTimeout) { + this.idleConnectionTimeout = idleConnectionTimeout; + return this; } public int getIdleWatchTimeout() { - // watch idle timeout based on SC PING/PONG interval. SC default value is 30. - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.watch", DEFAULT_TIMEOUT_IN_SECONDS * 2); - int timeout = property.get(); - return timeout < 1 ? DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; + return idleWatchTimeout; + } + + public ServiceRegistryConfig setIdleWatchTimeout(int idleWatchTimeout) { + this.idleWatchTimeout = idleWatchTimeout; + return this; } public int getRequestTimeout() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.request", DEFAULT_REQUEST_TIMEOUT_IN_MS); - int timeout = property.get(); - return timeout < 1 ? DEFAULT_REQUEST_TIMEOUT_IN_MS : timeout; + return requestTimeout; + } + + public ServiceRegistryConfig setRequestTimeout(int requestTimeout) { + this.requestTimeout = requestTimeout; + return this; } - //Set the timeout of the heartbeat request public int getHeartBeatRequestTimeout() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.heartbeat", - DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS); - int timeout = property.get(); - return timeout < 1 ? DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS : timeout; + return heartBeatRequestTimeout; + } + + public ServiceRegistryConfig setHeartBeatRequestTimeout(int heartBeatRequestTimeout) { + this.heartBeatRequestTimeout = heartBeatRequestTimeout; + return this; } public int getHeartbeatInterval() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.instance.healthCheck.interval", - DEFAULT_CHECK_INTERVAL_IN_S); - int interval = property.get(); - return interval < 0 ? DEFAULT_CHECK_INTERVAL_IN_S : interval; + return heartbeatInterval; + } + + public ServiceRegistryConfig setHeartbeatInterval(int heartbeatInterval) { + this.heartbeatInterval = heartbeatInterval; + return this; } public int getInstancePullInterval() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.instance.pull.interval", - DEFAULT_CHECK_INTERVAL_IN_S); - int interval = property.get(); - return interval < 0 ? DEFAULT_CHECK_INTERVAL_IN_S : interval; + return instancePullInterval; } - public long getMsInstancePullInterval() { - return TimeUnit.SECONDS.toMillis(getInstancePullInterval()); + public ServiceRegistryConfig setInstancePullInterval(int instancePullInterval) { + this.instancePullInterval = instancePullInterval; + return this; } public boolean isRegistryAutoDiscovery() { - DynamicBooleanProperty property = - DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.autodiscovery", - false); - return property.get(); + return registryAutoDiscovery; + } + + public ServiceRegistryConfig setRegistryAutoDiscovery(boolean registryAutoDiscovery) { + this.registryAutoDiscovery = registryAutoDiscovery; + return this; } public int getResendHeartBeatTimes() { - DynamicIntProperty property = - DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.instance.healthCheck.times", - DEFAULT_CHECK_TIMES); - int times = property.get(); - return times < 0 ? DEFAULT_CHECK_TIMES : times; + return resendHeartBeatTimes; + } + + public ServiceRegistryConfig setResendHeartBeatTimes(int resendHeartBeatTimes) { + this.resendHeartBeatTimes = resendHeartBeatTimes; + return this; } public boolean isEmptyInstanceProtectionEnabled() { - DynamicBooleanProperty property = - DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.instance.empty.protection", - true); - return property.get(); + return emptyInstanceProtectionEnabled; + } + + public ServiceRegistryConfig setEmptyInstanceProtectionEnabled(boolean emptyInstanceProtectionEnabled) { + this.emptyInstanceProtectionEnabled = emptyInstanceProtectionEnabled; + return this; } public boolean isAlwaysOverrideSchema() { - DynamicBooleanProperty property = - DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.instance.alwaysOverrideSchema", - false); - return property.get(); + return alwaysOverrideSchema; + } + + public ServiceRegistryConfig setAlwaysOverrideSchema(boolean alwaysOverrideSchema) { + this.alwaysOverrideSchema = alwaysOverrideSchema; + return this; } public boolean isPreferIpAddress() { - DynamicBooleanProperty property = - DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.instance.preferIpAddress", - false); - return property.get(); + return preferIpAddress; + } + + public ServiceRegistryConfig setPreferIpAddress(boolean preferIpAddress) { + this.preferIpAddress = preferIpAddress; + return this; } public boolean isWatch() { - DynamicBooleanProperty property = - DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.instance.watch", - true); - return property.get(); + return watch; + } + + public ServiceRegistryConfig setWatch(boolean watch) { + this.watch = watch; + return this; } public boolean isClientAuthEnabled() { - String isAuthEnabled = getProperty("false", AUTH_ENABLED); - return Boolean.parseBoolean(isAuthEnabled); + return clientAuthEnabled; + } + + public ServiceRegistryConfig setClientAuthEnabled(boolean clientAuthEnabled) { + this.clientAuthEnabled = clientAuthEnabled; + return this; } public String getRegistryApiVersion() { - return getProperty("v4", REGISTRY_API_VERSION); + return registryApiVersion; + } + + public ServiceRegistryConfig setRegistryApiVersion(String registryApiVersion) { + this.registryApiVersion = registryApiVersion; + return this; } public String getTenantName() { - return getProperty(NO_TENANT, TENANT_NAME); + return tenantName; + } + + public ServiceRegistryConfig setTenantName(String tenantName) { + this.tenantName = tenantName; + return this; } public String getDomainName() { - return getProperty(NO_DOMAIN, DOMAIN_NAME); + return domainName; + } + + public ServiceRegistryConfig setDomainName(String domainName) { + this.domainName = domainName; + return this; } public String getAccessKey() { - String tenantName = getProperty(null, TENANT_ACCESS_KEY); - return tenantName; + return accessKey; + } + + public ServiceRegistryConfig setAccessKey(String accessKey) { + this.accessKey = accessKey; + return this; } public String getSecretKey() { - String tenantName = getProperty(null, TENANT_SECRET_KEY); - return tenantName; + return secretKey; + } + + public ServiceRegistryConfig setSecretKey(String secretKey) { + this.secretKey = secretKey; + return this; } public Boolean isProxyEnable() { - String enable = getProperty("false", PROXY_ENABLE); - return Boolean.parseBoolean(enable); + return proxyEnable; + } + + public ServiceRegistryConfig setProxyEnable(Boolean proxyEnable) { + this.proxyEnable = proxyEnable; + return this; } public String getProxyHost() { - String host = getProperty("127.0.0.1", PROXY_HOST); - return host; + return proxyHost; } - public int getProxyPort() { - String port = getProperty("8080", PROXY_PORT); - return Integer.parseInt(port); + public ServiceRegistryConfig setProxyHost(String proxyHost) { + this.proxyHost = proxyHost; + return this; } - public String getProxyUsername() { - String username = getProperty(null, PROXY_USERNAME); - return username; + public int getProxyPort() { + return proxyPort; } - public String getProxyPasswd() { - String passwd = getProperty(null, PROXY_PASSWD); - return passwd; + public ServiceRegistryConfig setProxyPort(int proxyPort) { + this.proxyPort = proxyPort; + return this; } - public String getRegistryName() { - return registryName; + public String getProxyUsername() { + return proxyUsername; } - public ServiceRegistryConfig setRegistryName(String registryName) { - this.registryName = registryName; + public ServiceRegistryConfig setProxyUsername(String proxyUsername) { + this.proxyUsername = proxyUsername; return this; } - private String getProperty(String defaultValue, String... keys) { - String property = null; - for (String key : keys) { - property = DynamicPropertyFactory.getInstance().getStringProperty(key, null).get(); - if (property != null) { - break; - } - } - - if (property != null) { - return property; - } else { - return defaultValue; - } + public String getProxyPasswd() { + return proxyPasswd; + } + + public ServiceRegistryConfig setProxyPasswd(String proxyPasswd) { + this.proxyPasswd = proxyPasswd; + return this; } } 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/ServiceRegistryConfigBuilder.java similarity index 62% copy from service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java copy to service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfigBuilder.java index 9d5e440..95f2b6c 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/ServiceRegistryConfigBuilder.java @@ -20,13 +20,13 @@ package org.apache.servicecomb.serviceregistry.config; import java.net.URI; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; +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.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; @@ -37,72 +37,46 @@ import com.netflix.config.DynamicStringProperty; import io.vertx.core.http.HttpVersion; -/** - * Created by on 2016/12/23. - */ -public final class ServiceRegistryConfig { - private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistryConfig.class); - - public static final ServiceRegistryConfig INSTANCE = new ServiceRegistryConfig(); - - private static final int DEFAULT_TIMEOUT_IN_MS = 30000; - - private static final int DEFAULT_TIMEOUT_IN_SECONDS = 30; - - private static final int DEFAULT_REQUEST_TIMEOUT_IN_MS = 30000; - - private static final int DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS = 3000; - - private static final int DEFAULT_CHECK_INTERVAL_IN_S = 30; - - private static final int DEFAULT_CHECK_TIMES = 3; - - public static final String AUTH_ENABLED = "servicecomb.auth.enabled"; - - public static final String TENANT_ACCESS_KEY = "servicecomb.auth.accessKey"; - - public static final String TENANT_SECRET_KEY = "servicecomb.auth.secretKey"; - - public static final String REGISTRY_API_VERSION = "servicecomb.service.registry.api.version"; - - public static final String TENANT_NAME = "servicecomb.config.client.tenantName"; - - public static final String DOMAIN_NAME = "servicecomb.config.client.domainName"; - - public static final String NO_TENANT = "default"; - - public static final String NO_DOMAIN = "default"; - - private boolean ssl = true; - - public static final String PROXY_PRE_NAME = "servicecomb.proxy."; - - public static final String PROXY_ENABLE = PROXY_PRE_NAME + "enable"; - - public static final String PROXY_HOST = PROXY_PRE_NAME + "host"; - - public static final String PROXY_PORT = PROXY_PRE_NAME + "port"; - - public static final String PROXY_USERNAME = PROXY_PRE_NAME + "username"; - - public static final String PROXY_PASSWD = PROXY_PRE_NAME + "passwd"; - - public static final String SSL_KEY = "sc.consumer"; - - public static final String PROXY_KEY = "sc.consumer"; - - public static final String VERTICLE_INSTANCES = "servicecomb.service.registry.client.instances"; - - public static final String EVENT_LOOP_POOL_SIZE = "servicecomb.service.registry.client.eventLoopPoolSize"; - - public static final String WORKER_POOL_SIZE = "servicecomb.service.registry.client.workerPoolSize"; - - public static final String WORKER_POOL_NAME = "registry-vert.x-worker-thread"; - - private String registryName = ServiceRegistry.DEFAULT_REGISTRY_NAME; - - private ServiceRegistryConfig() { - +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() { + return new ServiceRegistryConfig() + .setHttpVersion(getHttpVersion()) + .setInstances(getInstances()) + .setIpPort(getIpPort()) + .setSsl(isSsl()) + .setConnectionTimeout(getConnectionTimeout()) + .setIdleConnectionTimeout(getIdleConnectionTimeout()) + .setIdleWatchTimeout(getIdleWatchTimeout()) + .setRequestTimeout(getRequestTimeout()) + .setHeartBeatRequestTimeout(getHeartBeatRequestTimeout()) + .setHeartbeatInterval(getHeartbeatInterval()) + .setInstancePullInterval(getInstancePullInterval()) + .setRegistryAutoDiscovery(isRegistryAutoDiscovery()) + .setResendHeartBeatTimes(getResendHeartBeatTimes()) + .setEmptyInstanceProtectionEnabled(isEmptyInstanceProtectionEnabled()) + .setAlwaysOverrideSchema(isAlwaysOverrideSchema()) + .setPreferIpAddress(isPreferIpAddress()) + .setWatch(isWatch()) + .setClientAuthEnabled(isClientAuthEnabled()) + .setRegistryApiVersion(getRegistryApiVersion()) + .setTenantName(getTenantName()) + .setDomainName(getDomainName()) + .setAccessKey(getAccessKey()) + .setSecretKey(getSecretKey()) + .setProxyEnable(isProxyEnable()) + .setProxyHost(getProxyHost()) + .setProxyPort(getProxyPort()) + .setProxyUsername(getProxyUsername()) + .setProxyPasswd(getProxyPasswd()); } public HttpVersion getHttpVersion() { @@ -115,25 +89,29 @@ public final class ServiceRegistryConfig { public int getInstances() { DynamicIntProperty property = DynamicPropertyFactory.getInstance() - .getIntProperty(VERTICLE_INSTANCES, 1); + .getIntProperty(ServiceRegistryConfig.VERTICLE_INSTANCES, 1); int deployInstances = property.get(); if (deployInstances <= 0) { int nAvailableProcessors = Runtime.getRuntime().availableProcessors(); LOGGER.warn("The property `{}` must be positive integer, fallback to use number of available processors: {}", - VERTICLE_INSTANCES, + ServiceRegistryConfig.VERTICLE_INSTANCES, nAvailableProcessors); return nAvailableProcessors; } return deployInstances; } + /** + * must be invoked after {@link #getIpPort()} + */ public boolean isSsl() { - getIpPort(); return this.ssl; } public ArrayList<IpPort> getIpPort() { - List<String> uriList = Deployment.getSystemBootStrapInfo(DeploymentProvider.SYSTEM_KEY_SERVICE_CENTER) + List<String> uriList = Objects + .requireNonNull(Deployment.getSystemBootStrapInfo(DeploymentProvider.SYSTEM_KEY_SERVICE_CENTER), + "no sc address found!") .getAccessURL(); ArrayList<IpPort> ipPortList = new ArrayList<>(); uriList.forEach(anUriList -> { @@ -155,35 +133,39 @@ public final class ServiceRegistryConfig { public int getConnectionTimeout() { DynamicIntProperty property = DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.connection", DEFAULT_TIMEOUT_IN_MS); + .getIntProperty("servicecomb.service.registry.client.timeout.connection", + ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_MS); int timeout = property.get(); - return timeout < 0 ? DEFAULT_TIMEOUT_IN_MS : timeout; + return timeout < 0 ? ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_MS : timeout; } public int getIdleConnectionTimeout() { // connection pool idle timeout based on client heart beat interval. Heart beat default value is 30. DynamicIntProperty property = DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.idle", DEFAULT_TIMEOUT_IN_SECONDS * 2); + .getIntProperty("servicecomb.service.registry.client.timeout.idle", + ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_SECONDS * 2); int timeout = property.get(); - return timeout < 1 ? DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; + return timeout < 1 ? ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; } public int getIdleWatchTimeout() { // watch idle timeout based on SC PING/PONG interval. SC default value is 30. DynamicIntProperty property = DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.watch", DEFAULT_TIMEOUT_IN_SECONDS * 2); + .getIntProperty("servicecomb.service.registry.client.timeout.watch", + ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_SECONDS * 2); int timeout = property.get(); - return timeout < 1 ? DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; + return timeout < 1 ? ServiceRegistryConfig.DEFAULT_TIMEOUT_IN_SECONDS * 2 : timeout; } public int getRequestTimeout() { DynamicIntProperty property = DynamicPropertyFactory.getInstance() - .getIntProperty("servicecomb.service.registry.client.timeout.request", DEFAULT_REQUEST_TIMEOUT_IN_MS); + .getIntProperty("servicecomb.service.registry.client.timeout.request", + ServiceRegistryConfig.DEFAULT_REQUEST_TIMEOUT_IN_MS); int timeout = property.get(); - return timeout < 1 ? DEFAULT_REQUEST_TIMEOUT_IN_MS : timeout; + return timeout < 1 ? ServiceRegistryConfig.DEFAULT_REQUEST_TIMEOUT_IN_MS : timeout; } //Set the timeout of the heartbeat request @@ -191,31 +173,27 @@ public final class ServiceRegistryConfig { DynamicIntProperty property = DynamicPropertyFactory.getInstance() .getIntProperty("servicecomb.service.registry.client.timeout.heartbeat", - DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS); + ServiceRegistryConfig.DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS); int timeout = property.get(); - return timeout < 1 ? DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS : timeout; + return timeout < 1 ? ServiceRegistryConfig.DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS : timeout; } public int getHeartbeatInterval() { DynamicIntProperty property = DynamicPropertyFactory.getInstance() .getIntProperty("servicecomb.service.registry.instance.healthCheck.interval", - DEFAULT_CHECK_INTERVAL_IN_S); + ServiceRegistryConfig.DEFAULT_CHECK_INTERVAL_IN_S); int interval = property.get(); - return interval < 0 ? DEFAULT_CHECK_INTERVAL_IN_S : interval; + return interval < 0 ? ServiceRegistryConfig.DEFAULT_CHECK_INTERVAL_IN_S : interval; } public int getInstancePullInterval() { DynamicIntProperty property = DynamicPropertyFactory.getInstance() .getIntProperty("servicecomb.service.registry.instance.pull.interval", - DEFAULT_CHECK_INTERVAL_IN_S); + ServiceRegistryConfig.DEFAULT_CHECK_INTERVAL_IN_S); int interval = property.get(); - return interval < 0 ? DEFAULT_CHECK_INTERVAL_IN_S : interval; - } - - public long getMsInstancePullInterval() { - return TimeUnit.SECONDS.toMillis(getInstancePullInterval()); + return interval < 0 ? ServiceRegistryConfig.DEFAULT_CHECK_INTERVAL_IN_S : interval; } public boolean isRegistryAutoDiscovery() { @@ -230,9 +208,9 @@ public final class ServiceRegistryConfig { DynamicIntProperty property = DynamicPropertyFactory.getInstance() .getIntProperty("servicecomb.service.registry.instance.healthCheck.times", - DEFAULT_CHECK_TIMES); + ServiceRegistryConfig.DEFAULT_CHECK_TIMES); int times = property.get(); - return times < 0 ? DEFAULT_CHECK_TIMES : times; + return times < 0 ? ServiceRegistryConfig.DEFAULT_CHECK_TIMES : times; } public boolean isEmptyInstanceProtectionEnabled() { @@ -268,64 +246,50 @@ public final class ServiceRegistryConfig { } public boolean isClientAuthEnabled() { - String isAuthEnabled = getProperty("false", AUTH_ENABLED); + String isAuthEnabled = getProperty("false", ServiceRegistryConfig.AUTH_ENABLED); return Boolean.parseBoolean(isAuthEnabled); } public String getRegistryApiVersion() { - return getProperty("v4", REGISTRY_API_VERSION); + return getProperty("v4", ServiceRegistryConfig.REGISTRY_API_VERSION); } public String getTenantName() { - return getProperty(NO_TENANT, TENANT_NAME); + return getProperty(ServiceRegistryConfig.NO_TENANT, ServiceRegistryConfig.TENANT_NAME); } public String getDomainName() { - return getProperty(NO_DOMAIN, DOMAIN_NAME); + return getProperty(ServiceRegistryConfig.NO_DOMAIN, ServiceRegistryConfig.DOMAIN_NAME); } public String getAccessKey() { - String tenantName = getProperty(null, TENANT_ACCESS_KEY); - return tenantName; + return getProperty(null, ServiceRegistryConfig.TENANT_ACCESS_KEY); } public String getSecretKey() { - String tenantName = getProperty(null, TENANT_SECRET_KEY); - return tenantName; + return getProperty(null, ServiceRegistryConfig.TENANT_SECRET_KEY); } public Boolean isProxyEnable() { - String enable = getProperty("false", PROXY_ENABLE); + String enable = getProperty("false", ServiceRegistryConfig.PROXY_ENABLE); return Boolean.parseBoolean(enable); } public String getProxyHost() { - String host = getProperty("127.0.0.1", PROXY_HOST); - return host; + return getProperty("127.0.0.1", ServiceRegistryConfig.PROXY_HOST); } public int getProxyPort() { - String port = getProperty("8080", PROXY_PORT); + String port = getProperty("8080", ServiceRegistryConfig.PROXY_PORT); return Integer.parseInt(port); } public String getProxyUsername() { - String username = getProperty(null, PROXY_USERNAME); - return username; + return getProperty(null, ServiceRegistryConfig.PROXY_USERNAME); } public String getProxyPasswd() { - String passwd = getProperty(null, PROXY_PASSWD); - return passwd; - } - - public String getRegistryName() { - return registryName; - } - - public ServiceRegistryConfig setRegistryName(String registryName) { - this.registryName = registryName; - return this; + return getProperty(null, ServiceRegistryConfig.PROXY_PASSWD); } private String getProperty(String defaultValue, String... keys) { diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/config/TestServiceRegistryConfig.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/config/TestServiceRegistryConfig.java index fc3be5a..f26d337 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/config/TestServiceRegistryConfig.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/config/TestServiceRegistryConfig.java @@ -43,7 +43,7 @@ public class TestServiceRegistryConfig { @Test public void testServiceRegistryConfig() { - ServiceRegistryConfig oConfig = ServiceRegistryConfig.INSTANCE; + ServiceRegistryConfig oConfig = new ServiceRegistryConfigBuilder().build(); Assert.assertNull(oConfig.getAccessKey()); Assert.assertEquals(30000, oConfig.getConnectionTimeout()); Assert.assertNotEquals(null, oConfig.getHeartbeatInterval()); @@ -62,10 +62,10 @@ public class TestServiceRegistryConfig { List<IpPort> ipPorts = oConfig.getIpPort(); Assert.assertEquals("127.0.0.1:80", ipPorts.get(0).toString()); Assert.assertEquals("127.0.0.1:443", ipPorts.get(1).toString()); - Assert.assertFalse(ServiceRegistryConfig.INSTANCE.isProxyEnable()); - Assert.assertEquals("127.0.0.1", ServiceRegistryConfig.INSTANCE.getProxyHost()); - Assert.assertEquals(8080, ServiceRegistryConfig.INSTANCE.getProxyPort()); - Assert.assertNull(ServiceRegistryConfig.INSTANCE.getProxyUsername()); - Assert.assertNull(ServiceRegistryConfig.INSTANCE.getProxyPasswd()); + Assert.assertFalse(oConfig.isProxyEnable()); + Assert.assertEquals("127.0.0.1", oConfig.getProxyHost()); + Assert.assertEquals(8080, oConfig.getProxyPort()); + Assert.assertNull(oConfig.getProxyUsername()); + Assert.assertNull(oConfig.getProxyPasswd()); } }
