kafka Windows客户端Linux服务器---转
原文:http://blog.csdn.net/jingshuigg/article/details/25001979
一、对于服务器端的搭建可以参考上一篇文章:kafka单机版环境搭建与测试
服务器端IP :10.0.30.221
运行环境的目录如下:
需要改动config文件夹下的server.properties中的以下两个属性
zookeeper.connect=localhost:2181改成zookeeper.connect=00.00.00.01 (IP地址):2181
以及默认注释掉的
#host.name=localhost改成host.name=00.00.00.01 (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)
上述步骤完成以后,按照《kafka单机版环境搭建与测试》中的方法启动zookeeper-server和kafka-server即可
二、客户端搭建
客户端使用的win7系统,在Eclipse中连接服务器
1.在eclipse下新建工程kafka_producer,目录如下:
注意:将config文件夹下的log4j.properties文件放在src下,这样才起作用,可以观测到日志信息
producer的代码如下:
import java.util.Properties;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig; public class Producer extends Thread{
private final kafka.javaapi.producer.Producer<Integer, String> producer;
private final String topic;
private final String name;
private final int numsOfMessage;
private final Properties props = new Properties(); public Producer(String name,String topic,int numsOfMessage){
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("metadata.broker.list", "10.0.30.221:9092");
//异步发送
//props.put("producer.type", "async");
//每次发送多少条
//props.put("batch.num.messages", "100");
producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props));
this.topic = topic;
this.name = name;
this.numsOfMessage = numsOfMessage;
} public void run() {
int messageNo = 1;
while(messageNo <= numsOfMessage) { //每个生产者生产的消息数;
String message = new String(name+"'s Message_" + messageNo+"******");
KeyedMessage<Integer, String> messageForSend = new KeyedMessage<Integer, String>(topic, message);
producer.send(messageForSend);
messageNo++;
}
producer.close();
}
}
启动producer的代码如下:
import java.util.concurrent.TimeUnit;
public class KafkaProducerDemo implements KafkaProperties{
public static void main(String[] args){
StartThread(1,"testTopic",10);
}
/**
* @param numsOfProducer 生产者的数目
* @param topic 消息的主题
* @param numsOfMessage 每个生产者生产的消息树
* @return
*/
public static void StartThread(int numsOfProducer,String topic,int numsOfMessage){
for(int i = 1; i <= numsOfProducer; i ++ ){
String name = "Producer" + i;
new Producer(name,topic,numsOfMessage).start();
}
}
}
2.在eclipse下新建kafka_consumer工程,目录如下:
consumer代码如下:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector; public class Consumer extends Thread {
private final ConsumerConnector consumer;
private final String topic;
private final String name; public Consumer(String name,String topic){
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig());
this.topic = topic;
this.name = name;
} private static ConsumerConfig createConsumerConfig(){
Properties props = new Properties();
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");
//每次最少接收的字节数,默认是1
//props.put("fetch.min.bytes", "1024");
//每次最少等待时间,默认是100
//props.put("fetch.wait.max.ms", "600000");
return new ConsumerConfig(props);
} 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("************"+name+" gets "+new String(it.next().message()));
}
}
}
启动consumer的代码:
public class KafkaConsumerDemo implements KafkaProperties
{
public static void main(String[] args){
//Consumer1
Consumer consumerThread1 = new Consumer("Consumer1",KafkaProperties.topic); consumerThread1.start();
}
}
properties的代码(为了传递属性值,当然也可以是xml提供属性值):
public interface KafkaProperties{
final static String zkConnect = "10.0.30.221:2181";
final static String groupId = "group1";
final static String topic = "testTopic";
final static String kafkaServerURL = "10.0.30.221";
final static int kafkaServerPort = 9092;
final static int kafkaProducerBufferSize = 64*1024;
final static int connectionTimeOut = 100000;
final static int reconnectInterval = 10000;
final static String clientId = "SimpleConsumerDemoClient";
}
3.启动consumer然后再启动producer,则在eclipse的Console窗口中观察到:
kafka Windows客户端Linux服务器---转的更多相关文章
- Windows客户端 Linux服务器通讯 字符编码问题
Windows下的字符编码默认是gb2312 在Linux下需要转成utf8 才能正确的看到对应的中文编码 提供转换函数 /*------------------------------------- ...
- Java实现windows,linux服务器word,excel转为PDF;aspose-words,Documents4j
Java实现windows,linux服务器word,excel转为PDF:aspose-words,Documents4j 一.通过aspose-words将word,Excel文档转为PDF 1. ...
- Windows远程linux服务器执行shell命令
一.前言 借用百度百科关于putty的描述:PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件.较早的版本仅支持Windows平台,在最近的版本中开始支持各类Unix平台 ...
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
转自:http://www.linuxidc.com/Linux/2012-05/60966.htm 在工作中,每次部署应用时都需要从本机Windows服务器拷贝文件到Linux上,有时还将Linux ...
- 使用pscp实现Windows 和 Linux 服务器间的远程拷贝文件
在工作中,每次部署应用时都需要从本机Windows 服务器拷贝文件到Linux 上,有时还将Linux 上的文件拷到本机,这些操作都是可以使用pscp实现的.下文将详细描述如何使用: PSCP (Pu ...
- Windows连接Linux服务器中MySQL数据库-权限配置
问题描述 在Windows系统中安装了监控MySQL数据库服务器性能的工具Spotlight on MySQL,利用Spotlight连接Linux服务器中的MySQL,进行相关配置如下: 点击& ...
- Windows连接Linux服务器远程开发解决方案
解决方案 vscode+Linux服务器 解决连接问题 vscode商店下载remote-ssh工具,然后进行配置. 这个网上依旧有很多详细的教程了,这里就不过多赘述. 配置免密登录 这一部分是我要重 ...
- 记crt 在windows与linux服务器之间利用ftp进行文件的上传下载
我们首先来熟悉一些相关的命令以及操作: ls #展示linux服务器当前工作目录[你连接sftp时所处的目录]下的所有文件以及文件夹 lcd F:\niki #绑定你在windo ...
- .Net Core 在Linux服务器下部署程序--(1). Windows 连接 Linux服务器
下载Linux服务器连接软件,市面上有Putty,FinalShell等,我以FinalShell为例,下载地址为 :http://www.hostbuf.com/t/988.html,软件安装结束后 ...
随机推荐
- [Leetcode] Validate BST
给一个Binary Tree,检查是不是Binary Search Tree. 即是否满足对每个节点,左子树的中的所有节点的值 < 当前节点的值 < 右子树所有节点的值. Solution ...
- BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间
Description 贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量.为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1. Farmer ...
- [原博客] POI系列(1)
正规.严谨.精妙. -POI 发现POI(波兰信息学奥赛)的题都很有意思.于是开刷bzoj上的poi题目(按ac人数降序..).顺手写一写题解,加深印象. 为了防止一篇文章过于长,打算每五道题另起一篇 ...
- hdu 1269
强连通分量题,用tarjin算法: 这是一道很简单的tarjin算法题,基本上就是套模板: 贴代码: #include<cstdio> #include<vector> #in ...
- cisco telnet会话SESSION管理及相关Dynagen配置文件
#Lab 2-5 autostart = False [localhost] [[2621]] ram = 64 image = C:\Program Files (x86)\Dynamips\ima ...
- 基于Delphi的Socket I/O模型全接触 good
老陈有一个在外地工作的女儿,不能经常回来,老陈和她通过信件联系.他们的信会被邮递员投递到他们的信箱里. 这和Socket模型非常类似.下面我就以老陈接收信件为例讲解Socket I/O模型. 一:se ...
- wcf中netTcpBinding的元素构成
<security> of <netTcpBinding> <transport> of <netTcpBinding> <message> ...
- ORA-32001: write to SPFILE requested but no SPFILE specified at startup
SQL> alter system set sga_max_size=2048M scope=spfile; alter system set sga_max_size=2048M scop ...
- 【转】我的电脑最近忽然开不了机,启动修复也无法修复,win7系统。开机的时候如果不点启动修复直接正常启动
原文网址:http://wenda.haosou.com/q/1356139178064356 你好,电脑开机蓝屏主要是:“磁盘有错误”或“非正常关机”引起!这是解决方法:(原创,引用请说明作者:力王 ...
- (转载)Javascript操作表单之间的数据传递
(转载)http://www.aspxhome.com/javascript/skills/200710/214825.htm 今天有朋友问我关于用JAVASCRIPT来进行页面各表单之间的数据传递的 ...