Search in sources :

Example 1 with CanDistro

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

the class InstanceController method patch.

/**
 * Patch instance.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during patch
 */
@CanDistro
@PatchMapping
@Secured(action = ActionTypes.WRITE)
public String patch(HttpServletRequest request) throws Exception {
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    String ip = WebUtils.required(request, "ip");
    String port = WebUtils.required(request, "port");
    String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, StringUtils.EMPTY);
    if (StringUtils.isBlank(cluster)) {
        cluster = WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
    }
    InstancePatchObject patchObject = new InstancePatchObject(cluster, ip, Integer.parseInt(port));
    String metadata = WebUtils.optional(request, METADATA, StringUtils.EMPTY);
    if (StringUtils.isNotBlank(metadata)) {
        patchObject.setMetadata(UtilsAndCommons.parseMetadata(metadata));
    }
    String app = WebUtils.optional(request, "app", StringUtils.EMPTY);
    if (StringUtils.isNotBlank(app)) {
        patchObject.setApp(app);
    }
    String weight = WebUtils.optional(request, "weight", StringUtils.EMPTY);
    if (StringUtils.isNotBlank(weight)) {
        patchObject.setWeight(Double.parseDouble(weight));
    }
    String healthy = WebUtils.optional(request, "healthy", StringUtils.EMPTY);
    if (StringUtils.isNotBlank(healthy)) {
        patchObject.setHealthy(ConvertUtils.toBoolean(healthy));
    }
    String enabledString = WebUtils.optional(request, "enabled", StringUtils.EMPTY);
    if (StringUtils.isNotBlank(enabledString)) {
        patchObject.setEnabled(ConvertUtils.toBoolean(enabledString));
    }
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    getInstanceOperator().patchInstance(namespaceId, serviceName, patchObject);
    return "ok";
}
Also used : InstancePatchObject(com.alibaba.nacos.naming.core.InstancePatchObject) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 2 with CanDistro

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

the class InstanceController method register.

/**
 * Register new instance.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during register
 */
@CanDistro
@PostMapping
@Secured(action = ActionTypes.WRITE)
public String register(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().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 3 with CanDistro

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

the class InstanceController method update.

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

Example 4 with CanDistro

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

the class InstanceControllerV2 method batchDeleteInstanceMetadata.

/**
 * Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
 *
 * @param namespaceId     namespace id
 * @param serviceName     service name
 * @param metadata        service metadata
 * @param consistencyType consistencyType
 * @param instances       instances info
 * @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(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = "") String consistencyType, @RequestParam(defaultValue = "") String instances, @RequestParam String metadata) throws Exception {
    List<Instance> targetInstances = parseBatchInstances(instances);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = instanceServiceV2.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 5 with CanDistro

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

the class InstanceControllerV2 method beat.

/**
 * Create a beat for instance.
 *
 * @param namespaceId service namespaceId
 * @param serviceName service serviceName
 * @param ip          instance ip
 * @param clusterName service clusterName
 * @param port        instance port
 * @param beat        instance beat info
 * @return detail information of instance
 * @throws Exception any error during handle
 */
@CanDistro
@PutMapping("/beat")
@Secured(action = ActionTypes.WRITE)
public ObjectNode beat(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = StringUtils.EMPTY) String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName, @RequestParam(defaultValue = "0") Integer port, @RequestParam(defaultValue = StringUtils.EMPTY) String beat) throws Exception {
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
    RsInfo clientBeat = null;
    if (StringUtils.isNotBlank(beat)) {
        clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
    }
    if (clientBeat != null) {
        if (StringUtils.isNotBlank(clientBeat.getCluster())) {
            clusterName = clientBeat.getCluster();
        } else {
            // fix #2533
            clientBeat.setCluster(clusterName);
        }
        ip = clientBeat.getIp();
        port = clientBeat.getPort();
    }
    NamingUtils.checkServiceNameFormat(serviceName);
    Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat, serviceName, namespaceId);
    BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
    int resultCode = instanceServiceV2.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
    result.put(CommonParams.CODE, resultCode);
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instanceServiceV2.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