CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-alibaba-csp--sentinel-transport-simple-http

Simple HTTP transport module for Sentinel providing basic HTTP communication capabilities for heartbeat and command transport

Pending
Overview
Eval results
Files

heartbeat.mddocs/

Heartbeat System

The heartbeat system provides periodic communication between Sentinel applications and dashboard servers to maintain connectivity and report application status. It includes automatic failover between multiple dashboard addresses and comprehensive application metadata reporting.

Capabilities

SimpleHttpHeartbeatSender

Main heartbeat sender implementation that manages communication with dashboard servers.

/**
 * The heartbeat sender provides basic API for sending heartbeat request to provided target.
 * This implementation is based on a trivial HTTP client.
 */
public class SimpleHttpHeartbeatSender implements HeartbeatSender {
    
    /**
     * Constructor that initializes with default dashboard addresses.
     * Retrieves dashboard server list from TransportConfig.
     */
    public SimpleHttpHeartbeatSender();
    
    /**
     * Send heartbeat to Sentinel Dashboard. Each invocation of this method will send
     * heartbeat once. Sentinel core is responsible for invoking this method
     * at every intervalMs() interval.
     * @return whether heartbeat is successfully sent
     */
    public boolean sendHeartbeat() throws Exception;
    
    /**
     * Default interval in milliseconds of the sender. It would take effect only when
     * the heartbeat interval is not configured in Sentinel config property.
     * @return default interval of the sender in milliseconds (10000ms)
     */
    public long intervalMs();
}

Usage Examples:

import com.alibaba.csp.sentinel.transport.heartbeat.SimpleHttpHeartbeatSender;
import com.alibaba.csp.sentinel.transport.HeartbeatSender;

// Create heartbeat sender
HeartbeatSender heartbeatSender = new SimpleHttpHeartbeatSender();

// Send single heartbeat
try {
    boolean success = heartbeatSender.sendHeartbeat();
    if (success) {
        System.out.println("Heartbeat sent successfully");
    } else {
        System.out.println("Failed to send heartbeat");
    }
} catch (Exception e) {
    System.err.println("Error sending heartbeat: " + e.getMessage());
}

// Get heartbeat interval
long interval = heartbeatSender.intervalMs(); // Returns 10000ms (10 seconds)

// Note: Sentinel core automatically manages periodic heartbeat sending
// Manual invocation is typically not needed in production

HeartbeatMessage

Container for heartbeat message data that includes application metadata and system information.

/**
 * Heart beat message entity.
 * The message consists of key-value pair parameters.
 */
public class HeartbeatMessage {
    
    /**
     * Constructor that auto-populates basic system information:
     * - hostname: system hostname
     * - ip: configured heartbeat client IP
     * - app: application name
     * - app_type: application type
     * - port: transport port
     */
    public HeartbeatMessage();
    
    /**
     * Register additional custom information in the heartbeat
     * @param key information key
     * @param value information value
     * @return this HeartbeatMessage for method chaining
     */
    public HeartbeatMessage registerInformation(String key, String value);
    
    /**
     * Generate the current message map with timestamp and version info.
     * Adds dynamic fields:
     * - v: Sentinel version
     * - version: current timestamp (via TimeUtil.currentTimeMillis())
     * - port: current transport port (updated from TransportConfig.getPort())
     * @return Map of all heartbeat parameters including static and dynamic fields
     */
    public Map<String, String> generateCurrentMessage();
}

Usage Examples:

import com.alibaba.csp.sentinel.transport.heartbeat.HeartbeatMessage;

// Create heartbeat message with default system info
HeartbeatMessage heartbeat = new HeartbeatMessage();

// Add custom application information
heartbeat.registerInformation("environment", "production")
         .registerInformation("version", "1.0.0")
         .registerInformation("instance_id", "app-001");

// Generate complete message for sending
Map<String, String> messageData = heartbeat.generateCurrentMessage();

// The message will include:
// - hostname, ip, app, app_type, port (from constructor)
// - environment, version, instance_id (custom additions)
// - v, version (timestamp), port (from generateCurrentMessage)

System.out.println("Heartbeat data: " + messageData);

Heartbeat Configuration

The heartbeat system automatically configures itself based on system settings:

Default Values:

  • Interval: 10000ms (10 seconds)
  • HTTP Status Success: 200 OK
  • Request Path: Retrieved from TransportConfig.getHeartbeatApiPath()
  • Client IP: Retrieved from TransportConfig.getHeartbeatClientIp()
  • Dashboard Addresses: Retrieved from TransportConfig.getConsoleServerList()

Address Failover: The heartbeat sender cycles through configured dashboard addresses. If one address fails, it automatically tries the next address in the list on subsequent heartbeat attempts.

Automatic Information: Each heartbeat automatically includes:

  • hostname: System hostname
  • ip: Configured client IP address
  • app: Application name from AppNameUtil
  • app_type: Application type from SentinelConfig
  • port: Current transport port
  • v: Sentinel framework version
  • version: Current timestamp (for tracking message freshness)

Error Handling and Logging

The heartbeat system handles various error conditions gracefully:

Connection Errors:

  • Network connectivity issues are logged and cause failover to next dashboard address
  • Socket timeouts are handled with appropriate logging

HTTP Errors:

  • Client errors (4xx): Logged as warnings with status code details
  • Server errors (5xx): Logged as warnings with status code details
  • Successful responses (200): Heartbeat considered successful

Configuration Errors:

  • Missing dashboard addresses: Logged as warning, heartbeat disabled
  • Invalid port configuration: Heartbeat skipped until port is initialized

All error conditions are logged using RecordLog with appropriate severity levels and do not throw exceptions to calling code.

Install with Tessl CLI

npx tessl i tessl/maven-com-alibaba-csp--sentinel-transport-simple-http

docs

command-center.md

heartbeat.md

http-client.md

index.md

tile.json