Skip to content

Commit

Permalink
Migrate the console server's specifying logic into sentinel-transport…
Browse files Browse the repository at this point in the history
…-common
  • Loading branch information
jasonjoo2010 committed Jul 17, 2019
1 parent 7dd20dd commit eb9b861
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 135 deletions.
Expand Up @@ -15,10 +15,14 @@
*/
package com.alibaba.csp.sentinel.transport.config;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.HostNameUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.util.function.Tuple2;

/**
* @author leyou
Expand Down Expand Up @@ -48,12 +52,69 @@ public static Long getHeartbeatIntervalMs() {
}

/**
* Get ip:port of Sentinel Dashboard.
* Get a list of (ip/domain, port) indicating Sentinel Dashboard's address.<br>
* NOTE: only support <b>HTTP</b> protocol
*
* @return console server ip:port, maybe null if not configured
* @return list of (ip/domain, port) pair. <br>
* <b>May not be null</b>. <br>
* An empty list returned when not configured.
*/
public static String getConsoleServer() {
return SentinelConfig.getConfig(CONSOLE_SERVER);
public static List<Tuple2<String, Integer>> getConsoleServerList() {
String config = SentinelConfig.getConfig(CONSOLE_SERVER);
List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>();
if (StringUtil.isBlank(config)) {
RecordLog.warn("Dashboard server address is not configured");
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();
if (ipPortStr.startsWith("http://")) {
ipPortStr = ipPortStr.substring(7);
}
int index = ipPortStr.indexOf(":");
int port = 80;
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(Tuple2.of(host, port));
}
return list;
}

public static int getRuntimePort() {
Expand Down
Expand Up @@ -17,13 +17,16 @@

import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.util.function.Tuple2;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

import java.util.List;

public class TransportConfigTest {

@Before
Expand Down Expand Up @@ -64,4 +67,84 @@ public void testGetHeartbeatClientIp() {
SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, "");
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatClientIp()));
}

@Test
public void testGetConsoleServerList() {
// empty
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "");
List<Tuple2<String, Integer>> list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

// single ip
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "112.13.223.3");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(1, list.size());
assertEquals("112.13.223.3", list.get(0).r1);
assertEquals(new Integer(80), list.get(0).r2);

// single domain
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(1, list.size());
assertEquals("www.dashboard.org", list.get(0).r1);
assertEquals(new Integer(80), list.get(0).r2);

// single ip including port
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(1, list.size());
assertEquals("www.dashboard.org", list.get(0).r1);
assertEquals(new Integer(81), list.get(0).r2);

// mixed
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81,112.13.223.3,112.13.223.4:8080,www.dashboard.org");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(4, list.size());
assertEquals("www.dashboard.org", list.get(0).r1);
assertEquals(new Integer(81), list.get(0).r2);
assertEquals("112.13.223.3", list.get(1).r1);
assertEquals(new Integer(80), list.get(1).r2);
assertEquals("112.13.223.4", list.get(2).r1);
assertEquals(new Integer(8080), list.get(2).r2);
assertEquals("www.dashboard.org", list.get(3).r1);
assertEquals(new Integer(80), list.get(3).r2);

// malformed
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:0");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:-1");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, ":80");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:80000");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(0, list.size());

SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:80000,www.dashboard.org:81,:80");
list = TransportConfig.getConsoleServerList();
assertNotNull(list);
assertEquals(1, list.size());
assertEquals("www.dashboard.org", list.get(0).r1);
assertEquals(new Integer(81), list.get(0).r2);
}
}
Expand Up @@ -15,7 +15,6 @@
*/
package com.alibaba.csp.sentinel.transport.heartbeat;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.csp.sentinel.Constants;
Expand Down Expand Up @@ -58,7 +57,7 @@ public class HttpHeartbeatSender implements HeartbeatSender {

public HttpHeartbeatSender() {
this.client = HttpClients.createDefault();
List<Tuple2<String, Integer>> dashboardList = parseDashboardList();
List<Tuple2<String, Integer>> dashboardList = TransportConfig.getConsoleServerList();
if (dashboardList == null || dashboardList.isEmpty()) {
RecordLog.info("[NettyHttpHeartbeatSender] No dashboard available");
} else {
Expand All @@ -68,40 +67,6 @@ public HttpHeartbeatSender() {
}
}

protected static List<Tuple2<String, Integer>> parseDashboardList() {
List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>();
try {
String ipsStr = TransportConfig.getConsoleServer();
if (StringUtil.isBlank(ipsStr)) {
RecordLog.warn("[NettyHttpHeartbeatSender] Dashboard server address is not configured");
return list;
}

for (String ipPortStr : ipsStr.split(",")) {
if (ipPortStr.trim().length() == 0) {
continue;
}
ipPortStr = ipPortStr.trim();
if (ipPortStr.startsWith("http://")) {
ipPortStr = ipPortStr.substring(7);
}
if (ipPortStr.startsWith(":")) {
continue;
}
String[] ipPort = ipPortStr.trim().split(":");
int port = 80;
if (ipPort.length > 1) {
port = Integer.parseInt(ipPort[1].trim());
}
list.add(Tuple2.of(ipPort[0].trim(), port));
}
} catch (Exception ex) {
RecordLog.warn("[NettyHttpHeartbeatSender] Parse dashboard list failed, current address list: " + list, ex);
ex.printStackTrace();
}
return list;
}

@Override
public boolean sendHeartbeat() throws Exception {
if (StringUtil.isEmpty(consoleHost)) {
Expand Down

This file was deleted.

0 comments on commit eb9b861

Please sign in to comment.