Jafka fast start of distributed message system

Source: Internet
Author: User

Jafka is an open-source/well-performing distributed messaging system. A brief introduction in the previous article.
The following is a simple introductory document. More detailed documentation Reference Wiki.

Step 1: Download the latest installation package

The Complete installation Guide is here.
Latest release address in: Https://github.com/adyliu/jafka/downloads



Optional configuration, set an environment variable. $export $JAFKA _home=/opt/apps/jafka-1.0 The following assumes that all operating directories are under $jafka_home.

Step 2: Start the service side

This launches a single-process server, starting with the default configuration. Because some paths use relative paths, they need to be run in Jafka's home directory.

By default, no configuration is required to run the service side. At this point, the server binds port 9092 to all network cards.

Step 3: Send a message

A simple text message can be sent using the small command line that comes with it.



Producer-console.sh has some parameters, which can be obtained by executing the following command. $bin/producer-console.sh

Send the message only need to enter the text in the prompt symbol ' > ', no error means that the sending is successful, directly enter or input CTRL + C exit program.

Step 4: Start the consumer

Now is the time to consume the message just sent.

The same jafka comes with a small program that can consume simple text messages.



Immediately after connecting to the server, you will see a message consumption. By default simple-consumer-console.sh The ordinal of the output message (which does not actually exist) and the next offset of the message (offset).

After decompression, simply execute the above three commands to complete a simple message delivery and accept the demo. This is a simple messaging system.

Step 5: Manual coding

We want to use the provided API to manually encode to send and receive some messages.

Message sender

Start by writing a simple message sender.

public static void Main (string[] args) throws Exception {
Properties Props = new properties ();
Props.put ("Broker.list", "0:127.0.0.1:9092");
Props.put ("Serializer.class", StringEncoder.class.getName ());
//
Producerconfig config = new Producerconfig (props);
producer<string, string> Producer = new producer<string, string> (config);
//
Stringproducerdata data = new Stringproducerdata ("demo");
for (int i=0;i<1000;i++) {
Data.add ("Hello World #" +i);
}
//
try {
Long start = System.currenttimemillis ();
for (int i = 0; i <; i++) {
Producer.send (data);
}
Long cost = System.currenttimemillis ()-Start;
System.out.println ("Send 100000 message cost:" +cost+ "MS");
} finally {
Producer.close ();
}
}

It looks a little complicated and we simply break it down.

Configuration parameters

First, you need to configure the server-side address. A Jfaka server address format is as follows:

    • The Brokerid is used to identify the service process, which is globally unique within a cluster
    • The Host/port user describes the IP address and port that the service listens on, and by default listens for data on 9092 ports on all network cards.

After configuring the service-side information, we need to provide a message encoding.

By default Jafka parses the byte array encoding, which is sent out intact. Here is a simple replacement, using the string UTF-8 encoding.

Constructing the messaging client

A simple message-sending client can be constructed using the simple parameters above.

Constructing messages

Used to encode strings, so only the data of the string can be sent here. Each message packet can have multiple messages, only the size of a message packet must not exceed the default of 1M. For example, the following constructs a packet of 100 messages destined for the theme:

