Consumer

August 26, 2021 ยท View on GitHub

Consumer configuration

Class longlang\phpkafka\Consumer\ConsumerConfig

You can pass an array to a constructor.

Configuration key

KeyDescriptionDefault
connectTimeoutConnection timeout(unit: second, decimal). -1 means no limit.-1
sendTimeoutSend timeout(unit: second, decimal). -1 means no limit.-1
recvTimeoutReceive timeout (unit: second, decimal). -1 means no limit.-1
clientIdKafka client ID. Use different settings for different consumers.null
maxWriteAttemptsMaximum attempts to write3
clientKafka client used. null by default means auto recognition.null
socketKafka Socket used. null by default means auto recognition.null
brokersAlias is broker. Format: '127.0.0.1:9092,127.0.0.1:9093' or ['127.0.0.1:9092','127.0.0.1:9093']null
bootstrapServersAlias bootstrapServer, used to boot the server. If configured, the server will be connected and brokers updated. Format '127.0.0.1:9092,127.0.0.1:9093' or ['127.0.0.1:9092','127.0.0.1:9093'].null
updateBrokersAuto update brokers.true
intervalIf the message is not received, try again internals. 0 is default and means no intervals(unit: second, decimal).0
groupIdGroup IDnull
memberIdMember IDnull
groupInstanceIdGroup instance ID. Use different settings for different consumers.null
sessionTimeoutIf no heartbeat sent out after the timeout, the group coordinator will consider it dead. (unit: second, decimal)60
rebalanceTimeoutThe maximum time the coordinator waits for consumers to join. (unit: second, decimal)60
topicTopic name. Suppoprt multiple topics consumed simultaneously.null
replicaIdReplica ID-1
rackIdRack ID''
autoCommitAuto commit offsettrue
groupRetryGroup retries allowed if matching an error code.5
groupRetrySleepGroup retry sleep time. (unit: second)1
offsetRetryOffset retries if matching an error code.5
groupHeartbeatGroup heartbeat intervals. (unit: second)3
autoCreateTopicAuto create topic.true
partitionAssignmentStrategyConsumer partition assignment strategy. Optional: Range-longlang\phpkafka\Consumer\Assignor\RangeAssignor, RoundRobin-\longlang\phpkafka\Consumer\Assignor\RoundRobinAssignor, Sticky-\longlang\phpkafka\Consumer\Assignor\StickyAssignor.
exceptionCallbackThis callback is called when an exception that cannot be thrown by the recv() coroutine is encountered. Format: function(\Exception $e){}null
minBytesMin bytes1
maxBytesMax bytes128 * 1024 * 1024
maxWaitThe maximum time. (unit: second, decimal)1
saslSASL authentication Info. If the field is null, it will not authenticate with SASL detail[]
sslSSL Connect Info. If the field is null, it will not use SSL detailnull

Asynchronous (callback)

Example

use longlang\phpkafka\Consumer\ConsumeMessage;
use longlang\phpkafka\Consumer\Consumer;
use longlang\phpkafka\Consumer\ConsumerConfig;

function consume(ConsumeMessage $message)
{
    var_dump($message->getKey() . ':' . $message->getValue());
    // $consumer->ack($message); // If autoCommit is set as false, commit manually.
}
$config = new ConsumerConfig();
$config->setBroker('127.0.0.1:9092');
$config->setTopic('test'); // topic
$config->setGroupId('testGroup'); // group ID
$config->setClientId('test'); // client ID. Use different settings for different consumers.
$config->setGroupInstanceId('test'); // group instance ID. Use different settings for different consumers.
$config->setInterval(0.1);
$consumer = new Consumer($config, 'consume');
$consumer->start();

Synchronous

Example

use longlang\phpkafka\Consumer\Consumer;
use longlang\phpkafka\Consumer\ConsumerConfig;

$config = new ConsumerConfig();
$config->setBroker('127.0.0.1:9092');
$config->setTopic('test'); // topic
$config->setGroupId('testGroup'); // group ID
$config->setClientId('test_custom'); // client ID. Use different settings for different consumers.
$config->setGroupInstanceId('test_custom'); // group instance ID. Use different settings for different consumers.
$consumer = new Consumer($config);
while(true) {
    $message = $consumer->consume();
    if($message) {
        var_dump($message->getKey() . ':' . $message->getValue());
        $consumer->ack($message); // commit manually
    }
    sleep(1);
}

SASL Support

Configuration

KeyDescriptionDefault
typeSASL Authentication Type. PLAIN is \longlang\phpkafka\Sasl\PlainSasl::class''
usernameusername''
passwordpassword''

Example

use longlang\phpkafka\Consumer\Consumer;
use longlang\phpkafka\Consumer\ConsumerConfig;

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

SSL Support

Class longlang\phpkafka\Config\SslConfig

You can pass an array to a constructor.

Configuration keys

KeyDescriptionDefault
openEnable SSLfalse
compressionTLS compression.true
certFilePath to local certificate file on filesystem.''
keyFilePath to local private key file on filesystem''
passphrasePassphrase with which your certFile file was encoded.''
peerNamePeer name to be used. If this value is not set, then the name is remote Host''
verifyPeerRequire verification of SSL certificate used.false
verifyPeerNameRequire verification of peer name.false
verifyDepthAbort if the certificate chain is too deep.0
allowSelfSignedAllow self-signed certificates.false
cafileLocation of Certificate Authority file on local filesystem which should be used''
capathIf cafile is not specified or if the certificate is not found there, the directory pointed to by capath is searched for a suitable certificate. capath must be a correctly hashed certificate directory.''

Example

use longlang\phpkafka\Consumer\Consumer;
use longlang\phpkafka\Consumer\ConsumerConfig;
use longlang\phpkafka\Config\SslConfig;

$config = new ConsumerConfig();
// .... Your Othor 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);
// ....  Your Business Code