Skip to content

Commit

Permalink
client aware server list change (#5952)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiyue1102 committed Jun 4, 2021
1 parent 55c6f44 commit de6a4b8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Expand Up @@ -49,6 +49,9 @@
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.ConnectionEventListener;
import com.alibaba.nacos.common.remote.client.RpcClient;
Expand Down Expand Up @@ -699,6 +702,18 @@ public List<String> getServerList() {

}
});

NotifyCenter.registerSubscriber(new Subscriber() {
@Override
public void onEvent(Event event) {
rpcClientInner.onServerListChange();
}

@Override
public Class<? extends Event> subscribeType() {
return ServerlistChangeEvent.class;
}
});
}

@Override
Expand Down
Expand Up @@ -243,6 +243,29 @@ public boolean isShutdown() {
return this.rpcClientStatus.get() == RpcClientStatus.SHUTDOWN;
}

/**
* check if current connected server is in serverlist ,if not switch server.
*/
public void onServerListChange() {
if (currentConnection != null && currentConnection.serverInfo != null) {
ServerInfo serverInfo = currentConnection.serverInfo;
boolean found = false;
for (String serverAddress : serverListFactory.getServerList()) {
if (resolveServerInfo(serverAddress).getAddress().equalsIgnoreCase(serverInfo.getAddress())) {
found = true;
break;
}
}
if (!found) {
LoggerUtils.printIfInfoEnabled(LOGGER,
"Current connected server {} is not in latest server list,switch switchServerAsync",
serverInfo.getAddress());
switchServerAsync();
}

}
}

/**
* Start this client.
*/
Expand Down

0 comments on commit de6a4b8

Please sign in to comment.