1. 程式人生 > >ribbon源碼之ServerListChangeListener

ribbon源碼之ServerListChangeListener

hang ble ... copy lean dds for 列表 remove

ServerListChangeListener

  用來監聽負載均衡器的服務器列表變化。

public interface ServerListChangeListener {
    /**
     * 當 BaseLoadBalancer的server列表變化時被調用。
     */
    public void serverListChanged(List<Server> oldList, List<Server> newList);
}

  通過BaseLoadBalancer的addServerListChangeListener和removeServerListChangeListener方法來增加和刪除ServerListChangeListener監聽器。

private List<ServerListChangeListener> changeListeners = new CopyOnWriteArrayList<ServerListChangeListener>();
...
public void addServerListChangeListener(ServerListChangeListener listener) {
        changeListeners.add(listener);
    }
    
    public void removeServerListChangeListener(ServerListChangeListener listener) {
        changeListeners.remove(listener);
    }
....

  當BaseLoadBalancer的Server列表發生變化時,調用註冊的ServerListChangeListener監聽器。

 public void setServersList(List lsrv) {
 ...

            boolean listChanged = false;
            if (!allServerList.equals(allServers)) {
                listChanged = true;
                if (changeListeners != null && changeListeners.size() > 0) {
                   List
<Server> oldList = ImmutableList.copyOf(allServerList); List<Server> newList = ImmutableList.copyOf(allServers); for (ServerListChangeListener l: changeListeners) { try { l.serverListChanged(oldList, newList); } catch (Exception e) { logger.error("LoadBalancer [{}]: Error invoking server list change listener", name, e); } } } } ... }

ribbon源碼之ServerListChangeListener