Search in sources :

Example 11 with CanDistro

use of com.alibaba.nacos.naming.web.CanDistro in project nacos by alibaba.

the class UpgradeOpsController method registerInstance.

/**
 * Register new instance.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during register
 */
@CanDistro
@PostMapping("/instance")
@Secured(action = ActionTypes.WRITE)
public String registerInstance(@RequestParam(defaultValue = "v2", required = false) String ver, HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    final Instance instance = HttpRequestInstanceBuilder.newBuilder().setDefaultInstanceEphemeral(switchDomain.isDefaultInstanceEphemeral()).setRequest(request).build();
    getInstanceOperator(ver).registerInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 12 with CanDistro

use of com.alibaba.nacos.naming.web.CanDistro in project nacos by alibaba.

the class InstanceController method deregister.

/**
 * Deregister instances.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during deregister
 */
@CanDistro
@DeleteMapping
@Secured(action = ActionTypes.WRITE)
public String deregister(HttpServletRequest request) throws Exception {
    Instance instance = HttpRequestInstanceBuilder.newBuilder().setDefaultInstanceEphemeral(switchDomain.isDefaultInstanceEphemeral()).setRequest(request).build();
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    getInstanceOperator().removeInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 13 with CanDistro

use of com.alibaba.nacos.naming.web.CanDistro in project nacos by alibaba.

the class InstanceController method batchUpdateInstanceMetadata.

/**
 * Batch update instance's metadata. old key exist = update, old key not exist = add.
 *
 * @param request http request
 * @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
 * @throws Exception any error during update
 * @since 1.4.0
 */
@CanDistro
@PutMapping(value = "/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchUpdateInstanceMetadata(HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    String consistencyType = WebUtils.optional(request, "consistencyType", StringUtils.EMPTY);
    String instances = WebUtils.optional(request, "instances", StringUtils.EMPTY);
    List<Instance> targetInstances = parseBatchInstances(instances);
    String metadata = WebUtils.required(request, METADATA);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = getInstanceOperator().batchUpdateMetadata(namespaceId, instanceOperationInfo, targetMetadata);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
    for (String ip : operatedInstances) {
        ipArray.add(ip);
    }
    result.replace("updated", ipArray);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 14 with CanDistro

use of com.alibaba.nacos.naming.web.CanDistro in project nacos by alibaba.

the class InstanceController method batchDeleteInstanceMetadata.

/**
 * Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
 *
 * @param request http request
 * @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
 * @throws Exception any error during update
 * @since 1.4.0
 */
@CanDistro
@DeleteMapping("/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchDeleteInstanceMetadata(HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    String consistencyType = WebUtils.optional(request, "consistencyType", StringUtils.EMPTY);
    String instances = WebUtils.optional(request, "instances", StringUtils.EMPTY);
    List<Instance> targetInstances = parseBatchInstances(instances);
    String metadata = WebUtils.required(request, METADATA);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = getInstanceOperator().batchDeleteMetadata(namespaceId, instanceOperationInfo, targetMetadata);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
    for (String ip : operatedInstances) {
        ipArray.add(ip);
    }
    result.replace("updated", ipArray);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 15 with CanDistro

use of com.alibaba.nacos.naming.web.CanDistro in project nacos by alibaba.

the class InstanceController method beat.

/**
 * Create a beat for instance.
 *
 * @param request http request
 * @return detail information of instance
 * @throws Exception any error during handle
 */
@CanDistro
@PutMapping("/beat")
@Secured(action = ActionTypes.WRITE)
public ObjectNode beat(HttpServletRequest request) throws Exception {
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
    String beat = WebUtils.optional(request, "beat", StringUtils.EMPTY);
    RsInfo clientBeat = null;
    if (StringUtils.isNotBlank(beat)) {
        clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
    }
    String clusterName = WebUtils.optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME);
    String ip = WebUtils.optional(request, "ip", StringUtils.EMPTY);
    int port = Integer.parseInt(WebUtils.optional(request, "port", "0"));
    if (clientBeat != null) {
        if (StringUtils.isNotBlank(clientBeat.getCluster())) {
            clusterName = clientBeat.getCluster();
        } else {
            // fix #2533
            clientBeat.setCluster(clusterName);
        }
        ip = clientBeat.getIp();
        port = clientBeat.getPort();
    }
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat, serviceName, namespaceId);
    BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
    builder.setRequest(request);
    int resultCode = getInstanceOperator().handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
    result.put(CommonParams.CODE, resultCode);
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, getInstanceOperator().getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
    result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
    return result;
}
Also used : BeatInfoInstanceBuilder(com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Aggregations

Secured (com.alibaba.nacos.auth.annotation.Secured)17 CanDistro (com.alibaba.nacos.naming.web.CanDistro)17 Instance (com.alibaba.nacos.api.naming.pojo.Instance)13 PutMapping (org.springframework.web.bind.annotation.PutMapping)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)5 InstanceOperationInfo (com.alibaba.nacos.naming.pojo.InstanceOperationInfo)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 InstancePatchObject (com.alibaba.nacos.naming.core.InstancePatchObject)2 RsInfo (com.alibaba.nacos.naming.healthcheck.RsInfo)2 BeatInfoInstanceBuilder (com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder)2 PatchMapping (org.springframework.web.bind.annotation.PatchMapping)2