#502 Compatible with 0.8.0

This commit is contained in:
nkorange 2019-03-14 16:04:07 +08:00
parent 902a77aedd
commit 6034bdbdb9
9 changed files with 105 additions and 51 deletions

View File

@ -35,7 +35,6 @@ public class ServiceInfo {
private String jsonFromServer = EMPTY;
public static final String SPLITER = "@@";
@JSONField(name = "dom")
private String name;
private String groupName;

View File

@ -0,0 +1,43 @@
/*
* 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.api.naming.utils;
import com.alibaba.nacos.api.common.Constants;
/**
* @author nkorange
* @since 1.0.0
*/
public class NamingUtils {
public static String getGroupedName(String serviceName, String groupName) {
return groupName + Constants.SERVICE_INFO_SPLITER + serviceName;
}
public static String getServiceName(String serviceNameWithGroup) {
if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) {
return serviceNameWithGroup;
}
return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[1];
}
public static String getGroupName(String serviceNameWithGroup) {
if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) {
return Constants.DEFAULT_GROUP;
}
return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[0];
}
}

View File

@ -24,6 +24,7 @@ import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.client.identify.CredentialService;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
@ -283,7 +284,7 @@ public class NacosNamingService implements NamingService {
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(groupName + Constants.SERVICE_INFO_SPLITER + serviceName);
beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
@ -291,9 +292,9 @@ public class NacosNamingService implements NamingService {
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
beatReactor.addBeatInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, beatInfo);
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
serverProxy.registerService(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, groupName, instance);
serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
}
@Override
@ -313,8 +314,8 @@ public class NacosNamingService implements NamingService {
@Override
public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {
beatReactor.removeBeatInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port);
serverProxy.deregisterService(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, ip, port, clusterName);
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), ip, port);
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), ip, port, clusterName);
}
@Override
@ -358,9 +359,9 @@ public class NacosNamingService implements NamingService {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","));
serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","));
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));
}
List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
@ -412,9 +413,9 @@ public class NacosNamingService implements NamingService {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","));
serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","));
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","));
}
return selectInstances(serviceInfo, healthy);
}
@ -460,10 +461,10 @@ public class NacosNamingService implements NamingService {
if (subscribe) {
return Balancer.RandomByWeight.selectHost(
hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")));
hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")));
} else {
return Balancer.RandomByWeight.selectHost(
hostReactor.getServiceInfoDirectlyFromServer(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ",")));
hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")));
}
}
@ -484,7 +485,7 @@ public class NacosNamingService implements NamingService {
@Override
public void subscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException {
eventDispatcher.addListener(hostReactor.getServiceInfo(groupName + Constants.SERVICE_INFO_SPLITER + serviceName,
eventDispatcher.addListener(hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName),
StringUtils.join(clusters, ",")), StringUtils.join(clusters, ","), listener);
}
@ -505,7 +506,7 @@ public class NacosNamingService implements NamingService {
@Override
public void unsubscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException {
eventDispatcher.removeListener(groupName + Constants.SERVICE_INFO_SPLITER + serviceName, StringUtils.join(clusters, ","), listener);
eventDispatcher.removeListener(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","), listener);
}
@Override

View File

@ -261,7 +261,7 @@ public class ServerListManager {
if (autoDisabledHealthCheck
&& curRatio > switchDomain.getDistroThreshold()
&& System.currentTimeMillis() - lastHealthServerMillis > STABLE_PERIOD) {
Loggers.SRV_LOG.info("[VIPSRV-DISTRO] distro threshold restored and " +
Loggers.SRV_LOG.info("[NACOS-DISTRO] distro threshold restored and " +
"stable now, enable health check. current ratio: {}", curRatio);
switchDomain.setHealthCheckEnabled(true);
@ -273,7 +273,7 @@ public class ServerListManager {
if (!CollectionUtils.isEqualCollection(healthyServers, newHealthyList)) {
// for every change disable healthy check for some while
if (switchDomain.isHealthCheckEnabled()) {
Loggers.SRV_LOG.info("[VIPSRV-DISTRO] healthy server list changed, " +
Loggers.SRV_LOG.info("[NACOS-DISTRO] healthy server list changed, " +
"disable health check for {} ms from now on", STABLE_PERIOD);
switchDomain.setHealthCheckEnabled(false);
@ -301,7 +301,6 @@ public class ServerListManager {
}
}
public Set<String> getLiveSites() {
return liveSites;
}

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.controllers;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.ServiceManager;
@ -38,9 +39,9 @@ import java.util.*;
* Old API entry
*
* @author nkorange
* @deprecated
*/
@RestController
@Deprecated
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api")
public class ApiController extends InstanceController {
@ -60,13 +61,14 @@ public class ApiController extends InstanceController {
String dnsfVersion = "1.0.1";
String agent = request.getHeader("Client-Version");
ClientInfo clientInfo = new ClientInfo(agent);
if (clientInfo.type == ClientInfo.ClientType.DNS && clientInfo.version.compareTo(VersionUtil.parseVersion(dnsfVersion)) <= 0) {
if (clientInfo.type == ClientInfo.ClientType.DNS &&
clientInfo.version.compareTo(VersionUtil.parseVersion(dnsfVersion)) <= 0) {
List<String> doms = new ArrayList<String>();
Set<String> domSet = null;
if (domMap.containsKey(CommonParams.NAMESPACE_ID)) {
domSet = domMap.get(CommonParams.NAMESPACE_ID);
if (domMap.containsKey(Constants.DEFAULT_NAMESPACE_ID)) {
domSet = domMap.get(Constants.DEFAULT_NAMESPACE_ID);
}
if (CollectionUtils.isEmpty(domSet)) {
@ -77,7 +79,7 @@ public class ApiController extends InstanceController {
for (String dom : domSet) {
if (distroMapper.responsible(dom) || !responsibleOnly) {
doms.add(dom);
doms.add(NamingUtils.getServiceName(dom));
}
}
@ -92,7 +94,7 @@ public class ApiController extends InstanceController {
doms.put(namespaceId, new HashSet<>());
for (String dom : domMap.get(namespaceId)) {
if (distroMapper.responsible(dom) || !responsibleOnly) {
doms.get(namespaceId).add(dom);
doms.get(namespaceId).add(NamingUtils.getServiceName(dom));
}
}
count += doms.get(namespaceId).size();
@ -104,6 +106,12 @@ public class ApiController extends InstanceController {
return result;
}
@RequestMapping("/hello")
@ResponseBody
public String hello(HttpServletRequest request) throws Exception {
return "ok";
}
@RequestMapping("/srvIPXT")
@ResponseBody
public JSONObject srvIPXT(HttpServletRequest request) throws Exception {
@ -126,6 +134,7 @@ public class ApiController extends InstanceController {
boolean healthyOnly = Boolean.parseBoolean(WebUtils.optional(request, "healthyOnly", "false"));
return doSrvIPXT(namespaceId, dom, agent, clusters, clientIP, udpPort, env, isCheck, app, tenant, healthyOnly);
return doSrvIPXT(namespaceId, NamingUtils.getGroupedName(dom, Constants.DEFAULT_GROUP),
agent, clusters, clientIP, udpPort, env, isCheck, app, tenant, healthyOnly);
}
}

View File

@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.pojo.Cluster;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.Service;
@ -69,9 +70,9 @@ public class CatalogController {
JSONObject detailView = new JSONObject();
JSONObject serviceObject = new JSONObject();
serviceObject.put("name", UtilsAndCommons.getServiceName(serviceName));
serviceObject.put("name", NamingUtils.getServiceName(serviceName));
serviceObject.put("protectThreshold", detailedService.getProtectThreshold());
serviceObject.put("groupName", UtilsAndCommons.getGroupName(serviceName));
serviceObject.put("groupName", NamingUtils.getGroupName(serviceName));
serviceObject.put("selector", detailedService.getSelector());
serviceObject.put("metadata", detailedService.getMetadata());
@ -155,8 +156,8 @@ public class CatalogController {
(serviceName, service) -> {
ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo();
serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName));
serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName));
serviceDetailInfo.setServiceName(NamingUtils.getServiceName(serviceName));
serviceDetailInfo.setGroupName(NamingUtils.getGroupName(serviceName));
serviceDetailInfo.setMetadata(service.getMetadata());
Map<String, ClusterInfo> clusterInfoMap = getStringClusterInfoMap(service);
@ -270,8 +271,8 @@ public class CatalogController {
JSONArray serviceJsonArray = new JSONArray();
for (Service service : services) {
ServiceView serviceView = new ServiceView();
serviceView.setName(UtilsAndCommons.getServiceName(service.getName()));
serviceView.setGroupName(UtilsAndCommons.getGroupName(service.getName()));
serviceView.setName(NamingUtils.getServiceName(service.getName()));
serviceView.setGroupName(NamingUtils.getGroupName(service.getName()));
serviceView.setClusterCount(service.getClusterMap().size());
serviceView.setIpCount(service.allIPs().size());

View File

@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.core.DistroMapper;
@ -31,12 +32,14 @@ import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.ClientInfo;
import com.alibaba.nacos.naming.push.DataSource;
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.web.CanDistro;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.util.VersionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -444,6 +447,8 @@ public class InstanceController {
JSONArray hosts = new JSONArray();
ClientInfo clientInfo = new ClientInfo(agent);
for (Map.Entry<Boolean, List<Instance>> entry : ipMap.entrySet()) {
List<Instance> ips = entry.getValue();
@ -465,7 +470,13 @@ public class InstanceController {
ipObj.put("enabled", instance.isEnabled());
ipObj.put("weight", instance.getWeight());
ipObj.put("clusterName", instance.getClusterName());
ipObj.put("serviceName", instance.getServiceName());
if (clientInfo.type == ClientInfo.ClientType.JAVA &&
clientInfo.version.compareTo(VersionUtil.parseVersion("1.0.0")) >= 0) {
ipObj.put("serviceName", instance.getServiceName());
} else {
ipObj.put("serviceName", NamingUtils.getServiceName(instance.getServiceName()));
}
ipObj.put("ephemeral", instance.isEphemeral());
hosts.add(ipObj);
@ -473,12 +484,16 @@ public class InstanceController {
}
result.put("hosts", hosts);
result.put("dom", serviceName);
if (clientInfo.type == ClientInfo.ClientType.JAVA &&
clientInfo.version.compareTo(VersionUtil.parseVersion("1.0.0")) >= 0) {
result.put("dom", serviceName);
} else {
result.put("dom", NamingUtils.getServiceName(serviceName));
}
result.put("name", serviceName);
result.put("cacheMillis", cacheMillis);
result.put("lastRefTime", System.currentTimeMillis());
result.put("checksum", service.getChecksum() + System.currentTimeMillis());
result.put("checksum", service.getChecksum());
result.put("useSpecifiedURL", false);
result.put("clusters", clusters);
result.put("env", env);

View File

@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager;
@ -131,12 +132,12 @@ public class ServiceController {
}
JSONObject res = new JSONObject();
res.put("name", UtilsAndCommons.getServiceName(serviceName));
res.put("name", NamingUtils.getServiceName(serviceName));
res.put("namespaceId", service.getNamespaceId());
res.put("protectThreshold", service.getProtectThreshold());
res.put("metadata", service.getMetadata());
res.put("selector", service.getSelector());
res.put("groupName", UtilsAndCommons.getGroupName(serviceName));
res.put("groupName", NamingUtils.getGroupName(serviceName));
JSONArray clusters = new JSONArray();
for (Cluster cluster : service.getClusterMap().values()) {
@ -284,7 +285,7 @@ public class ServiceController {
serviceNameMap.put(namespace, new HashSet<>());
for (Service service : services.get(namespace)) {
if (distroMapper.responsible(service.getName()) || !responsibleOnly) {
serviceNameMap.get(namespace).add(UtilsAndCommons.getServiceName(service.getName()));
serviceNameMap.get(namespace).add(NamingUtils.getServiceName(service.getName()));
}
}
}

View File

@ -238,18 +238,4 @@ public class UtilsAndCommons {
public static String assembleFullServiceName(String namespaceId, String serviceName) {
return namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + serviceName;
}
public static String getServiceName(String serviceNameWithGroup) {
if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) {
return serviceNameWithGroup;
}
return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[1];
}
public static String getGroupName(String serviceNameWithGroup) {
if (!serviceNameWithGroup.contains(Constants.SERVICE_INFO_SPLITER)) {
return Constants.DEFAULT_GROUP;
}
return serviceNameWithGroup.split(Constants.SERVICE_INFO_SPLITER)[0];
}
}