• [Nacos] Nacos Server处理心跳请求 (八)


    1.InstanceController#beat()

    在这里插入图片描述

        @CanDistro
        @PutMapping("/beat")
        @Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
        public ObjectNode beat(HttpServletRequest request) throws Exception {
            // 创建一个JSON Node,该方法的返回值就是它,后面的代码就是对这个Node进行各种初始化
            ObjectNode result = JacksonUtils.createEmptyJsonNode();
            result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
    
            // 从请求中获取到beat,即client端的beatInfo
            String beat = WebUtils.optional(request, "beat", StringUtils.EMPTY);
            RsInfo clientBeat = null;
            // 将beat构建为clientBeat
            if (StringUtils.isNotBlank(beat)) {
                clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
            }
            String clusterName = WebUtils
                    .optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME);
            String ip = WebUtils.optional(request, "ip", StringUtils.EMPTY);
            // 获取到客户端传递来的client的port,其将来用于UDP通信
            int port = Integer.parseInt(WebUtils.optional(request, "port", "0"));
            if (clientBeat != null) {
                if (StringUtils.isNotBlank(clientBeat.getCluster())) {
                    clusterName = clientBeat.getCluster();
                } else {
                    // fix #2533
                    clientBeat.setCluster(clusterName);
                }
                ip = clientBeat.getIp();
                port = clientBeat.getPort();
            }
            String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
            String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
            checkServiceNameFormat(serviceName);
            Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}", clientBeat, serviceName);
    
            // 从注册表中获取当前发送请求的client对应的instance,Ip port对应
            Instance instance = serviceManager.getInstance(namespaceId, serviceName, clusterName, ip, port);
    
            // 处理注册表中不存在该client的instance的情况
            if (instance == null) {
                // 若请求中没有携带心跳数据,则直接返回
                if (clientBeat == null) {
                    result.put(CommonParams.CODE, NamingResponseCode.RESOURCE_NOT_FOUND);
                    return result;
                }
    
                Loggers.SRV_LOG.warn("[CLIENT-BEAT] The instance has been removed for health mechanism, "
                        + "perform data compensation operations, beat: {}, serviceName: {}", clientBeat, serviceName);
    
                // 下面处理的情况是,注册表中没有该client的instance,但其发送的请求中具有心跳数据。
                // 在client的注册请求还未到达时(网络抖动等原因),第一次心跳请求先到达了server,会出现这种情况
                // 处理方式是,使用心跳数据构建出一个instance,注册到注册表
                instance = new Instance();
                instance.setPort(clientBeat.getPort());
                instance.setIp(clientBeat.getIp());
                instance.setWeight(clientBeat.getWeight());
                instance.setMetadata(clientBeat.getMetadata());
                instance.setClusterName(clusterName);
                instance.setServiceName(serviceName);
                instance.setInstanceId(instance.getInstanceId());
                instance.setEphemeral(clientBeat.isEphemeral());
                // 注册
                serviceManager.registerInstance(namespaceId, serviceName, instance);
            }
    
            // 从注册表中获取service
            Service service = serviceManager.getService(namespaceId, serviceName);
    
            if (service == null) {
                throw new NacosException(NacosException.SERVER_ERROR,
                        "service not found: " + serviceName + "@" + namespaceId);
            }
            // 从请求中获取到beat为null
            if (clientBeat == null) {
                clientBeat = new RsInfo();
                clientBeat.setIp(ip);
                clientBeat.setPort(port);
                clientBeat.setCluster(clusterName);
            }
            // 处理本次心跳
            service.processClientBeat(clientBeat);
    
            result.put(CommonParams.CODE, NamingResponseCode.OK);
            if (instance.containsMetadata(PreservedMetadataKeys.HEART_BEAT_INTERVAL)) {
                result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instance.getInstanceHeartBeatInterval());
            }
            result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
            return result;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    1. 创建一个JSON Node (result), 该方法的返回值就是它,后面的代码就是对这个Node进行各种初始化
    2. 从请求中获取到beat,即client端的beatInfo, 将beat构建为clientBeat
    3. 获取到客户端传递来的client的port,其将来用于UDP通信
    4. 从注册表中获取当前发送请求的client对应的instance,Ip port对应
      • 处理注册表中不存在该client的instance的情况, 若请求中没有携带心跳数据,则直接返回
      • 注册表中没有该client的instance,但其发送的请求中具有心跳数据。在client的注册请求还未到达时(网络抖动等原因),第一次心跳请求先到达了server,会出现这种情况, 使用心跳数据构建出一个instance,注册到注册表
    5. 从注册表中获取service, 并调用service.processClientBeat方法处理本次心跳
    1.1 serviceManager.registerInstance()

    在这里插入图片描述

    注册instance到service中。

    在这里插入图片描述

    之前的处理注册请求中分析过此代码。

    1. 创建一个空service
    2. 从注册表中获取到service
    3. instance写入到service,即写入到了注册表
    1.2 serviceManager.getService()

    在这里插入图片描述

    从注册表中获取service。

        public Service getService(String namespaceId, String serviceName) {
            if (serviceMap.get(namespaceId) == null) {
                return null;
            }
            return chooseServiceMap(namespaceId).get(serviceName);
        }
    
        public Map<String, Service> chooseServiceMap(String namespaceId) {
            return serviceMap.get(namespaceId);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    serviceMap为Server端的注册表。

    1.3 处理本次心跳

    在这里插入图片描述

    在这里插入图片描述

    1. 创建一个处理器,其是一个任务
    2. 开启一个立即执行的任务,即执行clientBeatProcessor任务的run()

    在这里插入图片描述

        public void run() {
            Service service = this.service;
            if (Loggers.EVT_LOG.isDebugEnabled()) {
                Loggers.EVT_LOG.debug("[CLIENT-BEAT] processing beat: {}", rsInfo.toString());
            }
    
            String ip = rsInfo.getIp();
            String clusterName = rsInfo.getCluster();
            int port = rsInfo.getPort();
            Cluster cluster = service.getClusterMap().get(clusterName);
            // 获取当前服务的所有临时实例
            List<Instance> instances = cluster.allIPs(true);
    
            // 遍历所有这些临时实例,从中查找当前发送心跳的instance
            for (Instance instance : instances) {
                // 只要ip与port与当前心跳的instance的相同,就是了
                if (instance.getIp().equals(ip) && instance.getPort() == port) {
                    if (Loggers.EVT_LOG.isDebugEnabled()) {
                        Loggers.EVT_LOG.debug("[CLIENT-BEAT] refresh beat: {}", rsInfo.toString());
                    }
                    // 修改最后心跳时间戳
                    instance.setLastBeat(System.currentTimeMillis());
                    // 修改该instance的健康状态
                    // 当instance被标记时,即其marked为true时,其是一个持久实例
                    if (!instance.isMarked()) {
                        // instance的healthy才是临时实例健康状态的表示
                        // 若当前instance健康状态为false,但本次是其发送的心跳,说明这个instance“起死回生”了,
                        // 我们需要将其health变为true
                        if (!instance.isHealthy()) {
                            instance.setHealthy(true);
                            Loggers.EVT_LOG
                                    .info("service: {} {POS} {IP-ENABLED} valid: {}:{}@{}, region: {}, msg: client beat ok",
                                            cluster.getService().getName(), ip, port, cluster.getName(),
                                            UtilsAndCommons.LOCALHOST_SITE);
                            // 发布服务变更事件(其对后续我们要分析的UDP通信非常重要)
                            getPushService().serviceChanged(service);
                        }
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    1. 获取当前服务的所有临时实例, 只有临时实例才会有心跳
    2. 遍历所有这些临时实例,从中查找当前发送心跳的instance, 只要ip与port与当前心跳的instance的相同,就是了
    3. 修改最后心跳时间戳, 这个为主要目的
    4. 修改该instance的健康状态, 如果没有被标记且不健康的实例为临时实例, 则修改健康状态为健康的
    5. 发布服务变更事件, 对于后面的UDP通信很重要

    在这里插入图片描述

  • 相关阅读:
    第三十一章 使用带附件的 SOAP
    SpringBoot+Vue+token实现(表单+图片)上传、图片地址保存到数据库。上传图片保存位置自己定义、图片可以在前端回显(一))
    自定义MVC增查
    说一下 ArrayDeque 和 LinkedList 的区别?
    ASP.NET Core如何知道一个请求执行了哪些中间件?
    程序设计:C++11原子 写优先的读写锁(源码详解二:操作跟踪)
    Eigen计算均值和标准差
    车辆车型识别系统python+TensorFlow+Django网页界面+算法模型
    《富爸爸财务自由之路》阅读笔记
    【光学】基于matlab GUI矩阵法和等效界面法光学薄膜对反射率影响【含Matlab源码 2102期】
  • 原文地址:https://blog.csdn.net/qq_43141726/article/details/130903606