phpkafka

repository·master·Indexed 43 days ago

https://github.com/swoole/phpkafka

A high-compatibility PHP Kafka client supporting both PHP-FPM and Swoole runtimes. It implements over 50 Kafka APIs and supports features including message compression, SASL, and SSL. Requires PHP >= 7.1 and Kafka >= 1.0.0, with Swoole >= 4.5 optional for specific features.

Tokens
14.6K
Snippets
36
Records
52
Agent score
52%

What's inside phpkafka

  1. Overview of longlang/phpkafka

    master
    longlang/phpkafka is a PHP Kafka client designed for use in both PHP-FPM and Swoole environments. It implements the Kafka communication protocol based on the JSON format used in Java and supports 50 different Kafka APIs. The library currently provides implementations for both producing and consuming messages.
  2. Overview of longlang/phpkafka features

    master

    longlang/phpkafka is a PHP Kafka client designed to work in both PHP-FPM and Swoole environments. It is built based on the JSON protocol structures from the Java version, allowing it to support a wide range of Kafka APIs.

    Key Features:

    • API Support: Supports all 50 Kafka APIs.
    • Compression: Supports gzip, snappy, lz4, and zstd message compression.
    • Environment Compatibility: Intelligent detection and compatibility for both PHP-FPM and Swoole.
    • Security: Supports SASL authentication and SSL encrypted communication.
    • Core Components: Includes Producer and Consumer classes.
  3. Understand the Kafka Message Protocol and Serialization

    master

    The Kafka protocol is defined via JSON specification files that describe how clients and servers exchange information. The protocol relies on Requests and Responses, where each request is identified by a 16-bit api key.

    Key concepts include:

    • Versions: Each message has a unique 16-bit version number. The version of a response must always match the version of the corresponding request.
    • validVersions: A top-level field in requests/responses specifying the range of protocol versions supported (e.g., "0-2" means versions 0, 1, and 2 are understood).
    • MessageData Objects: These are generated objects that represent all possible versions of a message, allowing a single code path to handle multiple versions.
    • Flexible Versions: Newer versions that use more efficient serialization (e.g., COMPACT_STRING instead of STRING) to save space.
  4. Define and Structure Message Fields

    master

    Messages consist of an array of fields. The order of fields is critical; changing the order is an incompatible change.

    Field Types

    • Primitives: boolean, int8, int16, int32, int64, float64, string (UTF-8), and bytes (binary).
    • Arrays: Declared using [] followed by the element type (e.g., []Foo).

    Field Properties

    • Version Specs: Fields can be version-specific. Use "3+" for fields appearing in version 3 and later, or "0-2" to indicate a field is removed in version 3.
    • Nullable Fields: string, bytes, and arrays can be marked as nullable via nullableVersions. Booleans, ints, and floats cannot be null.
    • Tagged Fields: Optional data that can be attached to messages without breaking compatibility. They can only be added to "flexible" message versions and must have an open-ended taggedVersions range.
    • mapKey: Setting "mapKey": true on an array element turns the array into a linked hash set, allowing $O(1)$ access via a generated find function while preserving order.
  5. How partitioning works in the Producer

    master

    The producer uses a default partitioning strategy if no specific partition is provided in the message. The logic follows this hierarchy:

    1. Explicit Partition: If a partition is specified, that partition is used.
    2. Key-based Partitioning: If partition is null but a key is provided, the partition is selected using crc32(key) % partitions.
    3. Round Robin: If both partition and key are null, the producer uses a Round Robin strategy to select a partition.
  6. Configure SSL connection settings

    master

    Use the longlang\phpkafka\Config\SslConfig class to enable and configure SSL/TLS encryption. Once configured, pass the SslConfig instance to the ConsumerConfig using setSsl().

    Key SslConfig parameters:

    • open: Boolean to enable SSL transmission encryption.
    • verifyPeer: Whether to verify the remote certificate.
    • allowSelfSigned: Whether to allow self-signed certificates.
    • cafile: Path to the CA certificate.
    • capath: Directory containing CA certificates (scans all .pem files).
    • certFile: Path to the client certificate.
    • keyFile: Path to the private key.
    • passphrase: Password for the certificate.
    use longlang\phpkafka\Consumer\Consumer;
    use longlang\phpkafka\Consumer\ConsumerConfig;
    use longlang\phpkafka\Config\SslConfig;
    
    $config = new ConsumerConfig();
    // ... other config
    $sslConfig = new SslConfig();
    $sslConfig->setOpen(true);
    $sslConfig->setVerifyPeer(true);
    $sslConfig->setAllowSelfSigned(true);
    $sslConfig->setCafile("/kafka-client/.github/kafka/cert/ca-cert");
    
    $config->setSsl($sslConfig);
    $consumer = new Consumer($config);
  7. Check environment requirements for longlang/phpkafka

    master

    Before installing, ensure your environment meets the following requirements:

    • PHP: >= 7.1
    • Kafka: >= 1.0.0
    • Swoole: >= 4.5 (optional, required if using Swoole-specific features)
  8. Avoid Incompatible Protocol Changes

    master

    To maintain compatibility between clients and servers, avoid the following changes to the message protocol:

    1. Modifying Released Versions: Never change a protocol version that has already been released. Correct mistakes by issuing a new version.
    2. Re-ordering Fields: You may add fields, but you must never change the relative order of existing fields.
    3. Changing Defaults: Never change the default value of an existing field.
    4. Changing Types: Never change the type of an existing field (except for converting an array of primitives to an array of single-field structures, which is binary-compatible).
  9. Configure SASL authentication for Consumer

    master

    To use SASL authentication, pass an array to $config->setSasl(). The type field should specify the SASL Authentication Type, such as \longlang\phpkafka\Sasl\PlainSasl::class for PLAIN authentication.

    use longlang//
    use longlang//
    
    $config = new ConsumerConfig();
    // .... Your Other Config
    $config->setSasl([
        "type"=>\longlang\phpkafka\Sasl\PlainSasl::class,
        "username"=>"admin",
        "password"=>"admin-secret"
    ]);
    $consumer = new Consumer($config);
    // .... Your Business Code
  10. Enable SASL authentication

    master

    To authenticate with Kafka using SASL, pass a configuration array to setSasl() in ProducerConfig.

    Supported SASL types include \longlang\phpkafka\Sasl\PlainSasl::class for PLAIN authentication. You must provide the type, username, and password.

    use longlang\phpkafka\Producer\ProducerConfig;
    use longlang\phpkafka\Producer\Producer;
    
    $config = new ProducerConfig();
    $config->setSasl([
        "type" => \longlang\phpkafka\Sasl\PlainSasl::class,
        "username" => "admin",
        "password" => "admin-secret"
    ]);
    $producer = new Producer($config);
  11. Enable SSL connection

    master

    Use longlang\phpkafka\Config\SslConfig to configure SSL/TLS settings and pass it to the ProducerConfig via setSsl().

    use longlang\phpkafka\Producer\ProducerConfig;
    use longlang\phpkafka\Producer\Producer;
    use longlang\phpkafka\Config\SslConfig;
    
    $config = new ProducerConfig();
    $sslConfig = new SslConfig();
    $sslConfig->setOpen(true);
    $sslConfig->setVerifyPeer(true);
    $sslConfig->setAllowSelfSigned(true);
    $sslConfig->setCafile("/kafka-client/.github/kafka/cert/ca-cert");
    
    $config->setSsl($sslConfig);
    $producer = new Producer($config);