Adapt create service and delete service in ServiceController

This commit is contained in:
KomachiSion 2020-12-08 18:06:36 +08:00
parent 8877bd3460
commit 3026120c2b
6 changed files with 81 additions and 31 deletions

View File

@ -110,30 +110,11 @@ public class ServiceController {
@RequestParam String serviceName, @RequestParam(required = false) float protectThreshold, @RequestParam String serviceName, @RequestParam(required = false) float protectThreshold,
@RequestParam(defaultValue = StringUtils.EMPTY) String metadata, @RequestParam(defaultValue = StringUtils.EMPTY) String metadata,
@RequestParam(defaultValue = StringUtils.EMPTY) String selector) throws Exception { @RequestParam(defaultValue = StringUtils.EMPTY) String selector) throws Exception {
ServiceMetadata serviceMetadata = new ServiceMetadata();
if (serviceManager.getService(namespaceId, serviceName) != null) { serviceMetadata.setProtectThreshold(protectThreshold);
throw new IllegalArgumentException("specified service already exists, serviceName : " + serviceName); serviceMetadata.setSelector(parseSelector(selector));
} serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(metadata));
serviceOperatorV2.create(namespaceId, serviceName, serviceMetadata);
Map<String, String> metadataMap = new HashMap<>(16);
if (StringUtils.isNotBlank(metadata)) {
metadataMap = UtilsAndCommons.parseMetadata(metadata);
}
Service service = new Service(serviceName);
service.setProtectThreshold(protectThreshold);
service.setEnabled(true);
service.setMetadata(metadataMap);
service.setSelector(parseSelector(selector));
service.setNamespaceId(namespaceId);
// now valid the service. if failed, exception will be thrown
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
service.validate();
serviceManager.addOrReplaceService(service);
return "ok"; return "ok";
} }
@ -150,8 +131,7 @@ public class ServiceController {
public String remove(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, public String remove(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
@RequestParam String serviceName) throws Exception { @RequestParam String serviceName) throws Exception {
serviceManager.easyRemoveService(namespaceId, serviceName); serviceOperatorV2.delete(namespaceId, serviceName);
return "ok"; return "ok";
} }

View File

@ -431,13 +431,13 @@ public class ServiceManager implements RecordListener<Service> {
* *
* @param namespaceId namespace * @param namespaceId namespace
* @param serviceName service name * @param serviceName service name
* @throws Exception exception * @throws NacosException exception
*/ */
public void easyRemoveService(String namespaceId, String serviceName) throws Exception { public void easyRemoveService(String namespaceId, String serviceName) throws NacosException {
Service service = getService(namespaceId, serviceName); Service service = getService(namespaceId, serviceName);
if (service == null) { if (service == null) {
throw new IllegalArgumentException("specified service not exist, serviceName : " + serviceName); throw new NacosException(NacosException.INVALID_PARAM, "specified service not exist, serviceName : " + serviceName);
} }
consistencyService.remove(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName)); consistencyService.remove(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName));

View File

@ -29,6 +29,16 @@ import java.util.List;
*/ */
public interface ServiceOperator { public interface ServiceOperator {
/**
* Create new service.
*
* @param namespaceId namespace id of service
* @param serviceName grouped service name format like 'groupName@@serviceName'
* @param metadata new metadata of service
* @throws NacosException nacos exception during creating
*/
void create(String namespaceId, String serviceName, ServiceMetadata metadata) throws NacosException;
/** /**
* Update service information. Due to service basic information can't be changed, so update should only update the * Update service information. Due to service basic information can't be changed, so update should only update the
* metadata of service. * metadata of service.
@ -39,6 +49,15 @@ public interface ServiceOperator {
*/ */
void update(Service service, ServiceMetadata metadata) throws NacosException; void update(Service service, ServiceMetadata metadata) throws NacosException;
/**
* Delete service.
*
* @param namespaceId namespace id of service
* @param serviceName grouped service name format like 'groupName@@serviceName'
* @throws NacosException nacos exception during delete
*/
void delete(String namespaceId, String serviceName) throws NacosException;
/** /**
* Page list service name. * Page list service name.
* *

View File

@ -41,10 +41,31 @@ public class ServiceOperatorV1Impl implements ServiceOperator {
this.serviceManager = serviceManager; this.serviceManager = serviceManager;
} }
@Override
public void create(String namespaceId, String serviceName, ServiceMetadata metadata) throws NacosException {
if (serviceManager.getService(namespaceId, serviceName) != null) {
throw new IllegalArgumentException("specified service already exists, serviceName : " + serviceName);
}
com.alibaba.nacos.naming.core.Service service = new com.alibaba.nacos.naming.core.Service(serviceName);
service.setProtectThreshold(metadata.getProtectThreshold());
service.setEnabled(true);
service.setMetadata(metadata.getExtendData());
service.setSelector(metadata.getSelector());
service.setNamespaceId(namespaceId);
// now valid the service. if failed, exception will be thrown
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
service.validate();
serviceManager.addOrReplaceService(service);
}
@Override @Override
public void update(Service service, ServiceMetadata metadata) throws NacosException { public void update(Service service, ServiceMetadata metadata) throws NacosException {
String serviceName = service.getGroupedServiceName(); String serviceName = service.getGroupedServiceName();
com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager.getService(service.getNamespace(), serviceName); com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager
.getService(service.getNamespace(), serviceName);
if (serviceV1 == null) { if (serviceV1 == null) {
throw new NacosException(NacosException.INVALID_PARAM, "service " + serviceName + " not found!"); throw new NacosException(NacosException.INVALID_PARAM, "service " + serviceName + " not found!");
} }
@ -57,6 +78,11 @@ public class ServiceOperatorV1Impl implements ServiceOperator {
serviceManager.addOrReplaceService(serviceV1); serviceManager.addOrReplaceService(serviceV1);
} }
@Override
public void delete(String namespaceId, String serviceName) throws NacosException {
serviceManager.easyRemoveService(namespaceId, serviceName);
}
@Override @Override
public List<String> listService(String namespaceId, String groupName, String selector, int pageSize, int pageNo) public List<String> listService(String namespaceId, String groupName, String selector, int pageSize, int pageNo)
throws NacosException { throws NacosException {

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.naming.core; package com.alibaba.nacos.naming.core;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService;
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
@ -44,6 +45,16 @@ public class ServiceOperatorV2Impl implements ServiceOperator {
this.metadataOperateService = metadataOperateService; this.metadataOperateService = metadataOperateService;
} }
@Override
public void create(String namespaceId, String serviceName, ServiceMetadata metadata) throws NacosException {
Service service = getServiceFromGroupedServiceName(namespaceId, serviceName);
if (ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.INVALID_PARAM,
String.format("specified service %s already exists!", service.getGroupedServiceName()));
}
metadataOperateService.updateServiceMetadata(service, metadata);
}
@Override @Override
public void update(Service service, ServiceMetadata metadata) throws NacosException { public void update(Service service, ServiceMetadata metadata) throws NacosException {
if (!ServiceManager.getInstance().containSingleton(service)) { if (!ServiceManager.getInstance().containSingleton(service)) {
@ -53,6 +64,11 @@ public class ServiceOperatorV2Impl implements ServiceOperator {
metadataOperateService.updateServiceMetadata(service, metadata); metadataOperateService.updateServiceMetadata(service, metadata);
} }
@Override
public void delete(String namespaceId, String serviceName) throws NacosException {
metadataOperateService.deleteServiceMetadata(getServiceFromGroupedServiceName(namespaceId, serviceName));
}
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<String> listService(String namespaceId, String groupName, String selector, int pageSize, int pageNo) public List<String> listService(String namespaceId, String groupName, String selector, int pageSize, int pageNo)
@ -75,4 +91,10 @@ public class ServiceOperatorV2Impl implements ServiceOperator {
} }
return result; return result;
} }
private Service getServiceFromGroupedServiceName(String namespaceId, String groupedServiceName) {
String groupName = NamingUtils.getGroupName(groupedServiceName);
String serviceName = NamingUtils.getServiceName(groupedServiceName);
return Service.newService(namespaceId, groupName, serviceName);
}
} }

View File

@ -25,6 +25,7 @@ import com.alibaba.nacos.consistency.entity.Response;
import com.alibaba.nacos.consistency.entity.WriteRequest; import com.alibaba.nacos.consistency.entity.WriteRequest;
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation; import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
import com.alibaba.nacos.core.distributed.ProtocolManager; import com.alibaba.nacos.core.distributed.ProtocolManager;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.utils.Constants; import com.alibaba.nacos.naming.utils.Constants;
import org.apache.commons.lang3.reflect.TypeUtils; import org.apache.commons.lang3.reflect.TypeUtils;
@ -99,13 +100,15 @@ public class ServiceMetadataProcessor extends RequestProcessor4CP {
} }
private void updateServiceMetadata(MetadataOperation<ServiceMetadata> op) { private void updateServiceMetadata(MetadataOperation<ServiceMetadata> op) {
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName()); Service service = ServiceManager.getInstance()
.getSingleton(Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName()));
namingMetadataManager.updateServiceMetadata(service, op.getMetadata()); namingMetadataManager.updateServiceMetadata(service, op.getMetadata());
} }
private void deleteServiceMetadata(MetadataOperation<ServiceMetadata> op) { private void deleteServiceMetadata(MetadataOperation<ServiceMetadata> op) {
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName()); Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName());
namingMetadataManager.removeServiceMetadata(service); namingMetadataManager.removeServiceMetadata(service);
ServiceManager.getInstance().removeSingleton(service);
} }
@Override @Override