Windows Client Linux Server---Go

Source: Internet
Author: User

Original: http://blog.csdn.net/jingshuigg/article/details/25001979

First, for the server-side construction can refer to the previous article: Kafka stand-alone version of the environment to build and test

Server-Side ip:10.0.30.221

The directory of the running environment is as follows:

You need to change the following two properties in Server.properties under the Config folder

zookeeper.connect=localhost:2181 Change to zookeeper.connect=00.00.00.01 (IP address): 2181

And the default commented out

#host. Name=localhost Change to host.name=00.00.00.01 (IP address)

Host.name does not change causes the client to report the error as follows

Exception in Thread "Thread-0" kafka.common.FailedToSendMessageException:Failed to send messages after 3 tries.at KAF Ka.producer.async.DefaultEventHandler.handle (Defaulteventhandler.scala:) at Kafka.producer.Producer.send (Producer.scala:) at Kafka.javaapi.producer.Producer.send (Producer.scala: ) at Its.kafka.Producer.run (Producer.java:46)

After completion of the above steps, according to the "Kafka stand-alone version of the environment set up and test" method to start Zookeeper-server and Kafka-server can

Second, the client building

The Win7 system used by the client to connect to the server in eclipse

1. Under Eclipse, create a new project Kafka_producer with the following directory:

Note: The log4j.properties file under the Config folder is placed under SRC so that the log information can be observed

The code for producer is as follows:

Importjava.util.Properties;ImportKafka.producer.KeyedMessage;ImportKafka.producer.ProducerConfig; Public classProducerextendsthread{Private FinalKafka.javaapi.producer.producer<integer, string>producer; Private FinalString topic; Private FinalString name; Private Final intNumsofmessage; Private FinalProperties props =NewProperties ();  PublicProducer (String name,string topic,intnumsofmessage) {Props.put ("Serializer.class", "Kafka.serializer.StringEncoder"); Props.put ("Metadata.broker.list", "10.0.30.221:9092"); //Send asynchronously//props.put ("Producer.type", "async"); //How many bars are sent each time//props.put ("Batch.num.messages", "the");Producer =NewKafka.javaapi.producer.producer<integer, String> (Newproducerconfig (props));  This. Topic =topic;  This. Name =name;  This. Numsofmessage =Numsofmessage; }         Public voidrun () {intMessageno = 1;  while(Messageno <= Numsofmessage) {//the number of messages produced by each producer;String message =NewString (name+ "s Message_" + messageno+ "******"); Keyedmessage<integer, string> messageforsend =NewKeyedmessage<integer, string>(topic, message);              Producer.send (Messageforsend); Messageno++;     } producer.close (); }}

The code to start producer is as follows:

ImportJava.util.concurrent.TimeUnit; Public classKafkaproducerdemoImplementskafkaproperties{ Public Static voidMain (string[] args) {Startthread (1, "Testtopic", 10); }    /**     * @paramnumber of Numsofproducer producers *@paramtopic The subject of the message *@paramnumsofmessage The message tree produced by each producer *@return      */     Public Static voidStartthread (intNumsofproducer,string topic,intnumsofmessage) {         for(inti = 1; I <= numsofproducer; i + +) {String name= "Producer" +i; NewProducer (name,topic,numsofmessage). Start (); }    }}

2. Create a new Kafka_consumer project under Eclipse, with the following directory:

The consumer code is as follows:

ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importjava.util.Properties;ImportKafka.consumer.ConsumerConfig;ImportKafka.consumer.ConsumerIterator;ImportKafka.consumer.KafkaStream;ImportKafka.javaapi.consumer.ConsumerConnector; Public classConsumerextendsThread {Private FinalConsumerconnector Consumer; Private FinalString topic; Private FinalString name;  PublicConsumer (String name,string topic) {Consumer=Kafka.consumer.Consumer.createJavaConsumerConnector (Createconsumerconfig ());  This. Topic =topic;  This. Name =name; }  Private Staticconsumerconfig Createconsumerconfig () {Properties props=NewProperties (); Props.put ("Zookeeper.connect", Kafkaproperties.zkconnect); Props.put ("Group.id", Kafkaproperties.groupid); Props.put ("Zookeeper.session.timeout.ms", "60000"); Props.put ("Zookeeper.sync.time.ms", "200"); Props.put ("Auto.commit.interval.ms", "1000"); //The minimum number of bytes received each time, the default is 1//props.put ("Fetch.min.bytes", "1024"); //minimum wait time per time, default is//props.put ("fetch.wait.max.ms", "600000");    return Newconsumerconfig (props); }    Public voidrun () {Map<string, integer> topiccountmap =NewHashmap<string, integer>(); Topiccountmap.put (Topic,NewInteger (1)); Map<string, list<kafkastream<byte[],byte[]>>> Consumermap =Consumer.createmessagestreams (TOPICCOUNTMAP); Kafkastream<byte[],byte[]> stream = Consumermap.get (topic). Get (0); Consumeriterator<byte[],byte[]> it =Stream.iterator ();  while(It.hasnext ()) {System.out.println ("************" +name+ "gets" +NewString (It.next (). Message ())); }  }}

To start the consumer code:

 Public class Implements kafkaproperties{  publicstaticvoid  main (string[] args) {     // Consumer1    New Consumer ("Consumer1", kafkaproperties.topic);    Consumerthread1.start ();  }}

The properties of the code (in order to pass the property value, of course, it can also be XML to provide property values):

 Public Interfacekafkaproperties{Final StaticString zkconnect = "10.0.30.221:2181"; Final StaticString groupId = "Group1"; Final StaticString topic = "Testtopic"; Final StaticString Kafkaserverurl = "10.0.30.221"; Final Static intKafkaserverport = 9092; Final Static intKafkaproducerbuffersize = 64*1024; Final Static intConnectionTimeout = 100000; Final Static intReconnectinterval = 10000; Final StaticString clientId = "Simpleconsumerdemoclient";}

3. Start consumer and then start producer, which is observed in the console window of Eclipse:

Windows Client Linux Server---Go

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.