Adapt create service and delete service in ServiceController
This commit is contained in:
parent
8877bd3460
commit
3026120c2b
@ -110,30 +110,11 @@ public class ServiceController {
|
||||
@RequestParam String serviceName, @RequestParam(required = false) float protectThreshold,
|
||||
@RequestParam(defaultValue = StringUtils.EMPTY) String metadata,
|
||||
@RequestParam(defaultValue = StringUtils.EMPTY) String selector) throws Exception {
|
||||
|
||||
if (serviceManager.getService(namespaceId, serviceName) != null) {
|
||||
throw new IllegalArgumentException("specified service already exists, serviceName : " + serviceName);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
ServiceMetadata serviceMetadata = new ServiceMetadata();
|
||||
serviceMetadata.setProtectThreshold(protectThreshold);
|
||||
serviceMetadata.setSelector(parseSelector(selector));
|
||||
serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(metadata));
|
||||
serviceOperatorV2.create(namespaceId, serviceName, serviceMetadata);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@ -150,8 +131,7 @@ public class ServiceController {
|
||||
public String remove(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
|
||||
@RequestParam String serviceName) throws Exception {
|
||||
|
||||
serviceManager.easyRemoveService(namespaceId, serviceName);
|
||||
|
||||
serviceOperatorV2.delete(namespaceId, serviceName);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
|
@ -431,13 +431,13 @@ public class ServiceManager implements RecordListener<Service> {
|
||||
*
|
||||
* @param namespaceId namespace
|
||||
* @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);
|
||||
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));
|
||||
|
@ -29,6 +29,16 @@ import java.util.List;
|
||||
*/
|
||||
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
|
||||
* metadata of service.
|
||||
@ -39,6 +49,15 @@ public interface ServiceOperator {
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -41,10 +41,31 @@ public class ServiceOperatorV1Impl implements ServiceOperator {
|
||||
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
|
||||
public void update(Service service, ServiceMetadata metadata) throws NacosException {
|
||||
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) {
|
||||
throw new NacosException(NacosException.INVALID_PARAM, "service " + serviceName + " not found!");
|
||||
}
|
||||
@ -57,6 +78,11 @@ public class ServiceOperatorV1Impl implements ServiceOperator {
|
||||
serviceManager.addOrReplaceService(serviceV1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String namespaceId, String serviceName) throws NacosException {
|
||||
serviceManager.easyRemoveService(namespaceId, serviceName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listService(String namespaceId, String groupName, String selector, int pageSize, int pageNo)
|
||||
throws NacosException {
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
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.metadata.NamingMetadataOperateService;
|
||||
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
|
||||
@ -44,6 +45,16 @@ public class ServiceOperatorV2Impl implements ServiceOperator {
|
||||
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
|
||||
public void update(Service service, ServiceMetadata metadata) throws NacosException {
|
||||
if (!ServiceManager.getInstance().containSingleton(service)) {
|
||||
@ -53,6 +64,11 @@ public class ServiceOperatorV2Impl implements ServiceOperator {
|
||||
metadataOperateService.updateServiceMetadata(service, metadata);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String namespaceId, String serviceName) throws NacosException {
|
||||
metadataOperateService.deleteServiceMetadata(getServiceFromGroupedServiceName(namespaceId, serviceName));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
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;
|
||||
}
|
||||
|
||||
private Service getServiceFromGroupedServiceName(String namespaceId, String groupedServiceName) {
|
||||
String groupName = NamingUtils.getGroupName(groupedServiceName);
|
||||
String serviceName = NamingUtils.getServiceName(groupedServiceName);
|
||||
return Service.newService(namespaceId, groupName, serviceName);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import com.alibaba.nacos.consistency.entity.Response;
|
||||
import com.alibaba.nacos.consistency.entity.WriteRequest;
|
||||
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
|
||||
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.utils.Constants;
|
||||
import org.apache.commons.lang3.reflect.TypeUtils;
|
||||
@ -99,13 +100,15 @@ public class ServiceMetadataProcessor extends RequestProcessor4CP {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
private void deleteServiceMetadata(MetadataOperation<ServiceMetadata> op) {
|
||||
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName());
|
||||
namingMetadataManager.removeServiceMetadata(service);
|
||||
ServiceManager.getInstance().removeSingleton(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user