Search in sources :

Example 1 with RsInfo

use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.

the class InstanceControllerV2 method beat.

/**
 * Create a beat for instance.
 *
 * @param namespaceId service namespaceId
 * @param serviceName service serviceName
 * @param ip          instance ip
 * @param clusterName service clusterName
 * @param port        instance port
 * @param beat        instance beat info
 * @return detail information of instance
 * @throws Exception any error during handle
 */
@CanDistro
@PutMapping("/beat")
@Secured(action = ActionTypes.WRITE)
public ObjectNode beat(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = StringUtils.EMPTY) String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName, @RequestParam(defaultValue = "0") Integer port, @RequestParam(defaultValue = StringUtils.EMPTY) String beat) throws Exception {
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
    RsInfo clientBeat = null;
    if (StringUtils.isNotBlank(beat)) {
        clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
    }
    if (clientBeat != null) {
        if (StringUtils.isNotBlank(clientBeat.getCluster())) {
            clusterName = clientBeat.getCluster();
        } else {
            // fix #2533
            clientBeat.setCluster(clusterName);
        }
        ip = clientBeat.getIp();
        port = clientBeat.getPort();
    }
    NamingUtils.checkServiceNameFormat(serviceName);
    Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat, serviceName, namespaceId);
    BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
    int resultCode = instanceServiceV2.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
    result.put(CommonParams.CODE, resultCode);
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instanceServiceV2.getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
    result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
    return result;
}
Also used : BeatInfoInstanceBuilder(com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 2 with RsInfo

use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.

the class InstanceOperatorServiceImpl method handleBeat.

@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat, BeatInfoInstanceBuilder builder) throws NacosException {
    com.alibaba.nacos.naming.core.Instance instance = serviceManager.getInstance(namespaceId, serviceName, cluster, ip, port);
    if (instance == null) {
        if (clientBeat == null) {
            return NamingResponseCode.RESOURCE_NOT_FOUND;
        }
        Loggers.SRV_LOG.warn("[CLIENT-BEAT] The instance has been removed for health mechanism, " + "perform data compensation operations, beat: {}, serviceName: {}", clientBeat, serviceName);
        instance = parseInstance(builder.setBeatInfo(clientBeat).setServiceName(serviceName).build());
        serviceManager.registerInstance(namespaceId, serviceName, instance);
    }
    Service service = serviceManager.getService(namespaceId, serviceName);
    serviceManager.checkServiceIsNull(service, namespaceId, serviceName);
    if (clientBeat == null) {
        clientBeat = new RsInfo();
        clientBeat.setIp(ip);
        clientBeat.setPort(port);
        clientBeat.setCluster(cluster);
    }
    service.processClientBeat(clientBeat);
    return NamingResponseCode.OK;
}
Also used : UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo)

Example 3 with RsInfo

use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.

the class InstanceOperatorClientImpl method handleBeat.

@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster, RsInfo clientBeat, BeatInfoInstanceBuilder builder) throws NacosException {
    Service service = getService(namespaceId, serviceName, true);
    String clientId = IpPortBasedClient.getClientId(ip + InternetAddressUtil.IP_PORT_SPLITER + port, true);
    IpPortBasedClient client = (IpPortBasedClient) clientManager.getClient(clientId);
    if (null == client || !client.getAllPublishedService().contains(service)) {
        if (null == clientBeat) {
            return NamingResponseCode.RESOURCE_NOT_FOUND;
        }
        Instance instance = builder.setBeatInfo(clientBeat).setServiceName(serviceName).build();
        registerInstance(namespaceId, serviceName, instance);
        client = (IpPortBasedClient) clientManager.getClient(clientId);
    }
    if (!ServiceManager.getInstance().containSingleton(service)) {
        throw new NacosException(NacosException.SERVER_ERROR, "service not found: " + serviceName + "@" + namespaceId);
    }
    if (null == clientBeat) {
        clientBeat = new RsInfo();
        clientBeat.setIp(ip);
        clientBeat.setPort(port);
        clientBeat.setCluster(cluster);
        clientBeat.setServiceName(serviceName);
    }
    ClientBeatProcessorV2 beatProcessor = new ClientBeatProcessorV2(namespaceId, clientBeat, client);
    HealthCheckReactor.scheduleNow(beatProcessor);
    client.setLastUpdatedTime();
    return NamingResponseCode.OK;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo) IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) NacosException(com.alibaba.nacos.api.exception.NacosException) ClientBeatProcessorV2(com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatProcessorV2)

Example 4 with RsInfo

use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.

the class DistroIpPortTagGenerator method getResponsibleTag.

@Override
public String getResponsibleTag(ReuseHttpServletRequest request) {
    String ip = request.getParameter(PARAMETER_IP);
    String port = request.getParameter(PARAMETER_PORT);
    if (StringUtils.isBlank(ip)) {
        // some old version clients using beat parameter
        String beatStr = request.getParameter(PARAMETER_BEAT);
        if (StringUtils.isNotBlank(beatStr)) {
            try {
                RsInfo rsInfo = JacksonUtils.toObj(beatStr, RsInfo.class);
                ip = rsInfo.getIp();
                port = String.valueOf(rsInfo.getPort());
            } catch (NacosDeserializationException ignored) {
            }
        }
    }
    if (StringUtils.isNotBlank(ip)) {
        ip = ip.trim();
    }
    port = StringUtils.isBlank(port) ? "0" : port.trim();
    return ip + InternetAddressUtil.IP_PORT_SPLITER + port;
}
Also used : NacosDeserializationException(com.alibaba.nacos.api.exception.runtime.NacosDeserializationException) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo)

Example 5 with RsInfo

use of com.alibaba.nacos.naming.healthcheck.RsInfo in project nacos by alibaba.

the class BeatInfoInstanceBuilderTest method setUp.

@Before
public void setUp() throws Exception {
    builder = BeatInfoInstanceBuilder.newBuilder();
    builder.setRequest(request);
    beatInfo = new RsInfo();
    beatInfo.setServiceName("g@@s");
    beatInfo.setCluster("c");
    beatInfo.setIp("1.1.1.1");
    beatInfo.setPort(8848);
    beatInfo.setWeight(10);
    beatInfo.setMetadata(new HashMap<>());
}
Also used : RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo) Before(org.junit.Before)

Aggregations

RsInfo (com.alibaba.nacos.naming.healthcheck.RsInfo)8 Secured (com.alibaba.nacos.auth.annotation.Secured)2 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)2 BeatInfoInstanceBuilder (com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder)2 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)2 CanDistro (com.alibaba.nacos.naming.web.CanDistro)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Test (org.junit.Test)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NacosDeserializationException (com.alibaba.nacos.api.exception.runtime.NacosDeserializationException)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)1 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)1 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)1 ClientBeatProcessorV2 (com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatProcessorV2)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1