diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java index c0da0c7f3..fd0eaa24c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.controllers; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.nacos.api.common.Constants; @@ -25,7 +26,6 @@ import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.ServerStatusManager; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; -import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.ServiceManager; @@ -41,6 +41,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collections; @@ -79,9 +80,6 @@ public class OperatorController { @Autowired private RaftCore raftCore; - @Autowired - private RaftPeerSet raftPeerSet; - @RequestMapping("/push/state") public JSONObject pushState(HttpServletRequest request) { @@ -238,11 +236,10 @@ public class OperatorController { int page = Integer.parseInt(WebUtils.required(request, "pageNo")); int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize")); String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY); - String containedInstance = WebUtils.optional(request, "instance", StringUtils.EMPTY); List raftPeerLists = new ArrayList<>(); - int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, containedInstance, raftPeerLists, raftPeerSet); + int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, raftPeerLists); if (CollectionUtils.isEmpty(raftPeerLists)) { result.put("clusterStateList", Collections.emptyList()); @@ -265,4 +262,13 @@ public class OperatorController { result.put("count", total); return result; } + + @RequestMapping(value = "/cluster/state", method = RequestMethod.GET) + public JSONObject getClusterStates() { + + RaftPeer peer = serviceManager.getMySelfClusterState(); + + return JSON.parseObject(JSON.toJSONString(peer)); + + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index fc0ca3334..e0e854b80 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -31,6 +31,7 @@ import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.push.PushService; +import com.google.common.collect.Maps; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -83,6 +84,9 @@ public class ServiceManager implements RecordListener { @Autowired private PushService pushService; + @Autowired + private RaftPeerSet raftPeerSet; + private final Object putServiceLock = new Object(); @PostConstruct @@ -230,10 +234,30 @@ public class ServiceManager implements RecordListener { } } - public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List raftPeerList, RaftPeerSet raftPeerSet) { - - List matchList = new ArrayList<>(raftPeerSet.allPeers()); + public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, List raftPeerList) { + List matchList = new ArrayList<>(); + RaftPeer localRaftPeer = raftPeerSet.local(); + matchList.add(localRaftPeer); + Set otherServerSet = raftPeerSet.allServersWithoutMySelf(); + if (null != otherServerSet && otherServerSet.size() > 0) { + for (String server: otherServerSet) { + String path = UtilsAndCommons.NACOS_NAMING_OPERATOR_CONTEXT + UtilsAndCommons.NACOS_NAMING_CLUSTER_CONTEXT + "/state"; + Map params = Maps.newHashMapWithExpectedSize(2); + try { + String content = NamingProxy.reqCommon(path, params, server, false); + if (!StringUtils.EMPTY.equals(content)) { + RaftPeer raftPeer = JSONObject.parseObject(content, RaftPeer.class); + if (null != raftPeer) { + matchList.add(raftPeer); + } + } + } catch (Exception e) { + Loggers.SRV_LOG.warn("[QUERY-CLUSTER-STATE] Exception while query cluster state from {}, error: {}", + server, e); + } + } + } List tempList = new ArrayList<>(); if (StringUtils.isNotBlank(keyword)) { for (RaftPeer raftPeer : matchList) { @@ -265,6 +289,10 @@ public class ServiceManager implements RecordListener { return matchList.size(); } + public RaftPeer getMySelfClusterState() { + return raftPeerSet.local(); + } + public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) { Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName)); JSONObject serviceJson = JSON.parseObject(msg.getData()); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java index c1a4aa27a..8d716c33c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java @@ -215,6 +215,47 @@ public class NamingProxy { return StringUtils.EMPTY; } + public static String reqCommon(String path, Map params, String curServer, boolean isPost) throws Exception { + try { + List headers = Arrays.asList("Client-Version", UtilsAndCommons.SERVER_VERSION, + "User-Agent", UtilsAndCommons.SERVER_VERSION, + "Accept-Encoding", "gzip,deflate,sdch", + "Connection", "Keep-Alive", + "Content-Encoding", "gzip"); + + + HttpClient.HttpResult result; + + if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) { + curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort(); + } + + if (isPost) { + result = HttpClient.httpPost("http://" + curServer + RunningConfig.getContextPath() + + UtilsAndCommons.NACOS_NAMING_CONTEXT + path, headers, params); + } else { + result = HttpClient.httpGet("http://" + curServer + RunningConfig.getContextPath() + + UtilsAndCommons.NACOS_NAMING_CONTEXT + path, headers, params); + } + + if (HttpURLConnection.HTTP_OK == result.code) { + return result.content; + } + + if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) { + return StringUtils.EMPTY; + } + + throw new IOException("failed to req API:" + "http://" + curServer + + RunningConfig.getContextPath() + + UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:" + + result.code + " msg: " + result.content); + } catch (Exception e) { + Loggers.SRV_LOG.warn("NamingProxy", e); + } + return StringUtils.EMPTY; + } + public static class Request { private Map params = new HashMap<>(8);