sequence

This article focuses on the DistroMapper of NacOS

ServerChangeListener

Nacos – 1.1.3 / naming/SRC/main/Java/com/alibaba/nacos/naming/cluster/servers/ServerChangeListener. Java

public interface ServerChangeListener {

    /**
     * If member list changed, this method is invoked.
     *
     * @param servers servers after change
     */
    void onChangeServerList(List<Server> servers);

    /**
     * If reachable member list changed, this method is invoked.
     *
     * @param healthyServer reachable servers after change
     */
    void onChangeHealthyServerList(List<Server> healthyServer);
}
Copy the code
  • ServerChangeListener defines onChangeServerList, onChangeHealthyServerList method

DistroMapper

Nacos – 1.1.3 / naming/SRC/main/Java/com/alibaba/nacos/naming/core/DistroMapper. Java

@Component("distroMapper")
public class DistroMapper implements ServerChangeListener {

    private List<String> healthyList = new ArrayList<>();

    public List<String> getHealthyList() {
        return healthyList;
    }

    @Autowired
    private SwitchDomain switchDomain;

    @Autowired
    private ServerListManager serverListManager;

    /**
     * init server list
     */
    @PostConstruct
    public void init() {
        serverListManager.listen(this);
    }

    public boolean responsible(Cluster cluster, Instance instance) {
        returnswitchDomain.isHealthCheckEnabled(cluster.getServiceName()) && ! cluster.getHealthCheckTask().isCancelled() && responsible(cluster.getServiceName()) && cluster.contains(instance); } public boolean responsible(String serviceName) {if(! switchDomain.isDistroEnabled() || SystemUtils.STANDALONE_MODE) {return true;
        }

        if (CollectionUtils.isEmpty(healthyList)) {
            // means distro config is not ready yet
            return false;
        }

        int index = healthyList.indexOf(NetUtils.localServer());
        int lastIndex = healthyList.lastIndexOf(NetUtils.localServer());
        if (lastIndex < 0 || index < 0) {
            return true;
        }

        int target = distroHash(serviceName) % healthyList.size();
        return target >= index && target <= lastIndex;
    }

    public String mapSrv(String serviceName) {
        if(CollectionUtils.isEmpty(healthyList) || ! switchDomain.isDistroEnabled()) {return NetUtils.localServer();
        }

        try {
            return healthyList.get(distroHash(serviceName) % healthyList.size());
        } catch (Exception e) {
            Loggers.SRV_LOG.warn("distro mapper failed, return localhost: " + NetUtils.localServer(), e);

            return NetUtils.localServer();
        }
    }

    public int distroHash(String serviceName) {
        return Math.abs(serviceName.hashCode() % Integer.MAX_VALUE);
    }

    @Override
    public void onChangeServerList(List<Server> latestMembers) {

    }

    @Override
    public void onChangeHealthyServerList(List<Server> latestReachableMembers) {

        List<String> newHealthyList = new ArrayList<>();
        for(Server server : latestReachableMembers) { newHealthyList.add(server.getKey()); } healthyList = newHealthyList; }}Copy the code
  • DistroMapper implements ServerChangeListener interface, its will update healthyList onChangeHealthyServerList method
  • It also provides a responsible method, this method in switchDomain. IsHealthCheckEnabled and cluster. The getHealthCheckTask will execute responsible under the condition of () is not cancelled, This method calls distroHash to compute the hash value
  • It also provides the mapSrv method, which also computs the hash via distroHash and then modulates the size of the healthyList to return the server’s key

summary

ServerChangeListener defines onChangeServerList, onChangeHealthyServerList method; DistroMapper implements ServerChangeListener interface, its onChangeHealthyServerList method will update healthyList; DistroMapper also provides the Responsible method and the mapSrv method

doc

  • ServerChangeListener