0
# HTTP Command Center
1
2
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.
3
4
## Capabilities
5
6
### SimpleHttpCommandCenter
7
8
Main command center implementation that starts an HTTP server to handle dashboard commands.
9
10
```java { .api }
11
/**
12
* The simple command center provides service to exchange information.
13
* Implements CommandCenter interface for HTTP-based communication.
14
*/
15
public class SimpleHttpCommandCenter implements CommandCenter {
16
17
/**
18
* Prepare and init for the command center (e.g. register commands).
19
* This will be executed before starting.
20
*/
21
public void beforeStart() throws Exception;
22
23
/**
24
* Start the command center in the background.
25
* This method should NOT block.
26
*/
27
public void start() throws Exception;
28
29
/**
30
* Stop the command center and do cleanup.
31
*/
32
public void stop() throws Exception;
33
34
/**
35
* Get the name set of all registered commands.
36
* @return Set of command names
37
*/
38
public static Set<String> getCommands();
39
40
/**
41
* Get handler for a specific command
42
* @param commandName name of the command
43
* @return CommandHandler for the command, or null if not found
44
*/
45
public static CommandHandler getHandler(String commandName);
46
47
/**
48
* Register a single command handler
49
* @param commandName name of the command
50
* @param handler the command handler implementation
51
*/
52
public static void registerCommand(String commandName, CommandHandler handler);
53
54
/**
55
* Register multiple command handlers at once
56
* @param handlerMap map of command names to handlers
57
*/
58
public static void registerCommands(Map<String, CommandHandler> handlerMap);
59
}
60
```
61
62
**Usage Examples:**
63
64
```java
65
import com.alibaba.csp.sentinel.transport.command.SimpleHttpCommandCenter;
66
import com.alibaba.csp.sentinel.command.CommandHandler;
67
import com.alibaba.csp.sentinel.transport.CommandCenter;
68
69
// Create and start command center
70
CommandCenter commandCenter = new SimpleHttpCommandCenter();
71
72
// Register custom command handlers before starting
73
Map<String, CommandHandler> customHandlers = new HashMap<>();
74
customHandlers.put("custom-command", new MyCustomCommandHandler());
75
SimpleHttpCommandCenter.registerCommands(customHandlers);
76
77
// Initialize and start the server
78
commandCenter.beforeStart(); // Registers built-in command handlers
79
commandCenter.start(); // Starts HTTP server on configured port
80
81
// Server is now listening for dashboard commands
82
// Default port is 8719, configurable via TransportConfig
83
84
// Check registered commands
85
Set<String> commands = SimpleHttpCommandCenter.getCommands();
86
System.out.println("Available commands: " + commands);
87
88
// Stop when shutting down
89
commandCenter.stop();
90
```
91
92
### HttpEventTask
93
94
Task that handles individual HTTP command requests. This is used internally by the command center but can be useful for understanding request processing.
95
96
```java { .api }
97
/**
98
* The task handles incoming command request in HTTP protocol.
99
* Implements Runnable for concurrent request processing.
100
*/
101
public class HttpEventTask implements Runnable {
102
public static final String SERVER_ERROR_MESSAGE = "Command server error";
103
public static final String INVALID_COMMAND_MESSAGE = "Invalid command";
104
105
/**
106
* Constructor taking the socket connection
107
* @param socket the socket connection from client
108
*/
109
public HttpEventTask(Socket socket);
110
111
/**
112
* Close the socket connection
113
*/
114
public void close() throws Exception;
115
116
/**
117
* Process the HTTP request (Runnable implementation)
118
*/
119
public void run();
120
121
/**
122
* Parse raw HTTP request line to a CommandRequest
123
* @param line HTTP request line
124
* @return parsed command request
125
*/
126
protected static CommandRequest processQueryString(String line);
127
128
/**
129
* Parse URL parameters into CommandRequest
130
* @param queryString parameter string from URL
131
* @param request CommandRequest to populate
132
*/
133
protected static void parseParams(String queryString, CommandRequest request);
134
135
/**
136
* Remove URL anchor from query string
137
* @param str query string with potential anchor
138
* @return query string without anchor
139
*/
140
protected static String removeAnchor(String str);
141
142
/**
143
* Process POST request headers and body
144
* @param in input stream from socket
145
* @param request CommandRequest to populate with POST data
146
* @throws RequestException for malformed requests
147
* @throws IOException for I/O errors
148
*/
149
protected static void processPostRequest(InputStream in, CommandRequest request)
150
throws RequestException, IOException;
151
152
/**
153
* Parse HTTP headers from POST request
154
* @param in input stream from socket
155
* @return map of header name-value pairs, null for illegal request
156
* @throws IOException for I/O errors
157
*/
158
protected static Map<String, String> parsePostHeaders(InputStream in) throws IOException;
159
}
160
```
161
162
163
### Server Configuration
164
165
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.
166
167
**Default Configuration:**
168
- Default port: 8719
169
- Socket timeout: 3000ms
170
- Thread pool size: Number of CPU cores
171
- Queue size: 10 requests
172
- Supported methods: GET, POST
173
- Content type: application/x-www-form-urlencoded
174
175
**Port Discovery:**
176
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.
177
178
## Error Handling
179
180
The command center handles various error conditions:
181
182
- **Command not found**: Returns 400 Bad Request
183
- **Invalid request format**: Returns 400 Bad Request
184
- **Request timeout**: Returns 408 Request Timeout
185
- **Missing Content-Length**: Returns 411 Length Required
186
- **Unsupported media type**: Returns 415 Unsupported Media Type
187
- **Server errors**: Returns 500 Internal Server Error
188
189
All errors are logged with appropriate detail levels and the connection is properly closed.