-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: Unified Nacos Client address module code, and provide custom expansion capabilities * refactor: Adjust the priority of endpoint and server addr * refactor: adjust code for code review * refactor: adjust code for code review * refactor: adjust code for code review * refactor: adjust code for code review * refactor: adjust code for code review * refactor: adjust code for code review * refactor: add new line * refactor: adjust code for review * refactor: adjust code for review * refactor: adjust code for review * refactor: merge develop and fix test * refactor: revert test code * refactor: adjust for code review * refactor: fix checkstyle
- Loading branch information
Showing
41 changed files
with
1,462 additions
and
1,215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
client/src/main/java/com/alibaba/nacos/client/address/AbstractServerListManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 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 NacosClientProperties properties; | ||
|
||
public AbstractServerListManager(NacosClientProperties properties) throws NacosException { | ||
this(properties, null); | ||
} | ||
|
||
public AbstractServerListManager(NacosClientProperties properties, String namespace) throws NacosException { | ||
if (null == properties) { | ||
LOGGER.error("properties is null"); | ||
return; | ||
} | ||
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); | ||
Collection<ServerListProvider> sorted = serverListProviders.stream() | ||
.sorted((a, b) -> b.getOrder() - a.getOrder()).collect(Collectors.toList()); | ||
for (ServerListProvider each : sorted) { | ||
if (each.match(properties)) { | ||
this.serverListProvider = each; | ||
break; | ||
} | ||
} | ||
if (null == serverListProvider) { | ||
LOGGER.error("no server list provider found"); | ||
return; | ||
} | ||
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 NacosClientProperties getProperties() { | ||
return properties; | ||
} | ||
|
||
public String getServerName() { | ||
return getModuleName() + "-" + serverListProvider.getServerName(); | ||
} | ||
|
||
public String getContextPath() { | ||
return serverListProvider.getContextPath(); | ||
} | ||
|
||
public String getNamespace() { | ||
return serverListProvider.getNamespace(); | ||
} | ||
|
||
public String getAddressSource() { | ||
return serverListProvider.getAddressSource(); | ||
} | ||
|
||
public boolean isFixed() { | ||
return serverListProvider.isFixed(); | ||
} | ||
|
||
/** | ||
* get module name. | ||
* @return module name | ||
*/ | ||
public abstract String getModuleName(); | ||
|
||
/** | ||
* get nacos rest template. | ||
* @return nacos rest template | ||
*/ | ||
public abstract NacosRestTemplate getNacosRestTemplate(); | ||
} |
90 changes: 90 additions & 0 deletions
90
client/src/main/java/com/alibaba/nacos/client/address/AbstractServerListProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
client/src/main/java/com/alibaba/nacos/client/address/AddressServerListProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.constant.Constants.Address; | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getServerList() { | ||
return serverList; | ||
} | ||
|
||
@Override | ||
public String getServerName() { | ||
return FIXED_NAME + "-" + (StringUtils.isNotBlank(namespace) ? (StringUtils.trim(namespace) + "-") | ||
: "") + ParamUtil.getNameSuffixByServerIps(serverList.toArray(new String[0])); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Address.ADDRESS_SERVER_LIST_PROVIDER_ORDER; | ||
} | ||
|
||
@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 { | ||
} | ||
} |
Oops, something went wrong.