Simple HTTP transport module for Sentinel providing basic HTTP communication capabilities for heartbeat and command transport
—
The SimpleHttpCommandCenter provides HTTP-based communication between Sentinel applications and dashboard servers. It acts as a server that receives and processes commands for configuration updates, monitoring queries, and control operations.
Main command center implementation that starts an HTTP server to handle dashboard commands.
/**
* The simple command center provides service to exchange information.
* Implements CommandCenter interface for HTTP-based communication.
*/
public class SimpleHttpCommandCenter implements CommandCenter {
/**
* Prepare and init for the command center (e.g. register commands).
* This will be executed before starting.
*/
public void beforeStart() throws Exception;
/**
* Start the command center in the background.
* This method should NOT block.
*/
public void start() throws Exception;
/**
* Stop the command center and do cleanup.
*/
public void stop() throws Exception;
/**
* Get the name set of all registered commands.
* @return Set of command names
*/
public static Set<String> getCommands();
/**
* Get handler for a specific command
* @param commandName name of the command
* @return CommandHandler for the command, or null if not found
*/
public static CommandHandler getHandler(String commandName);
/**
* Register a single command handler
* @param commandName name of the command
* @param handler the command handler implementation
*/
public static void registerCommand(String commandName, CommandHandler handler);
/**
* Register multiple command handlers at once
* @param handlerMap map of command names to handlers
*/
public static void registerCommands(Map<String, CommandHandler> handlerMap);
}Usage Examples:
import com.alibaba.csp.sentinel.transport.command.SimpleHttpCommandCenter;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.transport.CommandCenter;
// Create and start command center
CommandCenter commandCenter = new SimpleHttpCommandCenter();
// Register custom command handlers before starting
Map<String, CommandHandler> customHandlers = new HashMap<>();
customHandlers.put("custom-command", new MyCustomCommandHandler());
SimpleHttpCommandCenter.registerCommands(customHandlers);
// Initialize and start the server
commandCenter.beforeStart(); // Registers built-in command handlers
commandCenter.start(); // Starts HTTP server on configured port
// Server is now listening for dashboard commands
// Default port is 8719, configurable via TransportConfig
// Check registered commands
Set<String> commands = SimpleHttpCommandCenter.getCommands();
System.out.println("Available commands: " + commands);
// Stop when shutting down
commandCenter.stop();Task that handles individual HTTP command requests. This is used internally by the command center but can be useful for understanding request processing.
/**
* The task handles incoming command request in HTTP protocol.
* Implements Runnable for concurrent request processing.
*/
public class HttpEventTask implements Runnable {
public static final String SERVER_ERROR_MESSAGE = "Command server error";
public static final String INVALID_COMMAND_MESSAGE = "Invalid command";
/**
* Constructor taking the socket connection
* @param socket the socket connection from client
*/
public HttpEventTask(Socket socket);
/**
* Close the socket connection
*/
public void close() throws Exception;
/**
* Process the HTTP request (Runnable implementation)
*/
public void run();
/**
* Parse raw HTTP request line to a CommandRequest
* @param line HTTP request line
* @return parsed command request
*/
protected static CommandRequest processQueryString(String line);
/**
* Parse URL parameters into CommandRequest
* @param queryString parameter string from URL
* @param request CommandRequest to populate
*/
protected static void parseParams(String queryString, CommandRequest request);
/**
* Remove URL anchor from query string
* @param str query string with potential anchor
* @return query string without anchor
*/
protected static String removeAnchor(String str);
/**
* Process POST request headers and body
* @param in input stream from socket
* @param request CommandRequest to populate with POST data
* @throws RequestException for malformed requests
* @throws IOException for I/O errors
*/
protected static void processPostRequest(InputStream in, CommandRequest request)
throws RequestException, IOException;
/**
* Parse HTTP headers from POST request
* @param in input stream from socket
* @return map of header name-value pairs, null for illegal request
* @throws IOException for I/O errors
*/
protected static Map<String, String> parsePostHeaders(InputStream in) throws IOException;
}The command center automatically discovers an available port starting from the configured base port (default 8719). It creates a thread pool for handling concurrent requests and manages the server lifecycle.
Default Configuration:
Port Discovery: The server tries ports starting from the base port, incrementing by 1 every 3 attempts until it finds an available port. The final port is registered with TransportConfig for other components to use.
The command center handles various error conditions:
All errors are logged with appropriate detail levels and the connection is properly closed.
Install with Tessl CLI
npx tessl i tessl/maven-com-alibaba-csp--sentinel-transport-simple-http