Kafka原理解析-旧版本0.8高级Api的Demo和配置信息获取技巧

旧版本高级Api封装:

package xxxxxx;

import kafka.producer.KeyedMessage;

import kafka.producer.ProducerConfig;

import java.util.Properties;

public class KafkaProducerTest implements Runnable {

private final kafka.javaapi.producer.Producer<Integer, String> producer;

private final String topic;

private final Properties props = new Properties();

private final String message;

public KafkaProducerTest(String topic, String message) {

props.put("serializer.class", "kafka.serializer.StringEncoder");

props.put("metadata.broker.list", "127.0.0.1:9092");

// Use random partitioner. Don't need the key type. Just set it to Integer.

// The message is of type String.

producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props));

this.topic = topic;

this.message = message;

}

@Override

public void run() {

producer.send(new KeyedMessage<Integer, String>(topic, message));

System.out.println("发送kafka消息:"+message);

}

public static void main(String[] arg){

for(int icount=0;icount<100;icount++){

KafkaProducerTest kafkaProducer = new KafkaProducerTest("myTopic", "hello world!");

Thread thread = new Thread(kafkaProducer);

thread.start();

}

}

}

package xxxxxxxxx;

import kafka.consumer.ConsumerConfig;

import kafka.consumer.ConsumerIterator;

import kafka.consumer.KafkaStream;

import kafka.javaapi.consumer.ConsumerConnector;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

public class KafkaConsumerTest implements Runnable{

private final ConsumerConnector consumer;

private final String topic;

public KafkaConsumerTest(String topic)

{

consumer = kafka.consumer.Consumer.createJavaConsumerConnector(

createConsumerConfig());

this.topic = topic;

}

private static ConsumerConfig createConsumerConfig()

{

Properties props = new Properties();

props.put("zookeeper.connect", "127.0.0.1:2181");

props.put("group.id", "myGroup");

props.put("zookeeper.session.timeout.ms", "400");

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);

System.out.println("开始启动.....");

try {

KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);

ConsumerIterator<byte[], byte[]> it = stream.iterator();

while (it.hasNext()) {

System.out.println("消费信息:" + new String(it.next().message()));

}

}catch (Exception e){

e.printStackTrace();

}

}

public static void main(String [] arg){

KafkaConsumerTest kafkaConsumer = new KafkaConsumerTest("myTopic");

Thread thread = new Thread(kafkaConsumer);

thread.start();

}

}

注意配置信息:

1、pom.xml文件中增加plugin:

<!-- jetty -->

<plugin>

<groupId>org.mortbay.jetty</groupId>

<artifactId>jetty-maven-plugin</artifactId>

<version>7.6.10.v20130312</version>

<configuration>

<webAppSourceDirectory>${basedir}/webapp</webAppSourceDirectory>

<stopKey>stop</stopKey>

<stopPort>55855</stopPort>

<scanIntervalSeconds>0</scanIntervalSeconds>

<connectors>

<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">

<port>8085</port>

<maxIdleTime>60000</maxIdleTime>

</connector>

</connectors>

<webAppConfig>

<contextPath>/</contextPath>

</webAppConfig>

<systemProperties>

<systemProperty>

<name>global.config.path</name>

<value>${basedir}/src/main/resources/config</value>

</systemProperty>

<systemProperty>

<name>APPID</name>

<value>spider.mySpider</value>

</systemProperty>

</systemProperties>

</configuration>

</plugin>

2、读取配置文件信息的工具类:

package xxxxxx;

import com.alibaba.fastjson.JSON;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

public class ReadConfigUtil {

private static Properties prop;

private static String globalConfigPath = System.getProperty("global.config.path");

static {

prop = new Properties();

// 遍历所有.properties文件

File cfgFiles = new File(globalConfigPath);

if (cfgFiles.exists()) {

File[] fs = cfgFiles.listFiles();

for (File confFile : fs) {

try {

String confName = confFile.getName();

if (confName.endsWith(".properties")) {

FileInputStream fis = new FileInputStream(confFile);

prop.load(fis);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static Properties getProp() {

return prop;

}

public static String getValueByKey(String key) {

return getProp().getProperty(key);

}

}

kafka版本引用:

<!-- Kafka start -->

<dependency>

<groupId>org.apache.kafka</groupId>

<artifactId>kafka_2.11</artifactId>

<version>0.8.2.1</version>

<exclusions>

<exclusion>

<artifactId>slf4j-log4j12</artifactId>

<groupId>org.slf4j</groupId>

</exclusion>

</exclusions>

</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值