Skip to content

Commit

Permalink
[ISSUE alibaba#6019] Support fetching server list from endpoint with …
Browse files Browse the repository at this point in the history
…namespace. (alibaba#6020)
  • Loading branch information
pixystone authored Jun 9, 2021
1 parent 13fe9a8 commit 0d63616
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void init(Properties properties) throws NacosException {
namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
InitUtils.initWebRootContext(properties);
ServerListManager serverListManager = new ServerListManager(properties);
ServerListManager serverListManager = new ServerListManager(properties, namespace);
SecurityProxy securityProxy = new SecurityProxy(properties,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class ServerListManager implements ServerListFactory, Closeable {

private final long refreshServerListInternal = TimeUnit.SECONDS.toMillis(30);

private final String namespace;

private final AtomicInteger currentIndex = new AtomicInteger();

private final List<String> serverList = new ArrayList<>();
Expand All @@ -75,6 +77,11 @@ public class ServerListManager implements ServerListFactory, Closeable {
private long lastServerListRefreshTime = 0L;

public ServerListManager(Properties properties) {
this(properties, null);
}

public ServerListManager(Properties properties, String namespace) {
this.namespace = namespace;
initServerAddr(properties);
if (!serverList.isEmpty()) {
currentIndex.set(new Random().nextInt(serverList.size()));
Expand Down Expand Up @@ -105,7 +112,10 @@ private List<String> getServerListFromEndpoint() {
try {
String urlString = "http://" + endpoint + "/nacos/serverlist";
Header header = NamingHttpUtil.builderHeader();
HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, Query.EMPTY, String.class);
Query query = StringUtils.isNotBlank(namespace)
? Query.newInstance().addParam("namespace", namespace)
: Query.EMPTY;
HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, query, String.class);
if (!restResult.ok()) {
throw new IOException(
"Error while requesting: " + urlString + "'. Server returned: " + restResult.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfo
InstancesChangeNotifier changeNotifier) throws NacosException {
this.serviceInfoUpdateService = new ServiceInfoUpdateService(properties, serviceInfoHolder, this,
changeNotifier);
this.serverListManager = new ServerListManager(properties);
this.serverListManager = new ServerListManager(properties, namespace);
this.serviceInfoHolder = serviceInfoHolder;
this.securityProxy = new SecurityProxy(properties, NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy();
Expand Down

0 comments on commit 0d63616

Please sign in to comment.