diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Entity.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Entity.java index 6124839b2b3..509792a6bac 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Entity.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Entity.java @@ -19,6 +19,7 @@ /** * @author nkorange + * @since 0.7.0 */ public class Entity { diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEvent.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEvent.java index a51314ba4bc..15aafcb1031 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEvent.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEvent.java @@ -17,6 +17,7 @@ /** * @author nkorange + * @since 0.7.0 */ public class EntityEvent { diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEventType.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEventType.java index 5ad6dfac58e..c24f5de7009 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEventType.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/EntityEventType.java @@ -17,6 +17,7 @@ /** * @author nkorange + * @since 0.7.0 */ public enum EntityEventType { /** diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Label.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Label.java index 45883e6c331..ce00fc66001 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Label.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/Label.java @@ -19,6 +19,7 @@ /** * @author nkorange + * @since 0.7.0 */ public class Label { diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/PreservedEntityTypes.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/PreservedEntityTypes.java index 08d289c1056..d3c94272c60 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/PreservedEntityTypes.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/pojo/PreservedEntityTypes.java @@ -17,6 +17,7 @@ /** * @author nkorange + * @since 0.7.0 */ public enum PreservedEntityTypes { /** diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java index eadee776f33..4ecd210d71d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java @@ -27,6 +27,7 @@ * Service to visit CMDB store * * @author nkorange + * @since 0.7.0 */ public interface CmdbService { diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index 71c83de9335..7756bac3d0f 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -127,4 +127,7 @@ public class Constants { public static final String NAMING_HTTP_HEADER_SPILIER = "\\|"; public static final String NAMING_DEFAULT_CLUSTER_NAME = "DEFAULT"; + + public static final String REQUEST_PARAM_TENANT_ID = "tid"; + public static final String REQUEST_PARAM_SERVICE_NAME = "serviceName"; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index 2934ab17d67..0db2f6cd387 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -195,6 +195,7 @@ public interface NamingService { * @param selector selector to filter the resource * @return list of service names * @throws NacosException + * @since 0.7.0 */ ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException; diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java index b5dba2501da..24e72d30a08 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java @@ -190,13 +190,13 @@ public void setJsonFromServer(String jsonFromServer) { @JSONField(serialize = false) public String getKey() { - return getKey(name, clusters, env, isAllIPs()); + return getKey(name, clusters, env); } @JSONField(serialize = false) public String getKeyEncoded() { try { - return getKey(URLEncoder.encode(name, "UTF-8"), clusters, env, isAllIPs()); + return getKey(URLEncoder.encode(name, "UTF-8"), clusters, env); } catch (UnsupportedEncodingException e) { return getKey(); } @@ -204,31 +204,24 @@ public String getKeyEncoded() { @JSONField(serialize = false) public static String getKey(String name, String clusters, String unit) { - return getKey(name, clusters, unit, false); - } - - @JSONField(serialize = false) - public static String getKey(String name, String clusters, String unit, boolean isAllIPs) { if (isEmpty(unit)) { unit = EMPTY; } if (!isEmpty(clusters) && !isEmpty(unit)) { - return isAllIPs ? name + SPLITER + clusters + SPLITER + unit + SPLITER + ALL_IPS - : name + SPLITER + clusters + SPLITER + unit; + return name + SPLITER + clusters + SPLITER + unit; } if (!isEmpty(clusters)) { - return isAllIPs ? name + SPLITER + clusters + SPLITER + ALL_IPS : name + SPLITER + clusters; + return name + SPLITER + clusters; } if (!isEmpty(unit)) { - return isAllIPs ? name + SPLITER + EMPTY + SPLITER + unit + SPLITER + ALL_IPS : - name + SPLITER + EMPTY + SPLITER + unit; + return name + SPLITER + EMPTY + SPLITER + unit; } - return isAllIPs ? name + SPLITER + ALL_IPS : name; + return name; } @Override diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/AbstractSelector.java b/api/src/main/java/com/alibaba/nacos/api/selector/AbstractSelector.java index 650608c27f5..fcc4c4a7107 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/AbstractSelector.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/AbstractSelector.java @@ -19,6 +19,7 @@ * Abstract selector that only contains a type * * @author nkorange + * @since 0.7.0 */ public abstract class AbstractSelector { diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/ExpressionSelector.java b/api/src/main/java/com/alibaba/nacos/api/selector/ExpressionSelector.java index 35b8f0d43bf..8f3fec8b1ea 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/ExpressionSelector.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/ExpressionSelector.java @@ -19,6 +19,7 @@ * The selector to filter resource with flexible expression. * * @author nkorange + * @since 0.7.0 */ public class ExpressionSelector extends AbstractSelector { diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/SelectorType.java b/api/src/main/java/com/alibaba/nacos/api/selector/SelectorType.java index 9ec9ab25fc9..44429ec89bb 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/SelectorType.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/SelectorType.java @@ -19,6 +19,7 @@ * The types of selector accepted by Nacos * * @author nkorange + * @since 0.7.0 */ public enum SelectorType { /** diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 3c95db93f10..71bae46ac86 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -20,7 +20,6 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.listener.EventListener; -import com.alibaba.nacos.api.naming.pojo.Cluster; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; @@ -159,7 +158,7 @@ public void registerInstance(String serviceName, String ip, int port, String clu public void registerInstance(String serviceName, Instance instance) throws NacosException { BeatInfo beatInfo = new BeatInfo(); - beatInfo.setDom(serviceName); + beatInfo.setServiceName(serviceName); beatInfo.setIp(instance.getIp()); beatInfo.setPort(instance.getPort()); beatInfo.setCluster(instance.getClusterName()); @@ -191,7 +190,7 @@ public List getAllInstances(String serviceName) throws NacosException public List getAllInstances(String serviceName, List clusters) throws NacosException { ServiceInfo serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","), - StringUtils.EMPTY, false); + StringUtils.EMPTY); List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList(); @@ -209,7 +208,7 @@ public List selectInstances(String serviceName, List clusters, throws NacosException { ServiceInfo serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","), - StringUtils.EMPTY, false); + StringUtils.EMPTY); List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java index 2571723bc5b..c9195b56a28 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java @@ -27,7 +27,7 @@ public class BeatInfo { private int port; private String ip; private double weight; - private String dom; + private String serviceName; private String cluster; private Map metadata; @@ -36,12 +36,12 @@ public String toString() { return JSON.toJSONString(this); } - public String getDom() { - return dom; + public String getServiceName() { + return serviceName; } - public void setDom(String dom) { - this.dom = dom; + public void setServiceName(String serviceName) { + this.serviceName = serviceName; } public String getCluster() { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 2aefa1f2705..424080057ca 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.LogUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.util.HttpMethod; import java.util.HashMap; import java.util.Map; @@ -83,6 +84,7 @@ public void run() { } class BeatTask implements Runnable { + BeatInfo beatInfo; public BeatTask(BeatInfo beatInfo) { @@ -93,10 +95,10 @@ public BeatTask(BeatInfo beatInfo) { public void run() { Map params = new HashMap(2); params.put("beat", JSON.toJSONString(beatInfo)); - params.put("dom", beatInfo.getDom()); + params.put("serviceName", beatInfo.getServiceName()); try { - String result = serverProxy.callAllServers(UtilAndComs.NACOS_URL_BASE + "/api/clientBeat", params); + String result = serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/health", params, HttpMethod.POST); JSONObject jsonObject = JSON.parseObject(result); if (jsonObject != null) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index bf8727e9e92..0323c85dc45 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -190,53 +190,39 @@ public ServiceInfo processServiceJSON(String json) { private ServiceInfo getSerivceInfo0(String serviceName, String clusters, String env) { - String key = ServiceInfo.getKey(serviceName, clusters, env, false); + String key = ServiceInfo.getKey(serviceName, clusters, env); return serviceInfoMap.get(key); } private ServiceInfo getSerivceInfo0(String serviceName, String clusters, String env, boolean allIPs) { - String key = ServiceInfo.getKey(serviceName, clusters, env, allIPs); + String key = ServiceInfo.getKey(serviceName, clusters, env); return serviceInfoMap.get(key); } - public ServiceInfo getServiceInfo(String serviceName, String clusters, String env) { - return getServiceInfo(serviceName, clusters, env, false); + public ServiceInfo getServiceInfo(final String serviceName, final String clusters) { + return getServiceInfo(serviceName, clusters, StringUtils.EMPTY); } - public ServiceInfo getServiceInfo(String serviceName, String clusters) { - String env = StringUtils.EMPTY; - return getServiceInfo(serviceName, clusters, env, false); - } - public ServiceInfo getServiceInfo(final String serviceName, final String clusters, final String env, - final boolean allIPs) { + public ServiceInfo getServiceInfo(final String serviceName, final String clusters, final String env) { LogUtils.LOG.debug("failover-mode: " + failoverReactor.isFailoverSwitch()); - String key = ServiceInfo.getKey(serviceName, clusters, env, allIPs); + String key = ServiceInfo.getKey(serviceName, clusters, env); if (failoverReactor.isFailoverSwitch()) { return failoverReactor.getService(key); } - ServiceInfo serviceObj = getSerivceInfo0(serviceName, clusters, env, allIPs); + ServiceInfo serviceObj = getSerivceInfo0(serviceName, clusters, env); if (null == serviceObj) { serviceObj = new ServiceInfo(serviceName, clusters, env); - if (allIPs) { - serviceObj.setAllIPs(allIPs); - } - serviceInfoMap.put(serviceObj.getKey(), serviceObj); updatingMap.put(serviceName, new Object()); - - if (allIPs) { - updateService4AllIPNow(serviceName, clusters, env); - } else { - updateServiceNow(serviceName, clusters, env); - } + updateServiceNow(serviceName, clusters, env); updatingMap.remove(serviceName); } else if (updatingMap.containsKey(serviceName)) { @@ -248,29 +234,29 @@ public ServiceInfo getServiceInfo(final String serviceName, final String cluster serviceObj.wait(updateHoldInterval); } catch (InterruptedException e) { LogUtils.LOG.error("[getServiceInfo]", - "serviceName:" + serviceName + ", clusters:" + clusters + ", allIPs:" + allIPs, e); + "serviceName:" + serviceName + ", clusters:" + clusters, e); } } } } - scheduleUpdateIfAbsent(serviceName, clusters, env, allIPs); + scheduleUpdateIfAbsent(serviceName, clusters, env); return serviceInfoMap.get(serviceObj.getKey()); } - public void scheduleUpdateIfAbsent(String serviceName, String clusters, String env, boolean allIPs) { - if (futureMap.get(ServiceInfo.getKey(serviceName, clusters, env, allIPs)) != null) { + public void scheduleUpdateIfAbsent(String serviceName, String clusters, String env) { + if (futureMap.get(ServiceInfo.getKey(serviceName, clusters, env)) != null) { return; } synchronized (futureMap) { - if (futureMap.get(ServiceInfo.getKey(serviceName, clusters, env, allIPs)) != null) { + if (futureMap.get(ServiceInfo.getKey(serviceName, clusters, env)) != null) { return; } - ScheduledFuture future = addTask(new UpdateTask(serviceName, clusters, env, allIPs)); - futureMap.put(ServiceInfo.getKey(serviceName, clusters, env, allIPs), future); + ScheduledFuture future = addTask(new UpdateTask(serviceName, clusters, env)); + futureMap.put(ServiceInfo.getKey(serviceName, clusters, env), future); } } @@ -314,7 +300,7 @@ public void updateServiceNow(String serviceName, String clusters, String env) { ServiceInfo oldService = getSerivceInfo0(serviceName, clusters, env); try { Map params = new HashMap(8); - params.put("dom", serviceName); + params.put("serviceName", serviceName); params.put("clusters", clusters); params.put("udpPort", String.valueOf(pushRecver.getUDPPort())); params.put("env", env); @@ -337,7 +323,7 @@ public void updateServiceNow(String serviceName, String clusters, String env) { params.put("checksum", oldService.getChecksum()); } - String result = serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/api/srvIPXT", params); + String result = serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params); if (StringUtils.isNotEmpty(result)) { processServiceJSON(result); } @@ -353,10 +339,10 @@ public void updateServiceNow(String serviceName, String clusters, String env) { } } - public void refreshOnly(String serviceName, String clusters, String env, boolean allIPs) { + public void refreshOnly(String serviceName, String clusters, String env) { try { Map params = new HashMap(16); - params.put("dom", serviceName); + params.put("serviceName", serviceName); params.put("clusters", clusters); params.put("udpPort", String.valueOf(pushRecver.getUDPPort())); params.put("unit", env); @@ -376,11 +362,7 @@ public void refreshOnly(String serviceName, String clusters, String env, boolean params.put("useEnvId", "true"); } - if (allIPs) { - serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/api/srvAllIP", params); - } else { - serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/api/srvIPXT", params); - } + serverProxy.reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params); } catch (Exception e) { LogUtils.LOG.error("NA", "failed to update serviceName: " + serviceName, e); } @@ -391,7 +373,6 @@ public class UpdateTask implements Runnable { private String clusters; private String serviceName; private String env; - private boolean allIPs = false; public UpdateTask(String serviceName, String clusters, String env) { this.serviceName = serviceName; @@ -399,41 +380,24 @@ public UpdateTask(String serviceName, String clusters, String env) { this.env = env; } - public UpdateTask(String serviceName, String clusters, String env, boolean allIPs) { - this.serviceName = serviceName; - this.clusters = clusters; - this.env = env; - this.allIPs = allIPs; - } - @Override public void run() { try { - ServiceInfo serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters, env, allIPs)); + ServiceInfo serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters, env)); if (serviceObj == null) { - if (allIPs) { - updateService4AllIPNow(serviceName, clusters, env); - } else { - updateServiceNow(serviceName, clusters, env); - executor.schedule(this, DEFAULT_DELAY, TimeUnit.MILLISECONDS); - } + updateServiceNow(serviceName, clusters, env); + executor.schedule(this, DEFAULT_DELAY, TimeUnit.MILLISECONDS); return; } if (serviceObj.getLastRefTime() <= lastRefTime) { - if (allIPs) { - updateService4AllIPNow(serviceName, clusters, env); - serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters, env, true)); - } else { - updateServiceNow(serviceName, clusters, env); - serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters, env)); - } - + updateServiceNow(serviceName, clusters, env); + serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters, env)); } else { // if serviceName already updated by push, we should not override it // since the push data may be different from pull through force push - refreshOnly(serviceName, clusters, env, allIPs); + refreshOnly(serviceName, clusters, env); } executor.schedule(this, serviceObj.getCacheMillis(), TimeUnit.MILLISECONDS); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index aff9176b84b..d6cd292e621 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -18,13 +18,15 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.selector.AbstractSelector; -import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.client.naming.utils.*; +import com.alibaba.nacos.common.util.HttpMethod; import com.alibaba.nacos.common.util.UuidUtils; import java.io.IOException; @@ -96,9 +98,9 @@ public List getServerListFromEndpoint() { String urlString = "http://" + endpoint + "/nacos/serverlist"; List headers = Arrays.asList("Client-Version", UtilAndComs.VERSION, - "Accept-Encoding", "gzip,deflate,sdch", - "Connection", "Keep-Alive", - "RequestId", UuidUtils.generateUuid()); + "Accept-Encoding", "gzip,deflate,sdch", + "Connection", "Keep-Alive", + "RequestId", UuidUtils.generateUuid()); HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING); if (HttpURLConnection.HTTP_OK != result.code) { @@ -167,7 +169,7 @@ public void registerService(String serviceName, Instance instance) throws NacosE params.put("serviceName", serviceName); params.put("clusterName", instance.getClusterName()); - reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, "PUT"); + reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.PUT); } public void deregisterService(String serviceName, String ip, int port, String cluster) throws NacosException { @@ -182,7 +184,7 @@ public void deregisterService(String serviceName, String ip, int port, String cl params.put("serviceName", serviceName); params.put("cluster", cluster); - reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, "DELETE"); + reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); } public String queryList(String serviceName, String clusters, boolean healthyOnly) throws NacosException { @@ -193,7 +195,7 @@ public String queryList(String serviceName, String clusters, boolean healthyOnly params.put("clusters", clusters); params.put("healthyOnly", String.valueOf(healthyOnly)); - return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params, "GET"); + return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params, HttpMethod.GET); } public boolean serverHealthy() { @@ -284,7 +286,7 @@ public String reqAPI(String api, Map params, String method) thro } public String callServer(String api, Map params, String curServer) throws NacosException { - return callServer(api, params, curServer, "GET"); + return callServer(api, params, curServer, HttpMethod.GET); } public String callServer(String api, Map params, String curServer, String method) @@ -323,11 +325,13 @@ public String callServer(String api, Map params, String curServe } public String reqAPI(String api, Map params, List servers) { - return reqAPI(api, params, servers, "GET"); + return reqAPI(api, params, servers, HttpMethod.GET); } public String reqAPI(String api, Map params, List servers, String method) { + params.put(Constants.REQUEST_PARAM_TENANT_ID, getTenantId()); + if (CollectionUtils.isEmpty(servers) && StringUtils.isEmpty(nacosDomain)) { throw new IllegalArgumentException("no server available"); } @@ -363,4 +367,11 @@ public String reqAPI(String api, Map params, List server } + private String getTenantId() { + if (UtilAndComs.DEFAULT_NAMESPACE_ID.equals(namespace)) { + return StringUtils.EMPTY; + } + return namespace; + } + } diff --git a/client/src/test/java/com/alibaba/nacos/client/NamingTest.java b/client/src/test/java/com/alibaba/nacos/client/NamingTest.java index a57f13ae098..37963bdc6cf 100644 --- a/client/src/test/java/com/alibaba/nacos/client/NamingTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/NamingTest.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.client; import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; @@ -25,6 +27,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Properties; /** * @author nkorange @@ -35,7 +38,11 @@ public class NamingTest { @Ignore public void testServiceList() throws Exception { - NamingService namingService = NacosFactory.createNamingService("127.0.0.1:8848"); + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "11.160.165.126:8848"); + properties.put(PropertyKeyConst.NAMESPACE, "t1"); + + NamingService namingService = NacosFactory.createNamingService(properties); Instance instance = new Instance(); instance.setIp("1.1.1.1"); @@ -48,9 +55,9 @@ public void testServiceList() throws Exception { namingService.registerInstance("nacos.test.1", instance); - ExpressionSelector expressionSelector = new ExpressionSelector(); - expressionSelector.setExpression("INSTANCE.metadata.registerSource = 'dubbo'"); - ListView serviceList = namingService.getServicesOfServer(1, 10, expressionSelector); +// ExpressionSelector expressionSelector = new ExpressionSelector(); +// expressionSelector.setExpression("INSTANCE.metadata.registerSource = 'dubbo'"); +// ListView serviceList = namingService.getServicesOfServer(1, 10, expressionSelector); Thread.sleep(1000000000L); } diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/CmdbApp.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/CmdbApp.java index 96860e1f3db..8531eaac8e8 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/CmdbApp.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/CmdbApp.java @@ -20,6 +20,7 @@ /** * @author nkorange + * @since 0.7.0 */ @SpringBootApplication public class CmdbApp { diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/controllers/OperationController.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/controllers/OperationController.java index 9860d8e87a0..acee1747682 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/controllers/OperationController.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/controllers/OperationController.java @@ -28,6 +28,7 @@ /** * @author nkorange + * @since 0.7.0 */ @RestController @RequestMapping(UtilsAndCommons.NACOS_CMDB_CONTEXT + "/ops") diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/CmdbManager.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/CmdbManager.java index 597dc17907d..011725affec 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/CmdbManager.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/CmdbManager.java @@ -17,6 +17,7 @@ /** * @author nkorange + * @since 0.7.0 */ public class CmdbManager { } diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/SwitchAndOptions.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/SwitchAndOptions.java index dc199ae2756..893f0c836d4 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/SwitchAndOptions.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/core/SwitchAndOptions.java @@ -20,6 +20,7 @@ /** * @author nkorange + * @since 0.7.0 */ @Component public class SwitchAndOptions { diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java index 5e3b6d59c0c..24b6cfc8258 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/memory/CmdbProvider.java @@ -36,6 +36,7 @@ /** * @author nkorange + * @since 0.7.0 */ @Component public class CmdbProvider implements CmdbReader, CmdbWriter { diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbReader.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbReader.java index 75bebcbc282..acb81383923 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbReader.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbReader.java @@ -22,6 +22,7 @@ /** * @author nkorange + * @since 0.7.0 */ public interface CmdbReader { diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbWriter.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbWriter.java index ccd44c441a3..e860cb33343 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbWriter.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/service/CmdbWriter.java @@ -17,6 +17,7 @@ /** * @author nkorange + * @since 0.7.0 */ public interface CmdbWriter { } diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/Loggers.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/Loggers.java index 05df9e167db..f05408fcd33 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/Loggers.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/Loggers.java @@ -20,6 +20,7 @@ /** * @author nacos + * @since 0.7.0 */ public class Loggers { diff --git a/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/UtilsAndCommons.java b/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/UtilsAndCommons.java index ca1c51315f6..62a1c0457a7 100644 --- a/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/UtilsAndCommons.java +++ b/cmdb/src/main/java/com/alibaba/nacos/cmdb/utils/UtilsAndCommons.java @@ -21,6 +21,7 @@ /** * @author nkorange + * @since 0.7.0 */ public class UtilsAndCommons { diff --git a/common/src/main/java/com/alibaba/nacos/common/util/HttpMethod.java b/common/src/main/java/com/alibaba/nacos/common/util/HttpMethod.java new file mode 100644 index 00000000000..78b46ec58de --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/util/HttpMethod.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.common.util; + +/** + * @author nkorange + * @since 0.8.0 + */ +public class HttpMethod { + + public static final String GET = "GET"; + + public static final String HEAD = "HEAD"; + + public static final String POST = "POST"; + + public static final String PUT = "PUT"; + + public static final String PATCH = "PATCH"; + + public static final String DELETE = "PATCH"; + + public static final String OPTIONS = "PATCH"; + + public static final String TRACE = "TRACE"; +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java index 836107adb6b..bafc0b9d905 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java @@ -15,8 +15,27 @@ */ package com.alibaba.nacos.naming.controllers; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.core.utils.WebUtils; +import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.web.ApiCommands; +import com.alibaba.nacos.naming.web.OverrideParameterRequestWrapper; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + /** * @author nkorange + * @since 0.8.0 */ -public class HealthController { +@RestController +@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + UtilsAndCommons.NACOS_NAMING_HEALTH_CONTEXT) +public class HealthController extends ApiCommands { + + @RequestMapping(value = "", method = RequestMethod.POST) + public JSONObject update(HttpServletRequest request) throws Exception { + return clientBeat(OverrideParameterRequestWrapper.buildRequest(request, "dom", WebUtils.required(request, "serviceName"))); + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index a3b4d71ba9c..3f1b7cc5462 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -21,10 +21,9 @@ import com.alibaba.nacos.naming.core.IpAddress; import com.alibaba.nacos.naming.core.VirtualClusterDomain; import com.alibaba.nacos.naming.exception.NacosException; -import com.alibaba.nacos.naming.healthcheck.HealthCheckMode; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.web.ApiCommands; -import com.alibaba.nacos.naming.web.MockHttpRequest; +import com.alibaba.nacos.naming.web.OverrideParameterRequestWrapper; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -32,9 +31,7 @@ import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @author nkorange @@ -46,67 +43,18 @@ public class InstanceController extends ApiCommands { @RequestMapping(value = "/instance", method = RequestMethod.PUT) public String register(HttpServletRequest request) throws Exception { - Map params = new HashMap<>(request.getParameterMap()); - MockHttpRequest mockHttpRequest = MockHttpRequest.buildRequest(params); + OverrideParameterRequestWrapper requestWrapper = OverrideParameterRequestWrapper.buildRequest(request); String serviceJson = WebUtils.optional(request, "service", StringUtils.EMPTY); - String clusterJson = WebUtils.optional(request, "cluster", StringUtils.EMPTY); // set service info: if (StringUtils.isNotEmpty(serviceJson)) { JSONObject service = JSON.parseObject(serviceJson); - mockHttpRequest.addParameter("dom", service.getString("name")); - mockHttpRequest.addParameter("app", service.getString("app")); - mockHttpRequest.addParameter("group", service.getString("group")); - mockHttpRequest.addParameter("protectThreshold", service.getString("protectThreshold")); - - String healthCheckMode = service.getString("healthCheckMode"); - - if (HealthCheckMode.server.name().equals(healthCheckMode)) { - mockHttpRequest.addParameter("enableHealthCheck", "true"); - } - - if (HealthCheckMode.client.name().equals(healthCheckMode)) { - mockHttpRequest.addParameter("enableClientBeat", "true"); - } - - if (HealthCheckMode.none.name().equals(healthCheckMode)) { - mockHttpRequest.addParameter("enableHealthCheck", "false"); - mockHttpRequest.addParameter("enableClientBeat", "false"); - } - - mockHttpRequest.addParameter("serviceMetadata", service.getString("metadata")); + requestWrapper.addParameter("dom", service.getString("name")); } else { - mockHttpRequest.addParameter("dom", WebUtils.required(request, "serviceName")); - } - - // set cluster info: - if (StringUtils.isNotEmpty(clusterJson)) { - JSONObject cluster = JSON.parseObject(clusterJson); - String clusterName = cluster.getString("name"); - if (StringUtils.isEmpty(clusterName)) { - clusterName = UtilsAndCommons.DEFAULT_CLUSTER_NAME; - } - mockHttpRequest.addParameter("clusterName", clusterName); - - JSONObject healthChecker = cluster.getJSONObject("healthChecker"); - if (healthChecker == null) { - mockHttpRequest.addParameter("cktype", "TCP"); - } else { - for (String key : healthChecker.keySet()) { - mockHttpRequest.addParameter(key, healthChecker.getString(key)); - } - mockHttpRequest.addParameter("cktype", healthChecker.getString("type")); - } - - mockHttpRequest.addParameter("cluster", StringUtils.EMPTY); - mockHttpRequest.addParameter("defIPPort", cluster.getString("defaultPort")); - mockHttpRequest.addParameter("defCkport", cluster.getString("defaultCheckPort")); - mockHttpRequest.addParameter("ipPort4Check", cluster.getString("useIPPort4Check")); - mockHttpRequest.addParameter("clusterMetadata", cluster.getString("metadata")); - + requestWrapper.addParameter("dom", WebUtils.required(request, "serviceName")); } - return regService(mockHttpRequest); + return regService(requestWrapper); } @RequestMapping(value = "/instance", method = RequestMethod.DELETE) @@ -116,21 +64,12 @@ public String deregister(HttpServletRequest request) throws Exception { @RequestMapping(value = {"/instance/update", "instance"}, method = RequestMethod.POST) public String update(HttpServletRequest request) throws Exception { - String serviceName = WebUtils.required(request, "serviceName"); - Map params = new HashMap<>(request.getParameterMap()); - MockHttpRequest mockHttpRequest = MockHttpRequest.buildRequest(params); - mockHttpRequest.addParameter("dom", serviceName); - return regService(mockHttpRequest); + return regService(OverrideParameterRequestWrapper.buildRequest(request, "dom", WebUtils.required(request, "serviceName"))); } @RequestMapping(value = {"/instances", "/instance/list"}, method = RequestMethod.GET) public JSONObject queryList(HttpServletRequest request) throws Exception { - - Map params = new HashMap<>(request.getParameterMap()); - params.put("dom", params.get("serviceName")); - MockHttpRequest mockHttpRequest = MockHttpRequest.buildRequest(params); - - return srvIPXT(mockHttpRequest); + return srvIPXT(OverrideParameterRequestWrapper.buildRequest(request, "dom", WebUtils.required(request, "serviceName"))); } @RequestMapping(value = "/instance", method = RequestMethod.GET) @@ -170,6 +109,5 @@ public JSONObject queryDetail(HttpServletRequest request) throws Exception { } throw new IllegalStateException("no matched ip found!"); - } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java index 5a59b82c1db..529248acc07 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java @@ -32,18 +32,18 @@ public class RsInfo { private double mem; private int port; private String ip; - private String dom; + private String serviceName; private String ak; private String cluster; private double weight; private Map metadata; - public String getDom() { - return dom; + public String getServiceName() { + return serviceName; } - public void setDom(String dom) { - this.dom = dom; + public void setServiceName(String serviceName) { + this.serviceName = serviceName; } public String getAk() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index d5839f94fcb..79fc8f9fd68 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -49,6 +49,8 @@ public class UtilsAndCommons { public static final String NACOS_NAMING_INSTANCE_CONTEXT = "/instance"; + public static final String NACOS_NAMING_HEALTH_CONTEXT = "/health"; + public static final String NACOS_NAMING_RAFT_CONTEXT = "/raft"; public static final String NACOS_SERVER_HEADER = "Nacos-Server"; @@ -104,6 +106,8 @@ public class UtilsAndCommons { public static final String API_DOM = "/api/dom"; + public static final String SERVICE_TENANT_CONNECTOR = "::"; + public static final String INSTANCE_LIST_PERSISTED_PROPERTY_KEY = "nacos.instanceListPersisted"; public static final boolean INSTANCE_LIST_PERSISTED = Boolean.getBoolean(INSTANCE_LIST_PERSISTED_PROPERTY_KEY); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java index edaf15a5a6b..e26f0b7ece0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java @@ -103,7 +103,9 @@ public String getData(PushService.PushClient client) throws Exception { JSONObject result = new JSONObject(); try { - result = ApiCommands.this.srvIPXT(MockHttpRequest.buildRequest(params)); + result = ApiCommands.this.doSrvIPXT(client.getDom(), client.getAgent(), client.getClusters(), + inetAddress.getHostAddress(), 0, StringUtils.EMPTY, StringUtils.EMPTY, false, + StringUtils.EMPTY, StringUtils.EMPTY, false); } catch (Exception e) { Loggers.SRV_LOG.warn("PUSH-SERVICE: dom is not modified", e); } @@ -301,7 +303,7 @@ public JSONObject clientBeat(HttpServletRequest request) throws Exception { //if domain does not exist, register it. if (virtualClusterDomain == null) { - regDom(MockHttpRequest.buildRequest(stringMap)); + regDom(OverrideParameterRequestWrapper.buildRequest(request, stringMap)); Loggers.SRV_LOG.warn("dom not found, register it, dom:" + dom); } @@ -320,13 +322,13 @@ public JSONObject clientBeat(HttpServletRequest request) throws Exception { ipAddress.setInstanceId(ipAddress.generateInstanceId()); if (!virtualClusterDomain.getClusterMap().containsKey(ipAddress.getClusterName())) { - doAddCluster4Dom(MockHttpRequest.buildRequest(stringMap)); + doAddCluster4Dom(OverrideParameterRequestWrapper.buildRequest(request, stringMap)); } if (!virtualClusterDomain.allIPs().contains(ipAddress)) { stringMap.put("ipList", Arrays.asList(JSON.toJSONString(Arrays.asList(ipAddress))).toArray(new String[1])); stringMap.put("json", Arrays.asList("true").toArray(new String[1])); - addIP4Dom(MockHttpRequest.buildRequest(stringMap)); + addIP4Dom(OverrideParameterRequestWrapper.buildRequest(request, stringMap)); Loggers.SRV_LOG.warn("ip not found, register it, dom:" + dom + ", ip:" + ipAddress); } @@ -518,9 +520,8 @@ public String deRegService(HttpServletRequest request) throws Exception { parameterMap.put("ipList", Arrays.asList(JSON.toJSONString(Arrays.asList(ipAddress))).toArray(new String[1])); parameterMap.put("json", Arrays.asList("true").toArray(new String[1])); parameterMap.put("token", Arrays.asList(virtualClusterDomain.getToken()).toArray(new String[1])); - MockHttpRequest mockHttpRequest = MockHttpRequest.buildRequest(parameterMap); - return remvIP4Dom(mockHttpRequest); + return remvIP4Dom(OverrideParameterRequestWrapper.buildRequest(request, parameterMap)); } @@ -584,7 +585,7 @@ public void run() { stringMap.put("json", Arrays.asList("true").toArray(new String[1])); stringMap.put("token", Arrays.asList(virtualClusterDomain.getToken()).toArray(new String[1])); - doAddIP4Dom(MockHttpRequest.buildRequest(stringMap)); + doAddIP4Dom(OverrideParameterRequestWrapper.buildRequest(request, stringMap)); } else { throw new IllegalArgumentException("dom not found: " + dom); } @@ -1085,32 +1086,11 @@ public JSONObject srvAllIP(HttpServletRequest request) throws Exception { return result; } - @RequestMapping("/srvIPXT") - @ResponseBody - public JSONObject srvIPXT(HttpServletRequest request) throws Exception { + public JSONObject doSrvIPXT(String dom, String agent, String clusters, String clientIP, int udpPort, + String error, String env, boolean isCheck, String app, String tid, boolean healthyOnly) throws Exception { JSONObject result = new JSONObject(); - - if (DistroMapper.getLocalhostIP().equals(UtilsAndCommons.LOCAL_HOST_IP)) { - throw new Exception("invalid localhost ip: " + DistroMapper.getLocalhostIP()); - } - - String dom = WebUtils.required(request, "dom"); - VirtualClusterDomain domObj = (VirtualClusterDomain) domainsManager.getDomain(dom); - String agent = request.getHeader("Client-Version"); - String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); - String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); - Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); - String env = WebUtils.optional(request, "env", StringUtils.EMPTY); - String error = WebUtils.optional(request, "unconsistentDom", StringUtils.EMPTY); - boolean isCheck = Boolean.parseBoolean(WebUtils.optional(request, "isCheck", "false")); - - String app = WebUtils.optional(request, "app", StringUtils.EMPTY); - - String tenant = WebUtils.optional(request, "tid", StringUtils.EMPTY); - - boolean healthyOnly = Boolean.parseBoolean(WebUtils.optional(request, "healthOnly", "false")); if (!StringUtils.isEmpty(error)) { Loggers.ROLE_LOG.info("ENV-NOT-CONSISTENT", error); @@ -1128,12 +1108,12 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception { try { if (udpPort > 0 && PushService.canEnablePush(agent)) { PushService.addClient(dom, - clusters, - agent, - new InetSocketAddress(clientIP, udpPort), - pushDataSource, - tenant, - app); + clusters, + agent, + new InetSocketAddress(clientIP, udpPort), + pushDataSource, + tid, + app); cacheMillis = Switch.getPushCacheMillis(dom); } } catch (Exception e) { @@ -1173,7 +1153,7 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception { if ((float) ipMap.get(Boolean.TRUE).size() / srvedIPs.size() <= threshold) { Loggers.SRV_LOG.warn("protect threshold reached, return all ips, " + - "dom: " + dom); + "dom: " + dom); if (isCheck) { result.put("reachProtectThreshold", true); } @@ -1229,6 +1209,35 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception { return result; } + @RequestMapping("/srvIPXT") + @ResponseBody + public JSONObject srvIPXT(HttpServletRequest request) throws Exception { + + if (DistroMapper.getLocalhostIP().equals(UtilsAndCommons.LOCAL_HOST_IP)) { + throw new Exception("invalid localhost ip: " + DistroMapper.getLocalhostIP()); + } + + String dom = WebUtils.required(request, "dom"); + + VirtualClusterDomain domObj = (VirtualClusterDomain) domainsManager.getDomain(dom); + String agent = request.getHeader("Client-Version"); + String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); + String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); + Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); + String env = WebUtils.optional(request, "env", StringUtils.EMPTY); + String error = WebUtils.optional(request, "unconsistentDom", StringUtils.EMPTY); + boolean isCheck = Boolean.parseBoolean(WebUtils.optional(request, "isCheck", "false")); + + String app = WebUtils.optional(request, "app", StringUtils.EMPTY); + + String tenant = WebUtils.optional(request, "tid", StringUtils.EMPTY); + + boolean healthyOnly = Boolean.parseBoolean(WebUtils.optional(request, "healthyOnly", "false")); + + return doSrvIPXT(dom, agent, clusters, clientIP, udpPort, error, env, isCheck, app, tenant, healthyOnly); + + } + @NeedAuth @RequestMapping("/remvIP4Dom") public String remvIP4Dom(HttpServletRequest request) throws Exception { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 2abff4dfbec..c950fd988df 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -34,6 +34,7 @@ * @author nacos */ public class DistroFilter implements Filter { + @Override public void init(FilterConfig filterConfig) throws ServletException { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/MockHttpRequest.java b/naming/src/main/java/com/alibaba/nacos/naming/web/MockHttpRequest.java deleted file mode 100644 index 42e4391e25f..00000000000 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/MockHttpRequest.java +++ /dev/null @@ -1,403 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.naming.web; - -import javax.servlet.*; -import javax.servlet.http.*; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.Principal; -import java.util.*; - -/** - * @author nkorange - */ -public class MockHttpRequest implements HttpServletRequest { - - private Map params; - - public static MockHttpRequest buildRequest(Map params) { - - MockHttpRequest request = new MockHttpRequest(); - request.params = params; - request.params.put("encoding", new String[]{"UTF-8"}); - - return request; - } - - public static MockHttpRequest buildRequest2(Map params) { - - MockHttpRequest request = new MockHttpRequest(); - Map arrayMap = new HashMap<>(16); - for (String key : params.keySet()) { - arrayMap.put(key, new String[]{params.get(key)}); - } - request.params = arrayMap; - request.params.put("encoding", new String[]{"UTF-8"}); - - return request; - } - - public void addParameter(String key, String value) { - params.put(key, new String[]{value}); - } - - @Override - public String getAuthType() { - return null; - } - - @Override - public Cookie[] getCookies() { - return new Cookie[0]; - } - - @Override - public long getDateHeader(String s) { - return 0; - } - - @Override - public String getHeader(String s) { - return null; - } - - @Override - public Enumeration getHeaders(String s) { - return null; - } - - @Override - public Enumeration getHeaderNames() { - return null; - } - - @Override - public int getIntHeader(String s) { - return 0; - } - - @Override - public String getMethod() { - return null; - } - - @Override - public String getPathInfo() { - return null; - } - - @Override - public String getPathTranslated() { - return null; - } - - @Override - public String getContextPath() { - return null; - } - - @Override - public String getQueryString() { - return null; - } - - @Override - public String getRemoteUser() { - return null; - } - - @Override - public boolean isUserInRole(String s) { - return false; - } - - @Override - public Principal getUserPrincipal() { - return null; - } - - @Override - public String getRequestedSessionId() { - return null; - } - - @Override - public String getRequestURI() { - return null; - } - - @Override - public StringBuffer getRequestURL() { - return null; - } - - @Override - public String getServletPath() { - return null; - } - - @Override - public HttpSession getSession(boolean b) { - return null; - } - - @Override - public HttpSession getSession() { - return null; - } - - @Override - public String changeSessionId() { - return null; - } - - @Override - public boolean isRequestedSessionIdValid() { - return false; - } - - @Override - public boolean isRequestedSessionIdFromCookie() { - return false; - } - - @Override - public boolean isRequestedSessionIdFromURL() { - return false; - } - - @Override - public boolean isRequestedSessionIdFromUrl() { - return false; - } - - @Override - public boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException { - return false; - } - - @Override - public void login(String s, String s1) throws ServletException { - - } - - @Override - public void logout() throws ServletException { - - } - - @Override - public Collection getParts() throws IOException, ServletException { - return null; - } - - @Override - public Part getPart(String s) throws IOException, ServletException { - return null; - } - - @Override - public T upgrade(Class aClass) throws IOException, ServletException { - return null; - } - - @Override - public Object getAttribute(String s) { - return null; - } - - @Override - public Enumeration getAttributeNames() { - return null; - } - - @Override - public String getCharacterEncoding() { - return null; - } - - @Override - public void setCharacterEncoding(String s) throws UnsupportedEncodingException { - - } - - @Override - public int getContentLength() { - return 0; - } - - @Override - public long getContentLengthLong() { - return 0; - } - - @Override - public String getContentType() { - return null; - } - - @Override - public ServletInputStream getInputStream() throws IOException { - return null; - } - - @Override - public String getParameter(String s) { - return params.get(s)[0]; - } - - @Override - public Enumeration getParameterNames() { - return new Vector<>(params.keySet()).elements(); - } - - @Override - public String[] getParameterValues(String s) { - return params.get(s); - } - - @Override - public Map getParameterMap() { - return params; - } - - @Override - public String getProtocol() { - return null; - } - - @Override - public String getScheme() { - return null; - } - - @Override - public String getServerName() { - return null; - } - - @Override - public int getServerPort() { - return 0; - } - - @Override - public BufferedReader getReader() throws IOException { - return null; - } - - @Override - public String getRemoteAddr() { - return null; - } - - @Override - public String getRemoteHost() { - return null; - } - - @Override - public void setAttribute(String s, Object o) { - - } - - @Override - public void removeAttribute(String s) { - - } - - @Override - public Locale getLocale() { - return null; - } - - @Override - public Enumeration getLocales() { - return null; - } - - @Override - public boolean isSecure() { - return false; - } - - @Override - public RequestDispatcher getRequestDispatcher(String s) { - return null; - } - - @Override - public String getRealPath(String s) { - return null; - } - - @Override - public int getRemotePort() { - return 0; - } - - @Override - public String getLocalName() { - return null; - } - - @Override - public String getLocalAddr() { - return null; - } - - @Override - public int getLocalPort() { - return 0; - } - - @Override - public ServletContext getServletContext() { - return null; - } - - @Override - public AsyncContext startAsync() throws IllegalStateException { - return null; - } - - @Override - public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException { - return null; - } - - @Override - public boolean isAsyncStarted() { - return false; - } - - @Override - public boolean isAsyncSupported() { - return false; - } - - @Override - public AsyncContext getAsyncContext() { - return null; - } - - @Override - public DispatcherType getDispatcherType() { - return null; - } -} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java b/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java index 85855871191..f037a953c0d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java @@ -51,6 +51,18 @@ public FilterRegistrationBean authFilterRegistration() { return registration; } + @Bean + public FilterRegistrationBean tenantFilterRegistration() { + FilterRegistrationBean registration = new FilterRegistrationBean(); + + registration.setFilter(tenantFilter()); + registration.addUrlPatterns("/v1/ns/instance/*", "/v1/ns/service/*", "/v1/ns/cluster/*", "/v1/ns/health/*"); + registration.setName("tenantFilter"); + registration.setOrder(4); + + return registration; + } + @Bean public Filter distroFilter() { return new DistroFilter(); @@ -60,4 +72,9 @@ public Filter distroFilter() { public Filter authFilter() { return new AuthFilter(); } + + @Bean + public Filter tenantFilter() { + return new TenantFilter(); + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java new file mode 100644 index 00000000000..755a4547230 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java @@ -0,0 +1,86 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.naming.web; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.util.HashMap; +import java.util.Map; + +/** + * A request wrapper to override the parameters. + *

+ * Referenced article is https://blog.csdn.net/xieyuooo/article/details/8447301 + * + * @author nkorange + * @since 0.8.0 + */ +public class OverrideParameterRequestWrapper extends HttpServletRequestWrapper { + + private Map params = new HashMap(); + + /** + * Constructs a request object wrapping the given request. + * + * @param request The request to wrap + * @throws IllegalArgumentException if the request is null + */ + public OverrideParameterRequestWrapper(HttpServletRequest request) { + super(request); + this.params.putAll(request.getParameterMap()); + } + + public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request) { + return new OverrideParameterRequestWrapper(request); + } + + public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request, String name, String value) { + OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(request); + requestWrapper.addParameter(name, value); + return requestWrapper; + } + + public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request, Map appendParameters) { + OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(request); + requestWrapper.params.putAll(appendParameters); + return requestWrapper; + } + + @Override + public String getParameter(String name) { + String[] values = params.get(name); + if (values == null || values.length == 0) { + return null; + } + return values[0]; + } + + @Override + public Map getParameterMap() { + return params; + } + + public String[] getParameterValues(String name) { + return params.get(name); + } + + public void addParameter(String name, String value) { + if (value != null) { + params.put(name, new String[]{value}); + } + } + +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java new file mode 100644 index 00000000000..254bdc4c9c6 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java @@ -0,0 +1,67 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.naming.web; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.core.utils.WebUtils; +import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import org.apache.commons.lang3.StringUtils; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * A filter to intercept tenant parameter. + * + * @author nkorange + * @since 0.8.0 + */ +public class TenantFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse resp = (HttpServletResponse) response; + + String tenantId = WebUtils.optional(req, Constants.REQUEST_PARAM_TENANT_ID, StringUtils.EMPTY); + String serviceName = WebUtils.optional(req, Constants.REQUEST_PARAM_SERVICE_NAME, StringUtils.EMPTY); + + if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(serviceName)) { + chain.doFilter(req, resp); + return; + } + + OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(req); + requestWrapper.addParameter(Constants.REQUEST_PARAM_SERVICE_NAME, + serviceName + UtilsAndCommons.SERVICE_TENANT_CONNECTOR + tenantId); + + + chain.doFilter(requestWrapper, resp); + } + + @Override + public void destroy() { + + } +} diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java index 4db794aa30c..38f9d5b7699 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java @@ -163,7 +163,7 @@ public void autoRegDomTest() throws Exception { Assert.assertEquals(instances.size(), 1); BeatInfo beatInfo = new BeatInfo(); - beatInfo.setDom(serviceName); + beatInfo.setServiceName(serviceName); beatInfo.setIp("127.0.0.1"); beatInfo.setPort(TEST_PORT); @@ -207,7 +207,7 @@ public void autoRegDomClustersTest() throws Exception { Assert.assertEquals(instances.size(), 1); BeatInfo beatInfo = new BeatInfo(); - beatInfo.setDom(serviceName); + beatInfo.setServiceName(serviceName); beatInfo.setIp("127.0.0.1"); beatInfo.setPort(TEST_PORT); beatInfo.setCluster("c1"); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java index 8512b5294c4..201245181af 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.test.naming; +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; @@ -29,10 +31,7 @@ import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import static com.alibaba.nacos.test.naming.NamingBase.*; @@ -64,7 +63,14 @@ public void init() throws Exception { @Test @Ignore public void regService() throws NacosException, InterruptedException { - String serviceName = "dungu.test.99"; + + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); + properties.put(PropertyKeyConst.NAMESPACE, "t3"); + + naming = NacosFactory.createNamingService(properties); + + String serviceName = "dungu.test.8"; naming.registerInstance(serviceName, "127.0.0.1", 80, "c1"); naming.registerInstance(serviceName, "127.0.0.2", 80, "c2"); Thread.sleep(100000000L);