From b11bbd943780215458cb39a677be6e21efb36d10 Mon Sep 17 00:00:00 2001 From: Heng Du Date: Thu, 25 Apr 2019 10:52:03 +0800 Subject: [PATCH] [ISSUE #1147]Add name server domain host support (#1175) * Add nameserver host support * Polish namespace auto obtain feature --- .../apache/rocketmq/client/ClientConfig.java | 21 ++++++++-- .../common/utils/NameServerAddressUtils.java | 40 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java diff --git a/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java b/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java index 9a66744c..6493f2d6 100644 --- a/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java +++ b/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java @@ -18,12 +18,11 @@ package org.apache.rocketmq.client; import java.util.HashSet; import java.util.Set; - import org.apache.commons.lang3.StringUtils; -import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.message.MessageQueue; import org.apache.rocketmq.common.protocol.NamespaceUtil; +import org.apache.rocketmq.common.utils.NameServerAddressUtils; import org.apache.rocketmq.remoting.common.RemotingUtil; import org.apache.rocketmq.remoting.netty.TlsSystemConfig; import org.apache.rocketmq.remoting.protocol.LanguageCode; @@ -33,7 +32,7 @@ import org.apache.rocketmq.remoting.protocol.LanguageCode; */ public class ClientConfig { public static final String SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY = "com.rocketmq.sendMessageWithVIPChannel"; - private String namesrvAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY, System.getenv(MixAll.NAMESRV_ADDR_ENV)); + private String namesrvAddr = NameServerAddressUtils.getNameServerAddresses(); private String clientIP = RemotingUtil.getLocalAddress(); private String instanceName = System.getProperty("rocketmq.client.name", "DEFAULT"); private int clientCallbackExecutorThreads = Runtime.getRuntime().availableProcessors(); @@ -161,9 +160,16 @@ public class ClientConfig { } public String getNamesrvAddr() { + if (StringUtils.isNotEmpty(namesrvAddr) && NameServerAddressUtils.NAMESRV_ENDPOINT_PATTERN.matcher(namesrvAddr.trim()).matches()) { + return namesrvAddr.substring(NameServerAddressUtils.ENDPOINT_PREFIX.length()); + } return namesrvAddr; } + /** + * Domain name mode access way does not support the delimiter(;), and only one domain name can be set. + * @param namesrvAddr name server address + */ public void setNamesrvAddr(String namesrvAddr) { this.namesrvAddr = namesrvAddr; } @@ -241,6 +247,15 @@ public class ClientConfig { } public String getNamespace() { + if (StringUtils.isNotEmpty(namespace)) { + return namespace; + } + + if (StringUtils.isNotEmpty(this.namesrvAddr)) { + if (NameServerAddressUtils.validateInstanceEndpoint(namesrvAddr)) { + return NameServerAddressUtils.parseInstanceIdFromEndpoint(namesrvAddr); + } + } return namespace; } diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java b/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java new file mode 100644 index 00000000..6aaf3a28 --- /dev/null +++ b/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java @@ -0,0 +1,40 @@ +/** + * 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.rocketmq.common.utils; + +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.common.MixAll; + +public class NameServerAddressUtils { + public static final String INSTANCE_PREFIX = "MQ_INST_"; + public static final String INSTANCE_REGEX = INSTANCE_PREFIX + "\\w+_\\w+"; + public static final String ENDPOINT_PREFIX = "http://"; + public static final Pattern NAMESRV_ENDPOINT_PATTERN = Pattern.compile("^" + ENDPOINT_PREFIX + ".*"); + public static final Pattern INST_ENDPOINT_PATTERN = Pattern.compile("^" + ENDPOINT_PREFIX + INSTANCE_REGEX + "\\..*"); + + public static String getNameServerAddresses() { + return System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY, System.getenv(MixAll.NAMESRV_ADDR_ENV)); + } + + public static boolean validateInstanceEndpoint(String endpoint) { + return INST_ENDPOINT_PATTERN.matcher(endpoint).matches(); + } + + public static String parseInstanceIdFromEndpoint(String endpoint) { + if (StringUtils.isEmpty(endpoint)) { + return null; + } + return endpoint.substring(ENDPOINT_PREFIX.length(), endpoint.indexOf('.')); + } +} -- GitLab