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

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.lifecycle.Closeable;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.CollectionUtils;
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;

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 (StringUtils.isNotBlank(namespace)) {
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
}
Collection<ServerListProvider> serverListProviders = NacosServiceLoader.load(ServerListProvider.class);
totalo marked this conversation as resolved.
Show resolved Hide resolved
for (ServerListProvider each : serverListProviders) {
each.init(properties);
serverListProvider = each;
if (CollectionUtils.isNotEmpty(getServerList())) {
totalo marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}

@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);
ServerListHttpClientManager.getInstance().shutdown();
LOGGER.info("{} do shutdown stop", className);
}

public ServerListProvider getServerListProvider() {
return serverListProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.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) throws NacosException {
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,96 @@
/*
* 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.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) throws NacosException {
super.init(properties);
serverList = new ArrayList<>();
String serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
if (StringUtils.isBlank(serverAddrsStr)) {
return;
}
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;
}
}
Loading