springBoot连接kafka——JavaDemo
1.服务器配置kafka,进入kafka bin目录下,修改server.propertiesvi config/server.properties主要修改地方:修改运行地址地址,改为虚拟机ip# The address the socket server listens on. It will get the value returned from# java.net.InetAddress.g
1.服务器配置kafka,进入kafka bin目录下,修改server.properties
vi config/server.properties
主要修改地方:
修改运行地址地址,改为虚拟机ip
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
# host.name=192.168.217.142 192.168.217.142为虚拟机服务ip,不修改java连接会报:
# java.net.ConnectException: Connection refused: no further information
listeners=PLAINTEXT://192.168.217.142:9092
修改kafka日志
log.dirs=/opt/kafka/kafka_2.12-2.5.0/logs
2.启动kafka
bin/kafka-server-start.sh config/server.properties
查看是否启动成功
ps ef|grep kafka
查看进程
jps -l
3.查看防火墙是否开启,关闭防火墙(否则客户端无法访问)
firewall-cmd --state
如果是running的话
关闭防火墙(centos7关闭firewalld)
systemctl stop firewalld.service
Windows使用telnet测试端口能否访问
telnet 192.168.217.142 9092 如果可以进入界面就说明可以访问
4.java编写消息生产者
package com.qiming.kafka.chapter1;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class ProducerFastStart {
// 主题名称-之前已经创建
private static final String topic = "heima-per";
// Kafka集群地址
private static final String brokerList = "192.168.217.142:9092";
public static void main(String[] args) {
Properties properties = new Properties();
// 设置key序列化器
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//设置重试次数
properties.put(ProducerConfig.RETRIES_CONFIG,10);
// 设置值序列化器
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//设置集群地址
properties.put("bootstrap.servers", brokerList);
KafkaProducer<String,String> producer = new KafkaProducer<String,String>(properties);
ProducerRecord<String,String> producerRecord = new ProducerRecord<String,String>(topic,"kafka-demo","hello.kafka 23!!");
try {
producer.send(producerRecord);
} catch (Exception e) {
e.printStackTrace();
}
producer.close();
}
}
5.java编写消息消费者
package com.qiming.kafka.chapter1;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
/**
* Kafka 消息消费者
*/
public class ConsumerFastStart {
// Kafka集群地址
private static final String brokerList = "192.168.217.142:9092";
// 主题名称-之前已经创建
private static final String topic = "heima-per";
// 消费组
private static
final String groupId = "group.demo";
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
properties.put("bootstrap.servers", brokerList);
properties.put("group.id", groupId);
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
consumer.subscribe(Collections.singletonList(topic));
while (true) {
ConsumerRecords<String, String> records =
consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
}
}
}
}
6.启动消费者,不断监听,再启动消息生产者,可以看到消费者日志打出hello.kafka 23!!
注意:一定要先启动kafka
更多推荐
所有评论(0)