原文连接:http://www.cnblogs.com/davidwang456/p/4201875.html

程序仍然使用之前的一篇博文中的例子 :http://www.cnblogs.com/gnivor/p/4934265.html

这里是将producer和consumer与bocker分离

如何搭建Kafka集群见: http://www.cnblogs.com/gnivor/p/4934073.html

注意:不同的地方

需要改动config文件夹下的server.properties中的以下两个属性

zookeeper.connect=localhost:2181改成zookeeper.connect=192.168.1.164 (IP地址):2181

以及默认注释掉的

#host.name=localhost改成host.name=192.168.1.164 (IP地址) 注意:每个节点这一项要填自己的IP地址。

host.name不更改会造成客户端报如下的错误

Exception in thread "Thread-0" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)
at kafka.producer.Producer.send(Producer.scala:76)
at kafka.javaapi.producer.Producer.send(Producer.scala:33)
at its.kafka.Producer.run(Producer.java:46)

修改配置之后,启动zookeeper和kafka,然后分别在另外两台主机上运行producer/consumer

接口 KafkaProperties.java

public interface KafkaProperties {
final static String zkConnect = "192.168.1.160:2181";
final static String groupId = "group1";
final static String topic = "topic1";
// final static String kafkaServerURL = "192.168.1.160";
// final static int kafkaServerPort = 9092;
// final static int kafkaProducerBufferSize = 64 * 1024;
// final static int connectionTimeOut = 20000;
// final static int reconnectInterval = 10000;
// final static String topic2 = "topic2";
// final static String topic3 = "topic3";
// final static String clientId = "SimpleConsumerDemoClient";
}

生产者 KafkaProducer.java

public class KafkaProducer extends Thread {
private final kafka.javaapi.producer.Producer<Integer, String> producer;
private final String topic;
private final Properties props = new Properties(); public KafkaProducer(String topic) {
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("metadata.broker.list", "192.168.1.160:9092"); // 配置kafka端口
producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props));
this.topic = topic;
} @Override
public void run() {
int messageNo = 1;
while (true) {
String messageStr = new String("This is a message, number: " + messageNo);
System.out.println("Send:" + messageStr);
producer.send(new KeyedMessage<Integer, String>(topic, messageStr));
messageNo++;
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }

消费者 KafkaConsumer.java

import java.util.Properties;

import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector; public class KafkaConsumer extends Thread {
private final ConsumerConnector consumer;
private final String topic; public KafkaConsumer(String topic) {
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(createConsumerConfig());
this.topic = topic;
} private static ConsumerConfig createConsumerConfig() {
Properties props = new Properties();
props.put("zookeeper.connect", KafkaProperties.zkConnect); // zookeeper的地址
props.put("group.id", KafkaProperties.groupId); // 组ID //zk连接超时
props.put("zookeeper.session.timeout.ms", "40000");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "1000"); return new ConsumerConfig(props);
} @Override
public void run() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(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("receive:" + new String(it.next().message()));
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

执行函数 KafkaConsumerProducerDemo.java(注意 producer和consumer可以分开在不同的主机执行)

public class KafkaConsumerProducerDemo {
public static void main(String[] args) {
// KafkaProducer producerThread = new KafkaProducer(KafkaProperties.topic); //这里的topic为“topic1”
// producerThread.start();
//
// KafkaProducer producerThread2 = new KafkaProducer("topic2");
// producerThread2.start(); KafkaConsumer consumerThread = new KafkaConsumer(KafkaProperties.topic);
consumerThread.start(); KafkaConsumer consumerThread2 = new KafkaConsumer("topic2");
consumerThread2.start(); //两对生产者消费者,一对发送和接收topic1,一对发送和接收topic2
}
}

producer结果

Send topic1:This is a message, number: 1
Send topic2:This is a message, number: 1
Send topic2:This is a message, number: 2
Send topic1:This is a message, number: 2
Send topic1:This is a message, number: 3
Send topic2:This is a message, number: 3
Send topic1:This is a message, number: 4
Send topic2:This is a message, number: 4
Send topic1:This is a message, number: 5
Send topic2:This is a message, number: 5
Send topic1:This is a message, number: 6
Send topic2:This is a message, number: 6
Send topic2:This is a message, number: 7
Send topic1:This is a message, number: 7

consumer结果

receive topic1:This is a message, number: 1
receive topic2:This is a message, number: 1
receive topic1:This is a message, number: 2
receive topic2:This is a message, number: 2
receive topic1:This is a message, number: 3
receive topic2:This is a message, number: 3
receive topic1:This is a message, number: 4
receive topic2:This is a message, number: 4
receive topic1:This is a message, number: 5
receive topic2:This is a message, number: 5
receive topic1:This is a message, number: 6
receive topic2:This is a message, number: 6
receive topic1:This is a message, number: 7
receive topic2:This is a message, number: 7

Kafka笔记--使用ubuntu为bocker(服务器)windows做producer和comsumer(客户端)的更多相关文章

  1. 上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传)

    上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传) 最近在阿里云上面租了一个轻量级服务器玩玩,学习学习怎么在服务器部署网站.然后嘞,在想要将本地文件上传到服务器的时候,自己研究 ...

  2. Git初步配置 ubuntu服务器 windows客户端 虚拟机

    最近自己配置了一下Git,虽然网上相关的内容满天飞(ps:大多都差不多,很多都是直接转载,说的也比较乱),但是我还是碰到了很多问题,这里我就把我配置的步骤分享一下,遇到的问题也说一下,新手之间相互学习 ...

  3. ubuntu远程桌面连接windows系统(转)

    现在用ubuntu系统,公司买了个windows的服务器,需要给配置一套环境,来回跑很麻烦,就想windows下可以的远程桌面,linux应该也有. 现在自己的ubuntu13.10,无法进入桌面的& ...

  4. 【原生态跨平台:ASP.NET Core 1.0(非Mono)在 Ubuntu 14.04 服务器上一对一的配置实现-篇幅1】

    鸡冻人心的2016,微软高产年. build 2016后 各种干货层出不穷. 1 Win10 集成了bash  ,实现了纳德拉的成诺,Microsoft Love Linux!!! 2 跨平台  ,收 ...

  5. Ubuntu搭建Anki服务器

    Ubuntu搭建Anki服务器 第一步安装Anki 阿里云的服务器,xshell远程登录上 #以root用户进行操作 #安装Akni服务 easy_install AnkiServer #添加名为an ...

  6. 在 Ubuntu 14.04 服务器上部署 Hexo 博客

    版权声明:本文由宋秉金 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/241080001487926962 来源:腾云阁  ...

  7. ubuntu远程桌面连接windows系统

    现在用ubuntu系统,公司买了个windows的服务器,需要给配置一套环境,来回跑很麻烦,就想windows下可以的远程桌面,Linux应该也有. 现在自己的ubuntu13.10,无法进入桌面的“ ...

  8. 基于svnserve的SVN服务器(windows下安装与配置)

    基于svnserve的SVN服务器(windows下安装与配置) 基于svnserve的SVN服务器(windows下安装与配置)关键字: svn 安装SVNserve 从http://subvers ...

  9. Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程

    Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程 看了好多人的博客,有的不全 or 有问题,整理了一下,适合小白 新手先整理几个小问题 1.为啥使用 Linux 搭建服务器? ...

