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 Jan 22, 2020
1 parent c507155 commit 9bb794e
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 133 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 @@ -51,12 +55,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 @@ -82,4 +85,84 @@ public void testGetHeartbeatApiPath() {
SentinelConfig.removeConfig(TransportConfig.HEARTBEAT_API_PATH);
assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());
}
}

@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.

Expand Up @@ -26,6 +26,7 @@
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest;
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.util.function.Tuple2;

/**
* The heartbeat sender provides basic API for sending heartbeat request to provided target.
Expand All @@ -43,13 +44,13 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
private final HeartbeatMessage heartBeat = new HeartbeatMessage();
private final SimpleHttpClient httpClient = new SimpleHttpClient();

private final List<InetSocketAddress> addressList;
private final List<Tuple2<String, Integer>> addressList;

private int currentAddressIdx = 0;

public SimpleHttpHeartbeatSender() {
// Retrieve the list of default addresses.
List<InetSocketAddress> newAddrs = getDefaultConsoleIps();
List<Tuple2<String, Integer>> newAddrs = TransportConfig.getConsoleServerList();
RecordLog.info("[SimpleHttpHeartbeatSender] Default console address list retrieved: " + newAddrs);
this.addressList = newAddrs;
}
Expand All @@ -60,11 +61,12 @@ public boolean sendHeartbeat() throws Exception {
RecordLog.info("[SimpleHttpHeartbeatSender] Runtime port not initialized, won't send heartbeat");
return false;
}
InetSocketAddress addr = getAvailableAddress();
if (addr == null) {
Tuple2<String, Integer> addrInfo = getAvailableAddress();
if (addrInfo == null) {
return false;
}

InetSocketAddress addr = new InetSocketAddress(addrInfo.r1, addrInfo.r2);
SimpleHttpRequest request = new SimpleHttpRequest(addr, TransportConfig.getHeartbeatApiPath());
request.setParams(heartBeat.generateCurrentMessage());
try {
Expand All @@ -83,7 +85,7 @@ public long intervalMs() {
return DEFAULT_INTERVAL;
}

private InetSocketAddress getAvailableAddress() {
private Tuple2<String, Integer> getAvailableAddress() {
if (addressList == null || addressList.isEmpty()) {
return null;
}
Expand All @@ -94,33 +96,4 @@ private InetSocketAddress getAvailableAddress() {
return addressList.get(index);
}

private List<InetSocketAddress> getDefaultConsoleIps() {
List<InetSocketAddress> newAddrs = new ArrayList<InetSocketAddress>();
try {
String ipsStr = TransportConfig.getConsoleServer();
if (StringUtil.isEmpty(ipsStr)) {
RecordLog.warn("[SimpleHttpHeartbeatSender] Dashboard server address not configured");
return newAddrs;
}

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

0 comments on commit 9bb794e

Please sign in to comment.