Search in sources :

Example 1 with MachineInfo

use of com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo in project Sentinel by alibaba.

the class MachineRegistryController method receiveHeartBeat.

@ResponseBody
@RequestMapping("/machine")
public Result<?> receiveHeartBeat(String app, @RequestParam(value = "app_type", required = false, defaultValue = "0") Integer appType, Long version, String v, String hostname, String ip, Integer port) {
    if (StringUtil.isBlank(app) || app.length() > 256) {
        return Result.ofFail(-1, "invalid appName");
    }
    if (StringUtil.isBlank(ip) || ip.length() > 128) {
        return Result.ofFail(-1, "invalid ip: " + ip);
    }
    if (!IPAddressUtil.isIPv4LiteralAddress(ip) && !IPAddressUtil.isIPv6LiteralAddress(ip)) {
        return Result.ofFail(-1, "invalid ip: " + ip);
    }
    if (port == null || port < -1) {
        return Result.ofFail(-1, "invalid port");
    }
    if (hostname != null && hostname.length() > 256) {
        return Result.ofFail(-1, "hostname too long");
    }
    if (port == -1) {
        logger.warn("Receive heartbeat from " + ip + " but port not set yet");
        return Result.ofFail(-1, "your port not set yet");
    }
    String sentinelVersion = StringUtil.isBlank(v) ? "unknown" : v;
    version = version == null ? System.currentTimeMillis() : version;
    try {
        MachineInfo machineInfo = new MachineInfo();
        machineInfo.setApp(app);
        machineInfo.setAppType(appType);
        machineInfo.setHostname(hostname);
        machineInfo.setIp(ip);
        machineInfo.setPort(port);
        machineInfo.setHeartbeatVersion(version);
        machineInfo.setLastHeartbeat(System.currentTimeMillis());
        machineInfo.setVersion(sentinelVersion);
        appManagement.addMachine(machineInfo);
        return Result.ofSuccessMsg("success");
    } catch (Exception e) {
        logger.error("Receive heartbeat error", e);
        return Result.ofFail(-1, e.getMessage());
    }
}
Also used : MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with MachineInfo

use of com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo in project Sentinel by alibaba.

the class AppController method getMachinesByApp.

@GetMapping(value = "/{app}/machines.json")
public Result<List<MachineInfoVo>> getMachinesByApp(@PathVariable("app") String app) {
    AppInfo appInfo = appManagement.getDetailApp(app);
    if (appInfo == null) {
        return Result.ofSuccess(null);
    }
    List<MachineInfo> list = new ArrayList<>(appInfo.getMachines());
    Collections.sort(list, Comparator.comparing(MachineInfo::getApp).thenComparing(MachineInfo::getIp).thenComparingInt(MachineInfo::getPort));
    return Result.ofSuccess(MachineInfoVo.fromMachineInfoList(list));
}
Also used : MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) ArrayList(java.util.ArrayList) AppInfo(com.alibaba.csp.sentinel.dashboard.discovery.AppInfo) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with MachineInfo

use of com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo in project pig by pig-mesh.

the class AppController method getMachinesByApp.

@GetMapping(value = "/{app}/machines.json")
public Result<List<MachineInfoVo>> getMachinesByApp(@PathVariable("app") String app) {
    AppInfo appInfo = appManagement.getDetailApp(app);
    if (appInfo == null) {
        return Result.ofSuccess(null);
    }
    List<MachineInfo> list = new ArrayList<>(appInfo.getMachines());
    Collections.sort(list, Comparator.comparing(MachineInfo::getApp).thenComparing(MachineInfo::getIp).thenComparingInt(MachineInfo::getPort));
    return Result.ofSuccess(MachineInfoVo.fromMachineInfoList(list));
}
Also used : MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) ArrayList(java.util.ArrayList) AppInfo(com.alibaba.csp.sentinel.dashboard.discovery.AppInfo) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with MachineInfo

use of com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo in project spring-boot-student by wyh-spring-ecosystem-student.

the class MachineRegistryController method receiveHeartBeat.

@ResponseBody
@RequestMapping("/machine")
public Result<?> receiveHeartBeat(String app, @RequestParam(value = "app_type", required = false, defaultValue = "0") Integer appType, Long version, String v, String hostname, String ip, Integer port) {
    if (app == null) {
        app = MachineDiscovery.UNKNOWN_APP_NAME;
    }
    if (ip == null) {
        return Result.ofFail(-1, "ip can't be null");
    }
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (port == -1) {
        logger.info("Receive heartbeat from " + ip + " but port not set yet");
        return Result.ofFail(-1, "your port not set yet");
    }
    String sentinelVersion = StringUtil.isEmpty(v) ? "unknown" : v;
    version = version == null ? System.currentTimeMillis() : version;
    try {
        MachineInfo machineInfo = new MachineInfo();
        machineInfo.setApp(app);
        machineInfo.setAppType(appType);
        machineInfo.setHostname(hostname);
        machineInfo.setIp(ip);
        machineInfo.setPort(port);
        machineInfo.setHeartbeatVersion(version);
        machineInfo.setLastHeartbeat(System.currentTimeMillis());
        machineInfo.setVersion(sentinelVersion);
        appManagement.addMachine(machineInfo);
        return Result.ofSuccessMsg("success");
    } catch (Exception e) {
        logger.error("Receive heartbeat error", e);
        return Result.ofFail(-1, e.getMessage());
    }
}
Also used : MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with MachineInfo

use of com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo in project spring-boot-student by wyh-spring-ecosystem-student.

the class MachineEntity method toMachineInfo.

public MachineInfo toMachineInfo() {
    MachineInfo machineInfo = new MachineInfo();
    machineInfo.setApp(app);
    machineInfo.setHostname(hostname);
    machineInfo.setIp(ip);
    machineInfo.setPort(port);
    machineInfo.setLastHeartbeat(timestamp.getTime());
    machineInfo.setHeartbeatVersion(timestamp.getTime());
    return machineInfo;
}
Also used : MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)

Aggregations

MachineInfo (com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)20 AppInfo (com.alibaba.csp.sentinel.dashboard.discovery.AppInfo)10 MetricEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity)5 ConnectException (java.net.ConnectException)5 SocketTimeoutException (java.net.SocketTimeoutException)5 ArrayList (java.util.ArrayList)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 HttpResponse (org.apache.http.HttpResponse)5 HttpGet (org.apache.http.client.methods.HttpGet)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5