Kafka principle and Java Simple producer example

Source: Internet
Author: User

Brokers and consumers use ZK to get status information and track message coordinates.
Each partition is an ordered, immutable sequence of messages.
Only when the file in the partition is replaced with the disk files, is it open to consumers to consume.
Each partition is replicated across servers to other places, for fault-tolerant purposes.
This partition can be understood as a unit of block in Hadoop.
But only the server partition selected as leader to serve the consumer's reading and producer's writing,
Followers just syncs the data over. The synchronization state is better included in the ISR, these ISR and leader
The information is stored in ZK, and when the leader state is abnormal, one of the follower in the ISR becomes the new leader.
In the entire Kafka cluster, each server plays a dual role, it may be a top leader partition,
It can also be another topic follower partition. This ensures that the cluster is load balanced.

Each consumer represents a process, and multiple consumers form a consumer group.
A message in a top can only be consumed by one consumer in a consumer group, and if it needs to be consumed by multiple consumers, those consumers need to be in different consumer groups.
The reason may be that the units in the consumer group maintain partition offset in ZK.

In Kafka's design, broker is stateless, which means it is not responsible for managing which consumers consume what partition messages to where, or even who consumes them.
For message retention policies, Kafka takes a time-based SLA, and a message is automatically deleted when it reaches this SLA.

There are two replication strategies for Kafka, synchronous and asynchronous, and synchronization will send confirmation messages to producer after both the lead replica and follower have finished storing the messages.
Asynchronous synchronization, as long as the lead replica received information, send producer confirmation information, if this time lead replica broker problem, there will be risk.

Producers
Kafka Message API for producer
From the previous analysis, the data is encapsulated as a message, how to send to Kafka it? First, you need to get this topic lead partition.
Messages can be sent one by one, or they can be bulk compressed asynchronously. That is, save to a certain amount or a certain time before sending.
Producer:kafka provides the Kafka.javaapi.producer.Producer class (classproducer<k,v>). The default partitioning strategy is to hash the key.

Importjava.util.Date;Importjava.util.Properties;ImportKafka.javaapi.producer.Producer;ImportKafka.producer.KeyedMessage;ImportKafka.producer.ProducerConfig; Public    classSimpleproducer {Private    StaticProducer<string, string>producer;  PublicSimpleproducer () {Properties props=NewProperties ();//Set the broker list for requesting metadata to find the lead brokerProps.put ("Metadata.broker.list",                "192.168.146.132:9092, 192.168.146.132:9093, 192.168.146.132:9094");//This specifies the serializer class for keysProps.put ("Serializer.class", "Kafka.serializer.StringEncoder");//1 means the producer receives an acknowledgment once the leads replica//Has received the data. This option provides better durability as the//client waits until the server acknowledges the request as successful.Props.put ("Request.required.acks", "1"); Producerconfig Config=Newproducerconfig (props); Producer=NewProducer<string, string>(config); }     Public    Static    voidMain (string[] args) {intArgscount =args.length; if(Argscount = = 0 | | Argscount = = 1)            Throw    NewIllegalArgumentException ("Please provide topic name and Message count as arguments"); String Topic= (String) args[0]; String Count= (String) args[1]; intMessagecount =Integer.parseint (count); System.out.println ("Topic Name-" +topic); System.out.println ("Message Count-" +messagecount); Simpleproducer Simpleproducer=NewSimpleproducer ();    Simpleproducer.publishmessage (topic, messagecount); }    Private    voidPublishMessage (String topic,intMessagecount) {         for(intMCount = 0;    MCount < Messagecount; mcount++) {String runtime=NewDate (). toString (); String msg= "Message Publishing Time-" +runtime; SYSTEM.OUT.PRINTLN (msg);//creates a Keyedmessage instancekeyedmessage<string, string> data =NewKeyedmessage<string, string>(topic, msg);//Publish the messageproducer.send (data); }//Close producer connection with broker.Producer.close (); }}

Kafka principle and Java Simple producer example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.