Search in sources :

Example 1 with Message

use of com.alibaba.nacos.naming.misc.Message in project nacos by alibaba.

the class ServiceManager method updatedHealthStatus.

/**
 * Update health status of instance in service.
 *
 * @param namespaceId namespace
 * @param serviceName service name
 * @param serverIP    source server Ip
 */
public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) {
    Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));
    JsonNode serviceJson = JacksonUtils.toObj(msg.getData());
    ArrayNode ipList = (ArrayNode) serviceJson.get("ips");
    Map<String, String> ipsMap = new HashMap<>(ipList.size());
    for (int i = 0; i < ipList.size(); i++) {
        String ip = ipList.get(i).asText();
        String[] strings = ip.split("_");
        ipsMap.put(strings[0], strings[1]);
    }
    Service service = getService(namespaceId, serviceName);
    if (service == null) {
        return;
    }
    boolean changed = false;
    List<Instance> instances = service.allIPs();
    for (Instance instance : instances) {
        boolean valid = Boolean.parseBoolean(ipsMap.get(instance.toIpAddr()));
        if (valid != instance.isHealthy()) {
            changed = true;
            instance.setHealthy(valid);
            Loggers.EVT_LOG.info("{} {SYNC} IP-{} : {}:{}@{}", serviceName, (instance.isHealthy() ? "ENABLED" : "DISABLED"), instance.getIp(), instance.getPort(), instance.getClusterName());
        }
    }
    if (changed) {
        pushService.serviceChanged(service);
        if (Loggers.EVT_LOG.isDebugEnabled()) {
            StringBuilder stringBuilder = new StringBuilder();
            List<Instance> allIps = service.allIPs();
            for (Instance instance : allIps) {
                stringBuilder.append(instance.toIpAddr()).append('_').append(instance.isHealthy()).append(',');
            }
            Loggers.EVT_LOG.debug("[HEALTH-STATUS-UPDATED] namespace: {}, service: {}, ips: {}", service.getNamespaceId(), service.getName(), stringBuilder.toString());
        }
    }
}
Also used : Message(com.alibaba.nacos.naming.misc.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with Message

use of com.alibaba.nacos.naming.misc.Message in project nacos by alibaba.

the class ServiceManagerTest method testUpdatedHealthStatus.

@Test
public void testUpdatedHealthStatus() {
    String namespaceId = "namespaceId";
    String serviceName = "testService";
    String serverIp = "127.0.0.1";
    String example = "{\"ips\":[\"127.0.0.1:8848_true\"]}";
    Message message = new Message();
    message.setData(example);
    when(synchronizer.get(serverIp, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName))).thenReturn(message);
    serviceManager.updatedHealthStatus(namespaceId, serviceName, serverIp);
}
Also used : Message(com.alibaba.nacos.naming.misc.Message) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Aggregations

Message (com.alibaba.nacos.naming.misc.Message)2 BaseTest (com.alibaba.nacos.naming.BaseTest)1 ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)1 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1