Simple HTTP transport module for Sentinel providing basic HTTP communication capabilities for heartbeat and command transport
—
The HTTP client system provides lightweight, blocking HTTP communication capabilities designed specifically for dashboard communication. It supports GET and POST requests with form-encoded parameters and includes comprehensive response parsing.
Main HTTP client implementation that provides blocking, synchronous HTTP requests.
/**
* A very simple HTTP client that only supports GET/POST method and plain text request body.
* The Content-Type header is always set as application/x-www-form-urlencoded.
* All parameters in the request will be encoded using URLEncoder.encode(String, String).
*
* The result of a HTTP invocation will be wrapped as a SimpleHttpResponse. Content in response body
* will be automatically decoded to string with provided charset.
*
* This is a blocking and synchronous client, so an invocation will await the response until timeout exceed.
*
* Note that this is a very NAIVE client, Content-Length must be specified in the
* HTTP response header, otherwise, the response body will be dropped. All other body type such as
* Transfer-Encoding: chunked, Transfer-Encoding: deflate are not supported.
*/
public class SimpleHttpClient {
/**
* Execute a GET HTTP request.
* @param request HTTP request configuration
* @return the response if the request is successful
* @throws IOException when connection cannot be established or the connection is interrupted
*/
public SimpleHttpResponse get(SimpleHttpRequest request) throws IOException;
/**
* Execute a POST HTTP request.
* @param request HTTP request configuration
* @return the response if the request is successful
* @throws IOException when connection cannot be established or the connection is interrupted
*/
public SimpleHttpResponse post(SimpleHttpRequest request) throws IOException;
}Usage Examples:
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpClient;
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest;
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse;
import com.alibaba.csp.sentinel.transport.endpoint.Endpoint;
import com.alibaba.csp.sentinel.transport.endpoint.Protocol;
// Create HTTP client
SimpleHttpClient client = new SimpleHttpClient();
// Create endpoint for dashboard server
Endpoint endpoint = new Endpoint(Protocol.HTTP, "dashboard.example.com", 8080);
// GET request example
SimpleHttpRequest getRequest = new SimpleHttpRequest(endpoint, "/api/status");
getRequest.addParam("app", "my-application");
getRequest.addParam("type", "health");
try {
SimpleHttpResponse response = client.get(getRequest);
System.out.println("Status: " + response.getStatusCode());
System.out.println("Body: " + response.getBodyAsString());
} catch (IOException e) {
System.err.println("Request failed: " + e.getMessage());
}
// POST request example
SimpleHttpRequest postRequest = new SimpleHttpRequest(endpoint, "/api/heartbeat");
postRequest.addParam("hostname", "web-server-01");
postRequest.addParam("ip", "192.168.1.100");
postRequest.addParam("port", "8719");
postRequest.setSoTimeout(5000); // 5 second timeout
SimpleHttpResponse postResponse = client.post(postRequest);
if (postResponse.getStatusCode() == 200) {
System.out.println("Heartbeat successful");
}HTTP request configuration with builder-style methods for setting parameters and options.
/**
* Simple HTTP request representation.
*/
public class SimpleHttpRequest {
/**
* Constructor with endpoint and request path
* @param endpoint target server endpoint
* @param requestPath path component of the URL
*/
public SimpleHttpRequest(Endpoint endpoint, String requestPath);
/**
* Get the target endpoint
* @return endpoint configuration
*/
public Endpoint getEndpoint();
/**
* Set the target endpoint
* @param endpoint target server endpoint
* @return this request for method chaining
*/
public SimpleHttpRequest setEndpoint(Endpoint endpoint);
/**
* Get the request path
* @return URL path component
*/
public String getRequestPath();
/**
* Set the request path
* @param requestPath URL path component
* @return this request for method chaining
*/
public SimpleHttpRequest setRequestPath(String requestPath);
/**
* Get socket timeout in milliseconds
* @return timeout value (default: 3000ms)
*/
public int getSoTimeout();
/**
* Set socket timeout in milliseconds
* @param soTimeout timeout for socket operations
* @return this request for method chaining
*/
public SimpleHttpRequest setSoTimeout(int soTimeout);
/**
* Get all request parameters
* @return map of parameter key-value pairs
*/
public Map<String, String> getParams();
/**
* Set all request parameters
* @param params map of parameter key-value pairs
* @return this request for method chaining
*/
public SimpleHttpRequest setParams(Map<String, String> params);
/**
* Get request charset
* @return charset for encoding parameters (default: from SentinelConfig)
*/
public Charset getCharset();
/**
* Set request charset
* @param charset charset for encoding parameters
* @return this request for method chaining
*/
public SimpleHttpRequest setCharset(Charset charset);
/**
* Add a single parameter to the request
* @param key parameter name (cannot be empty)
* @param value parameter value
* @return this request for method chaining
* @throws IllegalArgumentException if key is blank
*/
public SimpleHttpRequest addParam(String key, String value);
}HTTP response representation providing access to status, headers, and body content.
/**
* Simple HTTP response representation.
*/
public class SimpleHttpResponse {
/**
* Constructor for response without body
* @param statusLine HTTP status line (e.g., "HTTP/1.1 200 OK")
* @param headers response headers
*/
public SimpleHttpResponse(String statusLine, Map<String, String> headers);
/**
* Constructor for response with body
* @param statusLine HTTP status line
* @param headers response headers
* @param body response body as bytes
*/
public SimpleHttpResponse(String statusLine, Map<String, String> headers, byte[] body);
/**
* Set response body
* @param body response body as bytes
*/
public void setBody(byte[] body);
/**
* Get response body as raw bytes
* @return body bytes, or null if no body
*/
public byte[] getBody();
/**
* Get HTTP status line
* @return full status line (e.g., "HTTP/1.1 200 OK")
*/
public String getStatusLine();
/**
* Get HTTP status code
* @return numeric status code (e.g., 200, 404)
*/
public Integer getStatusCode();
/**
* Get all response headers
* @return map of header name-value pairs
*/
public Map<String, String> getHeaders();
/**
* Get specific header value (case-insensitive lookup)
* First tries exact match, then falls back to case-insensitive search
* @param key header name
* @return header value, or null if not found
*/
public String getHeader(String key);
/**
* Get response body as string with appropriate charset parsing.
* Automatically detects charset from Content-Type header or uses default.
* @return body content as string using detected or default charset
*/
public String getBodyAsString();
/**
* String representation of entire response
* @return formatted response with status, headers, and body
*/
public String toString();
}Parser for converting raw HTTP response streams into SimpleHttpResponse objects.
/**
* The parser provides functionality to parse raw bytes HTTP response to a SimpleHttpResponse.
*
* Note that this is a very NAIVE parser, Content-Length must be specified in the
* HTTP response header, otherwise, the body will be dropped. All other body type such as
* Transfer-Encoding: chunked, Transfer-Encoding: deflate are not supported.
*/
public class SimpleHttpResponseParser {
private static final int MAX_BODY_SIZE = 1024 * 1024 * 4; // 4MB limit
/**
* Default constructor with 4KB buffer
*/
public SimpleHttpResponseParser();
/**
* Constructor with custom buffer size
* @param maxSize maximum buffer size for parsing (must be > 0)
* @throws IllegalArgumentException if maxSize < 0
*/
public SimpleHttpResponseParser(int maxSize);
/**
* Parse bytes from an input stream to a SimpleHttpResponse
* @param in input stream from socket connection
* @return parsed HTTP response entity
* @throws IOException when an IO error occurs
* @throws IllegalStateException when response body is too large or content length is invalid
* @throws IndexOutOfBoundsException when buffer size is exceeded during parsing
*/
public SimpleHttpResponse parse(InputStream in) throws IOException;
}Factory for creating sockets based on protocol requirements (HTTP vs HTTPS).
/**
* Factory for creating sockets based on protocol type
*/
public class SocketFactory {
/**
* Get socket for specified protocol
* @param protocol HTTP or HTTPS protocol
* @return Socket for HTTP, SSLSocket for HTTPS
* @throws IOException if socket creation fails
*/
public static Socket getSocket(Protocol protocol) throws IOException;
}HTTP Support:
HTTPS Support:
Parameter Encoding:
application/x-www-form-urlencodedRequest Format:
POST /api/endpoint HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: dashboard.example.com
Content-Length: 45
param1=value1¶m2=value2¶m3=value3Response Requirements:
The HTTP client handles errors at multiple levels:
Connection Errors:
Protocol Errors:
Response Size Limits:
Install with Tessl CLI
npx tessl i tessl/maven-com-alibaba-csp--sentinel-transport-simple-http