Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #12189] Unified Nacos Client address module code #12274

Merged
merged 20 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public class PropertyKeyConst {

public static final String LOG_ALL_PROPERTIES = "logAllProperties";

public static final String CLIENT_MODULE_TYPE = "clientModuleType";

/**
* Get the key value of some variable value from the system property.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.address;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.Logger;

import java.util.Collection;
import java.util.List;

/**
* Server list Manager.
*
* @author totalo
*/
public abstract class AbstractServerListManager implements ServerListFactory, Closeable {

private static final Logger LOGGER = LogUtils.logger(AbstractServerListManager.class);

protected ServerListProvider serverListProvider;

private final NacosClientProperties properties;

public AbstractServerListManager(NacosClientProperties properties) throws NacosException {
totalo marked this conversation as resolved.
Show resolved Hide resolved
this(properties, null);
}

public AbstractServerListManager(NacosClientProperties properties, String namespace) throws NacosException {
if (null == properties) {
throw new NacosException(NacosException.INVALID_PARAM, "properties is null");
}
if (StringUtils.isNotBlank(namespace)) {
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
}
properties.setProperty(PropertyKeyConst.CLIENT_MODULE_TYPE, getModuleName());
this.properties = properties;
Collection<ServerListProvider> serverListProviders = NacosServiceLoader.load(ServerListProvider.class);
totalo marked this conversation as resolved.
Show resolved Hide resolved
for (ServerListProvider each : serverListProviders) {
if (each.match(properties)) {
this.serverListProvider = each;
break;
}
}
if (null == serverListProvider) {
throw new NacosException(NacosException.SERVER_ERROR, "no server list provider found");
}
this.serverListProvider.init(properties, getNacosRestTemplate());
}

@Override
public List<String> getServerList() {
return serverListProvider.getServerList();
}

@Override
public void shutdown() throws NacosException {
String className = this.getClass().getName();
LOGGER.info("{} do shutdown begin", className);
if (null != serverListProvider) {
serverListProvider.shutdown();
}
serverListProvider = null;
LOGGER.info("{} do shutdown stop", className);
}

public ServerListProvider getServerListProvider() {
return serverListProvider;
}

public NacosClientProperties getProperties() {
return properties;
}

public String getServerName() {
return getModuleName() + "-" + serverListProvider.getServerName();
}

/**
* get module name.
* @return module name
*/
public abstract String getModuleName();

/**
* get nacos rest template.
* @return nacos rest template
*/
public abstract NacosRestTemplate getNacosRestTemplate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.address;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.List;

/**
* Address server list provider.
*
* @author totalo
*/
public abstract class AbstractServerListProvider implements ServerListProvider {

protected String contextPath = ParamUtil.getDefaultContextPath();

protected String namespace = "";

@Override
public void init(final NacosClientProperties properties, final NacosRestTemplate nacosRestTemplate) throws NacosException {
if (null == properties) {
throw new NacosException(NacosException.INVALID_PARAM, "properties is null");
}
initContextPath(properties);
initNameSpace(properties);
}

/**
* Get server list.
* @return server list
*/
@Override
public abstract List<String> getServerList();

/**
* Get server name.
* @return server name
*/
@Override
public abstract String getServerName();

/**
* Get order.
* @return order
*/
@Override
public abstract int getOrder();

public String getContextPath() {
return contextPath;
}

public String getNamespace() {
return namespace;
}

private void initContextPath(NacosClientProperties properties) {
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
this.contextPath = contentPathTmp;
}
}

private void initNameSpace(NacosClientProperties properties) {
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.address;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.utils.InternetAddressUtil;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTPS_PREFIX;
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX;

/**
* Address server list provider.
*
* @author totalo
*/
public class AddressServerListProvider extends AbstractServerListProvider {

private static final String FIXED_NAME = "fixed";

private List<String> serverList;

@Override
public void init(final NacosClientProperties properties, final NacosRestTemplate nacosRestTemplate) throws NacosException {
super.init(properties, nacosRestTemplate);
serverList = new ArrayList<>();
String serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
StringTokenizer serverAddrsTokens = new StringTokenizer(serverAddrsStr, ",;");
while (serverAddrsTokens.hasMoreTokens()) {
String serverAddr = serverAddrsTokens.nextToken().trim();
if (serverAddr.startsWith(HTTP_PREFIX) || serverAddr.startsWith(HTTPS_PREFIX)) {
this.serverList.add(serverAddr);
} else {
String[] serverAddrArr = InternetAddressUtil.splitIPPortStr(serverAddr);
if (serverAddrArr.length == 1) {
this.serverList
.add(serverAddrArr[0] + InternetAddressUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort());
} else {
this.serverList.add(serverAddr);
}
}
}
}

public String getFixedNameSuffix(String... serverIps) {
StringBuilder sb = new StringBuilder();
String split = "";
for (String serverIp : serverIps) {
sb.append(split);
serverIp = serverIp.replaceAll("http(s)?://", "");
sb.append(serverIp.replaceAll(":", "_"));
split = "-";
}
return sb.toString();
}

@Override
public List<String> getServerList() {
return serverList;
}

@Override
public String getServerName() {
return FIXED_NAME + "-" + (StringUtils.isNotBlank(namespace) ? (StringUtils.trim(namespace) + "-")
: "") + getFixedNameSuffix(serverList.toArray(new String[0]));
totalo marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public int getOrder() {
return ServerListProviderOrder.ORDER - 1;
}

@Override
public boolean match(final NacosClientProperties properties) {
return StringUtils.isNotBlank(properties.getProperty(PropertyKeyConst.SERVER_ADDR));
}

@Override
public boolean isFixed() {
return true;
}

@Override
public void shutdown() throws NacosException {
}
}
Loading