随机推荐

  1. quartz2.2.1-测试02-通过servlet动态修改定时任务运行时间

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  2. 8.2.1.15 ORDER BY Optimization ORDER BY 优化

    8.2.1.15 ORDER BY Optimization ORDER BY 优化 在一些情况下, MySQL 可以使用一个索引来满足一个ORDER BY 子句不需要做额外的排序 index 可以用 ...

  3. 【HDOJ】1494 跑跑卡丁车

    DP,将能量映射为0~14,注意当选择这圈加速的时候,这圈就不能再储存能量,同时能量14可能转化为10. #include <cstdio> #include <cstring> ...

  4. 【转】我的Android笔记(十)—— ProgressDialog的简单应用,等待提示

    原文网址:http://blog.csdn.net/barryhappy/article/details/7376231 在应用中经常会用到一些费时的操作,需要用户进行等待,比如加载网页内容…… 这时 ...

  5. C#中判断字符串是否中文的方法

    public bool IsChinaString(string CString) { bool BoolValue = false; ; i < CString.Length; i++) { ...

  6. Windows服务器Pyton辅助运维--01.自动Copy文件(文件夹)到远程服务器所在目录

    Windows服务器Pyton辅助运维 01.自动Copy文件(文件夹)到远程服务器所在目录 开发环境: u  Web服务器: Windows Server 2008 R2 SP1 IIS 7.5 u ...

  7. mysql命令行里的加载更多显示

    mysql> pager morePAGER set to 'more'mysql> pager lessPAGER set to 'less'mysql> nopagerPAGER ...

  8. magento产品eav笔记【持续跟新...】

    //magento把产品信息分在子表中,最顶上的表是catalog_product_entity,仅仅包含产品的信息(SKU) //表eav_attribute,这张表在magento里为全部不 同的 ...

  9. error C2143: 语法错误 : 缺少“;”(在“using”的前面)

    class JJMenuScene : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos ...

  10. 内部类之.this&&.new

    一..this 我们都知道this是指当前类中的对象本身,但是在内部类中需要指明外部类时,this不再起作用,那应该怎么做呢?下面,让我们看看: public class DotThis { void ...