Search in sources :

Example 1 with Protocol

use of com.alibaba.csp.sentinel.transport.endpoint.Protocol in project Sentinel by alibaba.

the class TransportConfig method getConsoleServerList.

/**
 * Get a list of Endpoint(protocol, ip/domain, port) indicating Sentinel Dashboard's address.<br>
 * NOTE: only support <b>HTTP</b> and <b>HTTPS</b> protocol
 *
 * @return list of Endpoint(protocol, ip/domain, port). <br>
 *         <b>May not be null</b>. <br>
 *         An empty list returned when not configured.
 */
public static List<Endpoint> getConsoleServerList() {
    String config = SentinelConfig.getConfig(CONSOLE_SERVER);
    List<Endpoint> list = new ArrayList<Endpoint>();
    if (StringUtil.isBlank(config)) {
        return list;
    }
    int pos = -1;
    int cur = 0;
    while (true) {
        pos = config.indexOf(',', cur);
        if (cur < config.length() - 1 && pos < 0) {
            // for single segment, pos move to the end
            pos = config.length();
        }
        if (pos < 0) {
            break;
        }
        if (pos <= cur) {
            cur++;
            continue;
        }
        // parsing
        String ipPortStr = config.substring(cur, pos);
        cur = pos + 1;
        if (StringUtil.isBlank(ipPortStr)) {
            continue;
        }
        ipPortStr = ipPortStr.trim();
        int port = 80;
        Protocol protocol = Protocol.HTTP;
        if (ipPortStr.startsWith("http://")) {
            ipPortStr = ipPortStr.substring(7);
        } else if (ipPortStr.startsWith("https://")) {
            ipPortStr = ipPortStr.substring(8);
            port = 443;
            protocol = Protocol.HTTPS;
        }
        int index = ipPortStr.indexOf(":");
        if (index == 0) {
            // skip
            continue;
        }
        String host = ipPortStr;
        if (index >= 0) {
            try {
                port = Integer.parseInt(ipPortStr.substring(index + 1));
                if (port <= 1 || port >= 65535) {
                    throw new RuntimeException("Port number [" + port + "] over range");
                }
            } catch (Exception e) {
                RecordLog.warn("Parse port of dashboard server failed: " + ipPortStr, e);
                // skip
                continue;
            }
            host = ipPortStr.substring(0, index);
        }
        list.add(new Endpoint(protocol, host, port));
    }
    return list;
}
Also used : Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) ArrayList(java.util.ArrayList) Protocol(com.alibaba.csp.sentinel.transport.endpoint.Protocol) Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint)

Aggregations

Endpoint (com.alibaba.csp.sentinel.transport.endpoint.Endpoint)1 Protocol (com.alibaba.csp.sentinel.transport.endpoint.Protocol)1 ArrayList (java.util.ArrayList)1