Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Service registration in nacOS Registry

NacosServiceRegistry register method:

public void register(Registration registration) {
    if (StringUtils.isEmpty(registration.getServiceId())) {
        log.warn("No service to register for nacos client...");
    } else {
        String serviceId = registration.getServiceId();
        String group = this.nacosDiscoveryProperties.getGroup();
        Instance instance = this.getNacosInstanceFromRegistration(registration);

        try {
            this.namingService.registerInstance(serviceId, group, instance);
            log.info("nacos registry, {} {} {}:{} register finished".new Object[]{group, serviceId, instance.getIp(), instance.getPort()});
        } catch (Exception var6) {
            log.error("nacos registry, {} register failed... {},".newObject[]{serviceId, registration.toString(), var6}); ReflectionUtils.rethrowRuntimeException(var6); }}}Copy the code

Method to register the service by calling Nacos Client’s registerInstance

RegisterInstance method:

@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {

    if (instance.isEphemeral()) {
        BeatInfo beatInfo = new BeatInfo();
        beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
        beatInfo.setIp(instance.getIp());
        beatInfo.setPort(instance.getPort());
        beatInfo.setCluster(instance.getClusterName());
        beatInfo.setWeight(instance.getWeight());
        beatInfo.setMetadata(instance.getMetadata());
        beatInfo.setScheduled(false);
        long instanceInterval = instance.getInstanceHeartBeatInterval();
        beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval);

        beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
    }

    serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
}
Copy the code
  1. Naocs Server must ensure that the registered service instances are healthy, and heartbeat detection is the means of service health detection
  2. Registered serverProxy. RegisterService implementation services.

AddBeatInfo method:

public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
    NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
    String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());
    BeatInfo existBeat = null;
    //fix #1733
    if((existBeat = dom2Beat.remove(key)) ! =null) {
        existBeat.setStopped(true);
    }
    dom2Beat.put(key, beatInfo);
    executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
    MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
}
Copy the code

The so-called heartbeat mechanism is that the client periodically sends a packet to the server through the schedule method, and then starts a thread to continuously detect the response from the server. If no response is received from the server within the specified time, the server is considered to be faulty. The Nacos server constantly updates the service status based on the heartbeat packets of the client.

Followed by serverProxy. RegisterService for service registration, enter the method can see is actually a HTTP POST request, namely Nacos provides the Open API to implement service registration, encapsulating the SDK.