use of com.alibaba.nacos.naming.core.Cluster in project nacos by alibaba.
the class DefaultServiceMetadataUpgradeHelper method toV2ServiceMetadata.
@Override
public ServiceMetadata toV2ServiceMetadata(Service service, boolean ephemeral) {
ServiceMetadata result = new ServiceMetadata();
result.setEphemeral(ephemeral);
result.setProtectThreshold(service.getProtectThreshold());
result.setSelector(service.getSelector());
result.setExtendData(service.getMetadata());
for (Map.Entry<String, Cluster> entry : service.getClusterMap().entrySet()) {
result.getClusters().put(entry.getKey(), toV2ClusterMetadata(entry.getValue()));
}
return result;
}
use of com.alibaba.nacos.naming.core.Cluster in project nacos by alibaba.
the class ClientBeatProcessor method run.
@Override
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);
for (Instance instance : instances) {
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());
if (!instance.isMarked() && !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);
getPushService().serviceChanged(service);
}
}
}
}
use of com.alibaba.nacos.naming.core.Cluster in project nacos by alibaba.
the class HealthCheckCommon method checkFail.
/**
* Health check fail, when instance check failed count more than max failed time, set unhealthy.
*
* @param ip instance
* @param task health check task
* @param msg message
*/
public void checkFail(Instance ip, HealthCheckTask task, String msg) {
Cluster cluster = task.getCluster();
try {
if (ip.isHealthy() || ip.isMockValid()) {
if (ip.getFailCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
if (distroMapper.responsible(cluster, ip)) {
ip.setHealthy(false);
ip.setMockValid(false);
Service service = cluster.getService();
service.setLastModifiedMillis(System.currentTimeMillis());
pushService.serviceChanged(service);
Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
} else {
Loggers.EVT_LOG.info("serviceName: {} {PROBE} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
}
} else {
Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-DISABLED} pre-invalid: {}:{}@{} in {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), ip.getFailCount(), msg);
}
}
} catch (Throwable t) {
Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
}
ip.getOkCount().set(0);
ip.setBeingChecked(false);
}
use of com.alibaba.nacos.naming.core.Cluster in project nacos by alibaba.
the class HealthCheckCommon method checkFailNow.
/**
* Health check fail, set instance unhealthy directly.
*
* @param ip instance
* @param task health check task
* @param msg message
*/
public void checkFailNow(Instance ip, HealthCheckTask task, String msg) {
Cluster cluster = task.getCluster();
try {
if (ip.isHealthy() || ip.isMockValid()) {
if (distroMapper.responsible(cluster, ip)) {
ip.setHealthy(false);
ip.setMockValid(false);
Service service = cluster.getService();
service.setLastModifiedMillis(System.currentTimeMillis());
pushService.serviceChanged(service);
Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid-now: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
} else {
if (ip.isMockValid()) {
ip.setMockValid(false);
Loggers.EVT_LOG.info("serviceName: {} {PROBE} {IP-DISABLED} invalid-now: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
}
}
}
} catch (Throwable t) {
Loggers.SRV_LOG.error("[CHECK-FAIL-NOW] error when close check task.", t);
}
ip.getOkCount().set(0);
ip.setBeingChecked(false);
}
use of com.alibaba.nacos.naming.core.Cluster in project nacos by alibaba.
the class ClusterControllerTest method testUpdate.
@Test
public void testUpdate() throws Exception {
Service service = new Service(TEST_SERVICE_NAME);
service.setNamespaceId("test-namespace");
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME).param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"HTTP\"}").param("checkPort", "1").param("useInstancePort4Check", "true");
Assert.assertEquals("ok", mockmvc.perform(builder).andReturn().getResponse().getContentAsString());
Cluster expectedCluster = new Cluster(TEST_CLUSTER_NAME, service);
Cluster actualCluster = service.getClusterMap().get(TEST_CLUSTER_NAME);
Assert.assertEquals(expectedCluster, actualCluster);
Assert.assertEquals(1, actualCluster.getDefCkport());
Assert.assertTrue(actualCluster.isUseIPPort4Check());
}
Aggregations