[Nacos] Nacos Server main classes and interfaces (5)

InstanceController: Processor, which handles requests such as heartbeat and registration of service instances.

core/Service: A microservice defined by a microservice name on the Nacos client side appears as a Service instance on the Nacos server side. Similar to ServiceInfo, ServiceInfo serves the client, and Service serves the server.

RecordListener: The Service class implements the RecordListener interface, which is a data monitoring interface, so Service itself is a listener that can monitor changes and deletions of specified data.

Record: The generic type of the RecordListener interface specifies the entity type that the listener will listen to. This type is a sub-interface of Record, and Record is the record transmitted and stored by the Nacos cluster.

Cluster: Instance cluster that provides a certain service, and Service is n: 1 state, Service is 1.

Instance: A specific service instance registered in Nacos.

ServiceManager: The core manager of all services in Nacos. The serviceMap attribute is the service registry in Nacos.

Synchronizer: Synchronizer. Currently Nacos actively initiates a synchronization operation. It includes two methods, one is that the current Nacos actively sends its own message to the specified Nacos and actively obtains the message of the specified key from the specified Nacos.

Article directory

      • 1. Instance Controller
      • 2. core/Service
      • 3. RecordListener interface and Record interface
      • 4. Cluster class and Instance class
      • 5. Service Manager class
      • 6. Synchronizer

1. InstanceController

Handle requests such as heartbeat and registration of service instances.

2.core/Service

Inherit pojo/Service, implement Record and RecordListener interfaces.

There is an attribute protectThreshold in the Service class, which is the threshold.

Compared with the protection threshold in Eureka:

  • The same point: 0-1, indicating the proportion of healthy instances to all instances
  • The protection methods are different:
    • Eureka: Once the number of healthy instances is less than the threshold, unhealthy instances are no longer cleared from the registry
    • Nacos: If the number of healthy instances is greater than the threshold, consumers will call all healthy instances. Once the number of healthy instances is less than the threshold, the consumer will select calls from all instances, and may call unhealthy instances. This protects healthy instances from being overwhelmed.
  • The range is different:
    • Eureka: This threshold is for instances in all services
    • Nacos: This threshold is for the service instance in the current Service
 private int finalizeCount = 0;

    private String token;

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

    private Boolean resetWeight = false;

    private Boolean enabled = true;

    private Selector selector = new NoneSelector();

    private String namespaceId;

    /**
     * IP will be deleted if it has not send beat for some time, default timeout is 30 seconds.
     */
    private long ipDeleteTimeout = 30 * 1000;

    private volatile long lastModifiedMillis = 0L;

    // The checksum is the string concatenation of all SCI information of the current Service
    private volatile String checksum;

    /**
     * TODO set customized push expire time.
     */
    private long pushCacheMillis = 0L;
    // important collection
    // key is clusterName
    // value is the Cluster instance
    private Map<String, Cluster> clusterMap = new HashMap<>();

3. RecordListener interface and Record interface

RecordListener is a data listening interface

// The generic type specifies the data type that the current listener is listening to
public interface RecordListener<T extends Record> {<!-- -->

    /**
     * Determine if the listener was registered with this key.
     * Determine whether the current listener is listening to the data of the specified key
     *
     * @param key candidate key
     * @return true if the listener was registered with this key
     */
    boolean interests(String key);

    /**
     * Determine if the listener is to be removed by matching the 'key'.
     * Determine whether the current listener is no longer listening to the data of the currently specified key
     *
     * @param key key to match
     * @return true if match success
     */
    boolean matchUnlistenKey(String key);

    /**
     * Action to do if data of target key has changed.
     * If the data of the specified key has changed, trigger the execution of this method
     *
     * @param key target key
     * @param value data of the key
     * @throws Exception exception
     */
    void onChange(String key, T value) throws Exception;

    /**
     * Action to do if data of target key has been removed.
     * If the data of the specified key is deleted, the execution of this method will be triggered
     *
     * @param key target key
     * @throws Exception exception
     */
    void onDelete(String key) throws Exception;
}

Record is the record transmitted and stored by Nacos cluster.

public interface Record {<!-- -->

    /**
     * get the checksum of this record, usually for record comparison.
     *
     * @return checksum of record
     */
    String getChecksum();
}

4.Cluster class and Instance class

Cluster class: An instance cluster that provides a certain service, that is, an instance cluster that belongs to a certain Service.

Instance: A specific service instance registered in Nacos.

5. ServiceManager class

ServiceManager: The core manager of all services in Nacos. The serviceMap attribute is the service registry on the Nacos Server side.

serviceMap is the service registry of Naocs Server. serviceInfoMap is the service registry of Nacos Client

6. Synchronizer

service state synchronizer

public interface Synchronizer {<!-- -->

    /**
     * Send message to server.
     * Send msg to the specified server
     *
     * @param serverIP target server address
     * @param msg message to send
     */
    void send(String serverIP, Message msg);

    /**
     * Get message from server using message key.
     *
     * @param serverIP source server address
     * @param key message key
     * @return message
     */
    Message get(String serverIP, String key);
}

The send method is to send the msg to the specified server, and the get method is to get the msg from the specified server.