StringProducerData data = new StringProducerData("demo");
for(int i=0;i<1000;i++) {
data.add("Hello world #"+i);
Send Message

The last message sent only needs to call Producer.send (). In the example above, the loop is sent 100 times.

Here is the result of a send:

$bin/run-console.sh demo.client.StaticBrokerSender
Message Recipients

The logic to accept the message is simple, just configure the server-side address, and then consume the message sequentially from offset 0.

The following logic is simple to display the accepted message as a UTF-8 string.

Simpleconsumer consumer = new Simpleconsumer ("127.0.0.1", 9092);
//
Long offset = 0;
while (true) {
Fetchrequest request = new Fetchrequest ("test", 0, offset);
For (Messageandoffset Msg:consumer.fetch (request)) {
System.out.println (Utils.tostring (Msg.message.payload (), "UTF-8"));
offset = Msg.offset;
}
}

Integrated Zookeeper

Jafka uses zookeeper for automatic broker addressing and consumer load balancing.

(1) Start zookeeper service

You can use a single-process zookeeper to replace the zookeeper cluster when testing.

(2) Start Jafka server $bin/server-single.sh config/server.properties
[2012-04-24 12:29:56,526] INFO starting Jafka Server (com.sohu.jafka.server.server.java:68)
[2012-04-24 12:29:56,532] INFO starting log Cleaner every 60000 MS (com.sohu.jafka.log.logmanager.java:155)
[2012-04-24 12:29:56,552] INFO Connecting to zookeeper:127.0.0.1:2181 (COM.SOHU.JAFKA.SERVER.ZOOKEEPER.JAVA:80)
[2012-04-24 12:29:56,568] INFO starting Zkclient event thread. (com.github.zkclient.zkeventthread.java:64)


Information that is automatically registered to the zookeeper server after the server is started, such as IP address, port, existing message, and so on.

(3) Start message sender

$bin/producer-console.sh--zookeeper localhost:2181--topic Demo
Enter a message and exit with empty string.
> Jafka Second day
> Jafka use Zookeeper to search brokers and consumers
>

Similar to the sender of the message above, except that it uses the zookeeper configuration to automatically find the server instead of specifying the server-side address.

(4) Start message recipient $bin/consumer-console.sh--zookeeper localhost:2181--topic Demo--from-beginning
Jafka second day
Jafka use zookeeper to search brokers and consumers

It was very quick to see the message that was just sent.

Because zookeeper is used as the configuration center, more server, message senders, and message recipients can be started. All you have to do is to make sure that you are connected to zookeeper, and that all services have a unique Brokerid (located in server.properties).

(5) API usage

The above is a simple text message sent using your own program. The API is used here to develop.

Send message public static void main (string[] args) throws Exception {
Properties Props = new properties ();
Props.put ("Zk.connect", "localhost:2181");
Props.put ("Serializer.class", StringEncoder.class.getName ());
//
Producerconfig config = new Producerconfig (props);
producer<string, string> Producer = new producer<string, string> (config);
//
Stringproducerdata data = new Stringproducerdata ("demo");
for (int i=0;i<100;i++) {
Data.add ("Hello World #" +i);
}
//
try {
Long start = System.currenttimemillis ();
for (int i = 0; i <; i++) {
Producer.send (data);
}
Long cost = System.currenttimemillis ()-Start;
SYSTEM.OUT.PRINTLN ("Send 10000 message cost:" +cost+ "MS");
} finally {
Producer.close ();
}
}


In contrast to message senders who do not use zookeeper, simply replace the server configuration information with the Zookeeper connection address. The other is exactly the same.

Receiving messages

Accepting the message looks a little bit more complicated, in a nutshell, the following steps:

    • Configuring Zookeeper and Client GroupID
    • Connection to the server
    • Creating a Message Flow
    • Start thread pool consumption message

public static void Main (string[] args) throws Exception {

Properties Props = new properties ();
Props.put ("Zk.connect", "localhost:2181");
Props.put ("GroupID", "Test_group");
//
Consumerconfig consumerconfig = new Consumerconfig (props);
Consumerconnector connector = consumer.create (consumerconfig);
//
map<string, list<messagestream<string>>> topicmessagestreams = Connector.createMessageStreams ( Immutablemap.of ("Demo", 2), New Stringdecoder ());
list<messagestream<string>> streams = Topicmessagestreams.get ("demo");
//
Executorservice executor = Executors.newfixedthreadpool (2);
Final Atomicinteger count = new Atomicinteger ();
For (final messagestream<string> stream:streams) {
Executor.submit (New Runnable () {

public void Run () {
for (String Message:stream) {
System.out.println (Count.incrementandget () + "= +" + message);
}
}
});
}
//
Executor.awaittermination (1, timeunit.hours);
}

All messages are consumed in almost the same way, except for the topic name of the consumer.

It's not easy, just try it.

Https://github.com/adyliu/jafka/wiki/quickstart.zh_CN

Http://www.blogjava.net/xylz/archive/2012/05/11/377938.html

Distributed messaging system Jafka fast Start (RPM)

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.