Adapt update service metadata api. (#4260)
* Adapt update service metadata api. * For checkstyle
This commit is contained in:
parent
69c84f1397
commit
d4b2d2f146
@ -38,11 +38,7 @@ server.servlet.contextPath=/nacos
|
|||||||
# db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
|
# db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
|
||||||
# db.user=nacos
|
# db.user=nacos
|
||||||
# db.password=nacos
|
# db.password=nacos
|
||||||
spring.datasource.platform=mysql
|
|
||||||
db.num=1
|
|
||||||
db.url.0=jdbc:mysql://10.101.167.27:3306/acm?characterEncoding=utf8&connectTimeout=1000&socketTimeout=10000&autoReconnect=true
|
|
||||||
db.user=root
|
|
||||||
db.password=root
|
|
||||||
#*************** Naming Module Related Configurations ***************#
|
#*************** Naming Module Related Configurations ***************#
|
||||||
### Data dispatch task execution period in milliseconds:
|
### Data dispatch task execution period in milliseconds:
|
||||||
# nacos.naming.distro.taskDispatchPeriod=200
|
# nacos.naming.distro.taskDispatchPeriod=200
|
||||||
|
@ -32,7 +32,10 @@ import com.alibaba.nacos.naming.core.DistroMapper;
|
|||||||
import com.alibaba.nacos.naming.core.Instance;
|
import com.alibaba.nacos.naming.core.Instance;
|
||||||
import com.alibaba.nacos.naming.core.Service;
|
import com.alibaba.nacos.naming.core.Service;
|
||||||
import com.alibaba.nacos.naming.core.ServiceManager;
|
import com.alibaba.nacos.naming.core.ServiceManager;
|
||||||
|
import com.alibaba.nacos.naming.core.ServiceOperatorV1Impl;
|
||||||
|
import com.alibaba.nacos.naming.core.ServiceOperatorV2Impl;
|
||||||
import com.alibaba.nacos.naming.core.SubscribeManager;
|
import com.alibaba.nacos.naming.core.SubscribeManager;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
|
||||||
import com.alibaba.nacos.naming.misc.Loggers;
|
import com.alibaba.nacos.naming.misc.Loggers;
|
||||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||||
import com.alibaba.nacos.naming.pojo.Subscriber;
|
import com.alibaba.nacos.naming.pojo.Subscriber;
|
||||||
@ -44,7 +47,6 @@ import com.alibaba.nacos.naming.web.NamingResourceParser;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.math.NumberUtils;
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -87,6 +89,12 @@ public class ServiceController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SubscribeManager subscribeManager;
|
private SubscribeManager subscribeManager;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ServiceOperatorV1Impl serviceOperatorV1;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ServiceOperatorV2Impl serviceOperatorV2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new service.
|
* Create a new service.
|
||||||
*
|
*
|
||||||
@ -241,28 +249,17 @@ public class ServiceController {
|
|||||||
@PutMapping
|
@PutMapping
|
||||||
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
|
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
|
||||||
public String update(HttpServletRequest request) throws Exception {
|
public String update(HttpServletRequest request) throws Exception {
|
||||||
|
|
||||||
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
|
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
|
||||||
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
|
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
|
||||||
float protectThreshold = NumberUtils.toFloat(WebUtils.required(request, "protectThreshold"));
|
ServiceMetadata serviceMetadata = new ServiceMetadata();
|
||||||
String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
|
serviceMetadata.setProtectThreshold(NumberUtils.toFloat(WebUtils.required(request, "protectThreshold")));
|
||||||
|
serviceMetadata.setExtendData(
|
||||||
Service service = serviceManager.getService(namespaceId, serviceName);
|
UtilsAndCommons.parseMetadata(WebUtils.optional(request, "metadata", StringUtils.EMPTY)));
|
||||||
if (service == null) {
|
serviceMetadata.setSelector(parseSelector(WebUtils.optional(request, "selector", StringUtils.EMPTY)));
|
||||||
throw new NacosException(NacosException.INVALID_PARAM, "service " + serviceName + " not found!");
|
com.alibaba.nacos.naming.core.v2.pojo.Service service = com.alibaba.nacos.naming.core.v2.pojo.Service
|
||||||
}
|
.newService(namespaceId, NamingUtils.getGroupName(serviceName),
|
||||||
|
NamingUtils.getServiceName(serviceName));
|
||||||
service.setProtectThreshold(protectThreshold);
|
serviceOperatorV2.update(service, serviceMetadata);
|
||||||
|
|
||||||
Map<String, String> metadataMap = UtilsAndCommons.parseMetadata(metadata);
|
|
||||||
service.setMetadata(metadataMap);
|
|
||||||
service.setSelector(parseSelector(WebUtils.optional(request, "selector", StringUtils.EMPTY)));
|
|
||||||
service.setLastModifiedMillis(System.currentTimeMillis());
|
|
||||||
service.recalculateChecksum();
|
|
||||||
service.validate();
|
|
||||||
|
|
||||||
serviceManager.addOrReplaceService(service);
|
|
||||||
|
|
||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 1999-2020 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.core;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service operator.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
public interface ServiceOperator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update service information. Due to service basic information can't be changed, so update should only update the
|
||||||
|
* metadata of service.
|
||||||
|
*
|
||||||
|
* @param service service need to be updated.
|
||||||
|
* @param metadata new metadata of service.
|
||||||
|
* @throws NacosException nacos exception during update
|
||||||
|
*/
|
||||||
|
void update(Service service, ServiceMetadata metadata) throws NacosException;
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 1999-2020 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.core;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of service operator for v1.x.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ServiceOperatorV1Impl implements ServiceOperator {
|
||||||
|
|
||||||
|
private final ServiceManager serviceManager;
|
||||||
|
|
||||||
|
public ServiceOperatorV1Impl(ServiceManager serviceManager) {
|
||||||
|
this.serviceManager = serviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
if (serviceV1 == null) {
|
||||||
|
throw new NacosException(NacosException.INVALID_PARAM, "service " + serviceName + " not found!");
|
||||||
|
}
|
||||||
|
serviceV1.setProtectThreshold(metadata.getProtectThreshold());
|
||||||
|
serviceV1.setSelector(metadata.getSelector());
|
||||||
|
serviceV1.setMetadata(metadata.getExtendData());
|
||||||
|
serviceV1.setLastModifiedMillis(System.currentTimeMillis());
|
||||||
|
serviceV1.recalculateChecksum();
|
||||||
|
serviceV1.validate();
|
||||||
|
serviceManager.addOrReplaceService(serviceV1);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 1999-2020 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.core;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
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;
|
||||||
|
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of service operator for v2.x.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ServiceOperatorV2Impl implements ServiceOperator {
|
||||||
|
|
||||||
|
private final NamingMetadataOperateService metadataOperateService;
|
||||||
|
|
||||||
|
public ServiceOperatorV2Impl(NamingMetadataOperateService metadataOperateService) {
|
||||||
|
this.metadataOperateService = metadataOperateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Service service, ServiceMetadata metadata) throws NacosException {
|
||||||
|
if (!ServiceManager.getInstance().containSingleton(service)) {
|
||||||
|
throw new NacosException(NacosException.INVALID_PARAM,
|
||||||
|
String.format("service %s not found!", service.getGroupedServiceName()));
|
||||||
|
}
|
||||||
|
metadataOperateService.updateServiceMetadata(service, metadata);
|
||||||
|
}
|
||||||
|
}
|
@ -57,9 +57,9 @@ public class NamingMetadataOperateService {
|
|||||||
MetadataOperation<ServiceMetadata> operation = new MetadataOperation<>();
|
MetadataOperation<ServiceMetadata> operation = new MetadataOperation<>();
|
||||||
operation.setNamespace(service.getNamespace());
|
operation.setNamespace(service.getNamespace());
|
||||||
operation.setGroup(service.getGroup());
|
operation.setGroup(service.getGroup());
|
||||||
operation.setServiceName(service.getGroupedServiceName());
|
operation.setServiceName(service.getName());
|
||||||
operation.setMetadata(serviceMetadata);
|
operation.setMetadata(serviceMetadata);
|
||||||
Log operationLog = Log.newBuilder().setGroup(Constants.SERVICE_METADATA).setType(DataOperation.CHANGE.name())
|
Log operationLog = Log.newBuilder().setGroup(Constants.SERVICE_METADATA).setOperation(DataOperation.CHANGE.name())
|
||||||
.setData(ByteString.copyFrom(serializer.serialize(operation))).build();
|
.setData(ByteString.copyFrom(serializer.serialize(operation))).build();
|
||||||
submitMetadataOperation(operationLog);
|
submitMetadataOperation(operationLog);
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ public class NamingMetadataOperateService {
|
|||||||
operation.setNamespace(service.getNamespace());
|
operation.setNamespace(service.getNamespace());
|
||||||
operation.setGroup(service.getGroup());
|
operation.setGroup(service.getGroup());
|
||||||
operation.setServiceName(service.getGroupedServiceName());
|
operation.setServiceName(service.getGroupedServiceName());
|
||||||
Log operationLog = Log.newBuilder().setGroup(Constants.SERVICE_METADATA).setType(DataOperation.DELETE.name())
|
Log operationLog = Log.newBuilder().setGroup(Constants.SERVICE_METADATA).setOperation(DataOperation.DELETE.name())
|
||||||
.setData(ByteString.copyFrom(serializer.serialize(operation))).build();
|
.setData(ByteString.copyFrom(serializer.serialize(operation))).build();
|
||||||
submitMetadataOperation(operationLog);
|
submitMetadataOperation(operationLog);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,6 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service metadata processor.
|
* Service metadata processor.
|
||||||
@ -81,13 +80,8 @@ public class ServiceMetadataProcessor extends LogProcessor4CP {
|
|||||||
private void updateServiceMetadata(ByteString data) {
|
private void updateServiceMetadata(ByteString data) {
|
||||||
MetadataOperation<ServiceMetadata> op = serializer.deserialize(data.toByteArray(), processType);
|
MetadataOperation<ServiceMetadata> op = serializer.deserialize(data.toByteArray(), processType);
|
||||||
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName());
|
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName());
|
||||||
Optional<ServiceMetadata> serviceMetadata = namingMetadataManager.getServiceMetadata(service);
|
|
||||||
if (serviceMetadata.isPresent()) {
|
|
||||||
namingMetadataManager.updateServiceMetadata(service, serviceMetadata.get());
|
|
||||||
} else {
|
|
||||||
namingMetadataManager.updateServiceMetadata(service, op.getMetadata());
|
namingMetadataManager.updateServiceMetadata(service, op.getMetadata());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteServiceMetadata(ByteString data) {
|
private void deleteServiceMetadata(ByteString data) {
|
||||||
MetadataOperation<ServiceMetadata> op = serializer.deserialize(data.toByteArray(), processType);
|
MetadataOperation<ServiceMetadata> op = serializer.deserialize(data.toByteArray(), processType);
|
||||||
|
Loading…
Reference in New Issue
Block a user