Search in sources :

Example 1 with Http

use of com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http in project nacos by alibaba.

the class HttpHealthCheckProcessor method process.

@Override
public void process(HealthCheckTaskV2 task, Service service, ClusterMetadata metadata) {
    HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
    if (null == instance) {
        return;
    }
    try {
        // TODO handle marked(white list) logic like v1.x.
        if (!instance.tryStartCheck()) {
            SRV_LOG.warn("http check started before last one finished, service: {} : {} : {}:{}", service.getGroupedServiceName(), instance.getCluster(), instance.getIp(), instance.getPort());
            healthCheckCommon.reEvaluateCheckRT(task.getCheckRtNormalized() * 2, task, switchDomain.getHttpHealthParams());
            return;
        }
        Http healthChecker = (Http) metadata.getHealthChecker();
        int ckPort = metadata.isUseInstancePortForCheck() ? instance.getPort() : metadata.getHealthyCheckPort();
        URL host = new URL(HTTP_PREFIX + instance.getIp() + ":" + ckPort);
        URL target = new URL(host, healthChecker.getPath());
        Map<String, String> customHeaders = healthChecker.getCustomHeaders();
        Header header = Header.newInstance();
        header.addAll(customHeaders);
        ASYNC_REST_TEMPLATE.get(target.toString(), header, Query.EMPTY, String.class, new HttpHealthCheckCallback(instance, task, service));
        MetricsMonitor.getHttpHealthCheckMonitor().incrementAndGet();
    } catch (Throwable e) {
        instance.setCheckRt(switchDomain.getHttpHealthParams().getMax());
        healthCheckCommon.checkFail(task, service, "http:error:" + e.getMessage());
        healthCheckCommon.reEvaluateCheckRT(switchDomain.getHttpHealthParams().getMax(), task, switchDomain.getHttpHealthParams());
    }
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Http(com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http) HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo) URL(java.net.URL)

Example 2 with Http

use of com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http in project nacos by alibaba.

the class ClusterTest method updateCluster.

@Test
public void updateCluster() {
    Service service = new Service();
    service.setName("nacos.service.2");
    Cluster newCluster = new Cluster("nacos-cluster-1", service);
    newCluster.setDefCkport(8888);
    newCluster.setDefIPPort(9999);
    Http healthCheckConfig = new Http();
    healthCheckConfig.setPath("/nacos-path-1");
    healthCheckConfig.setExpectedResponseCode(500);
    healthCheckConfig.setHeaders("Client-Version:nacos-test-1");
    newCluster.setHealthChecker(healthCheckConfig);
    cluster.update(newCluster);
    assertEquals(8888, cluster.getDefCkport());
    assertEquals(9999, cluster.getDefIPPort());
    assertTrue(cluster.getHealthChecker() instanceof Http);
    Http httpHealthCheck = (Http) (cluster.getHealthChecker());
    assertEquals("/nacos-path-1", httpHealthCheck.getPath());
    assertEquals(500, httpHealthCheck.getExpectedResponseCode());
    assertEquals("Client-Version:nacos-test-1", httpHealthCheck.getHeaders());
}
Also used : Http(com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http) Test(org.junit.Test)

Example 3 with Http

use of com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http in project nacos by alibaba.

the class HttpHealthCheckProcessor method process.

@Override
public void process(HealthCheckTask task) {
    List<Instance> ips = task.getCluster().allIPs(false);
    if (CollectionUtils.isEmpty(ips)) {
        return;
    }
    if (!switchDomain.isHealthCheckEnabled()) {
        return;
    }
    Cluster cluster = task.getCluster();
    for (Instance ip : ips) {
        try {
            if (ip.isMarked()) {
                if (SRV_LOG.isDebugEnabled()) {
                    SRV_LOG.debug("http check, ip is marked as to skip health check, ip: {}" + ip.getIp());
                }
                continue;
            }
            if (!ip.markChecking()) {
                SRV_LOG.warn("http check started before last one finished, service: {}:{}:{}", task.getCluster().getService().getName(), task.getCluster().getName(), ip.getIp());
                healthCheckCommon.reEvaluateCheckRT(task.getCheckRtNormalized() * 2, task, switchDomain.getHttpHealthParams());
                continue;
            }
            Http healthChecker = (Http) cluster.getHealthChecker();
            int ckPort = cluster.isUseIPPort4Check() ? ip.getPort() : cluster.getDefCkport();
            URL host = new URL(HTTP_PREFIX + ip.getIp() + ":" + ckPort);
            URL target = new URL(host, healthChecker.getPath());
            Map<String, String> customHeaders = healthChecker.getCustomHeaders();
            Header header = Header.newInstance();
            header.addAll(customHeaders);
            ASYNC_REST_TEMPLATE.get(target.toString(), header, Query.EMPTY, String.class, new HttpHealthCheckCallback(ip, task));
            MetricsMonitor.getHttpHealthCheckMonitor().incrementAndGet();
        } catch (Throwable e) {
            ip.setCheckRt(switchDomain.getHttpHealthParams().getMax());
            healthCheckCommon.checkFail(ip, task, "http:error:" + e.getMessage());
            healthCheckCommon.reEvaluateCheckRT(switchDomain.getHttpHealthParams().getMax(), task, switchDomain.getHttpHealthParams());
        }
    }
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Instance(com.alibaba.nacos.naming.core.Instance) Cluster(com.alibaba.nacos.naming.core.Cluster) Http(com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http) URL(java.net.URL)

Aggregations

Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)3 Header (com.alibaba.nacos.common.http.param.Header)2 URL (java.net.URL)2 Cluster (com.alibaba.nacos.naming.core.Cluster)1 Instance (com.alibaba.nacos.naming.core.Instance)1 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)1 Test (org.junit.Test)1