This is an automated email from the ASF dual-hosted git repository. duhengforever pushed a commit to branch OpenMessaging in repository https://gitbox.apache.org/repos/asf/rocketmq-ons.git
commit 422eb18af4f8d735a0cb663ee004abc229443cfa Author: duhenglucky <[email protected]> AuthorDate: Wed Jul 17 10:31:29 2019 +0800 Reduce too strict check in ONS Client create process --- .../ons/api/impl/authority/SessionCredentials.java | 2 +- .../ons/api/impl/rocketmq/ONSClientAbstract.java | 49 ++++++++++++---------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/ons-core/ons-auth4client/src/main/java/org/apache/rocketmq/ons/api/impl/authority/SessionCredentials.java b/ons-core/ons-auth4client/src/main/java/org/apache/rocketmq/ons/api/impl/authority/SessionCredentials.java index fdbac11..854c093 100644 --- a/ons-core/ons-auth4client/src/main/java/org/apache/rocketmq/ons/api/impl/authority/SessionCredentials.java +++ b/ons-core/ons-auth4client/src/main/java/org/apache/rocketmq/ons/api/impl/authority/SessionCredentials.java @@ -46,7 +46,7 @@ public class SessionCredentials { private String securityToken; private String signature; private String signatureMethod; - private ONSChannel onsChannel = ONSChannel.ALIYUN; + private ONSChannel onsChannel = ONSChannel.CLOUD; public SessionCredentials() { String keyContent = null; diff --git a/ons-core/ons-client/src/main/java/org/apache/rocketmq/ons/api/impl/rocketmq/ONSClientAbstract.java b/ons-core/ons-client/src/main/java/org/apache/rocketmq/ons/api/impl/rocketmq/ONSClientAbstract.java index 2cc4d9c..74735e6 100644 --- a/ons-core/ons-client/src/main/java/org/apache/rocketmq/ons/api/impl/rocketmq/ONSClientAbstract.java +++ b/ons-core/ons-client/src/main/java/org/apache/rocketmq/ons/api/impl/rocketmq/ONSClientAbstract.java @@ -23,23 +23,20 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; - import javax.annotation.Generated; - -import org.apache.rocketmq.ons.open.trace.core.dispatch.AsyncDispatcher; +import org.apache.commons.lang3.StringUtils; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl; import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.namesrv.TopAddressing; import org.apache.rocketmq.logging.InternalLogger; - import org.apache.rocketmq.ons.api.Admin; import org.apache.rocketmq.ons.api.PropertyKeyConst; import org.apache.rocketmq.ons.api.exception.ONSClientException; import org.apache.rocketmq.ons.api.impl.authority.SessionCredentials; import org.apache.rocketmq.ons.api.impl.util.ClientLoggerUtil; import org.apache.rocketmq.ons.api.impl.util.NameAddrUtils; -import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.ons.open.trace.core.dispatch.AsyncDispatcher; import static org.apache.rocketmq.common.UtilAll.getPid; @@ -54,7 +51,7 @@ public abstract class ONSClientAbstract implements Admin { protected static final long WSADDR_INTERNAL_TIMEOUTMILLS = Long.parseLong(System.getProperty("com.aliyun.openservices.ons.addr.internal.timeoutmills", "3000")); protected static final long WSADDR_INTERNET_TIMEOUTMILLS = - Long.parseLong(System.getProperty("com.aliyun.openservices.ons.addr.internet.timeoutmills", "5000")); + Long.parseLong(System.getProperty("com.aliyun.openservices.ons.addr.internet.timeoutmills", "5000")); private final static InternalLogger LOGGER = ClientLoggerUtil.getClientLogger(); protected final Properties properties; protected final SessionCredentials sessionCredentials = new SessionCredentials(); @@ -75,11 +72,13 @@ public abstract class ONSClientAbstract implements Admin { public ONSClientAbstract(Properties properties) { this.properties = properties; this.sessionCredentials.updateContent(properties); - if (null == this.sessionCredentials.getAccessKey() || "".equals(this.sessionCredentials.getAccessKey())) { + if (this.sessionCredentials.getOnsChannel().equals(ONSChannel.ALIYUN) && + (null == this.sessionCredentials.getAccessKey() || "".equals(this.sessionCredentials.getAccessKey()))) { throw new ONSClientException("please set access key"); } - if (null == this.sessionCredentials.getSecretKey() || "".equals(this.sessionCredentials.getSecretKey())) { + if (this.sessionCredentials.getOnsChannel().equals(ONSChannel.ALIYUN) && + (null == this.sessionCredentials.getSecretKey() || "".equals(this.sessionCredentials.getSecretKey()))) { throw new ONSClientException("please set secret key"); } @@ -87,12 +86,13 @@ public abstract class ONSClientAbstract implements Admin { throw new ONSClientException("please set ons channel"); } - - this.nameServerAddr = getNameSrvAddrFromProperties(); if (nameServerAddr != null) { return; } + if (nameServerAddr == null && !this.sessionCredentials.getOnsChannel().equals(ONSChannel.ALIYUN)) { + return; + } this.nameServerAddr = fetchNameServerAddr(); if (null == nameServerAddr) { throw new ONSClientException(FAQ.errorMessage("Can not find name server, May be your network problem.", FAQ.FIND_NS_FAILED)); @@ -131,7 +131,6 @@ public abstract class ONSClientAbstract implements Admin { private String fetchNameServerAddr() { String nsAddrs; - { String property = this.properties.getProperty(PropertyKeyConst.ONSAddr); if (property != null) { @@ -145,7 +144,6 @@ public abstract class ONSClientAbstract implements Admin { } } - { TopAddressing top = new TopAddressing(WSADDR_INTERNAL); nsAddrs = top.fetchNSAddr(false, WSADDR_INTERNAL_TIMEOUTMILLS); @@ -155,7 +153,6 @@ public abstract class ONSClientAbstract implements Admin { } } - { TopAddressing top = new TopAddressing(WSADDR_INTERNET); nsAddrs = top.fetchNSAddr(false, WSADDR_INTERNET_TIMEOUTMILLS); @@ -172,16 +169,21 @@ public abstract class ONSClientAbstract implements Admin { } protected String buildIntanceName() { - return Integer.toString(UtilAll.getPid()) - + "#" + this.nameServerAddr.hashCode() - + "#" + this.sessionCredentials.getAccessKey().hashCode() - + "#" + System.nanoTime(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(Integer.toString(UtilAll.getPid())).append("#"); + if (this.nameServerAddr != null) { + stringBuilder.append(this.nameServerAddr.hashCode()).append("#"); + } + if (this.sessionCredentials.getAccessKey() != null) { + stringBuilder.append(this.sessionCredentials.getAccessKey().hashCode()).append("#"); + } + stringBuilder.append(System.nanoTime()); + return stringBuilder.toString(); } protected String getNamespace() { String namespace = null; - { String nameserverAddr = this.properties.getProperty(PropertyKeyConst.NAMESRV_ADDR); if (StringUtils.isNotEmpty(nameserverAddr)) { @@ -192,7 +194,6 @@ public abstract class ONSClientAbstract implements Admin { } } - { String namespaceFromProperty = this.properties.getProperty(PropertyKeyConst.INSTANCE_ID, null); if (StringUtils.isNotEmpty(namespaceFromProperty)) { @@ -236,13 +237,15 @@ public abstract class ONSClientAbstract implements Admin { @Override public void updateCredential(Properties credentialProperties) { - if (null == credentialProperties.getProperty(SessionCredentials.AccessKey) - || "".equals(credentialProperties.getProperty(SessionCredentials.AccessKey))) { + if (this.sessionCredentials.getOnsChannel().equals(ONSChannel.ALIYUN) && + (null == credentialProperties.getProperty(SessionCredentials.AccessKey) + || "".equals(credentialProperties.getProperty(SessionCredentials.AccessKey)))) { throw new ONSClientException("update credential failed. please set access key."); } - if (null == credentialProperties.getProperty(SessionCredentials.SecretKey) - || "".equals(credentialProperties.getProperty(SessionCredentials.SecretKey))) { + if (this.sessionCredentials.getOnsChannel().equals(ONSChannel.ALIYUN) && + (null == credentialProperties.getProperty(SessionCredentials.SecretKey) + || "".equals(credentialProperties.getProperty(SessionCredentials.SecretKey)))) { throw new ONSClientException("update credential failed. please set secret key"); } this.sessionCredentials.updateContent(credentialProperties);
