云服务器docker-compose部署kafka并编写ava使用kafka示例代码
时间: 2023-09-04 13:06:41 浏览: 170
详解使用docker搭建kafka环境
一、部署Kafka
1. 在云服务器上安装Docker和Docker Compose;
2. 创建一个文件夹,命名为kafka,进入该文件夹;
3. 创建docker-compose.yml文件,写入以下内容:
```
version: '3.7'
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
restart: always
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
container_name: kafka
restart: always
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: <云服务器IP>
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "test_topic:1:1"
depends_on:
- zookeeper
```
4. 执行以下命令启动Kafka:
```
docker-compose up -d
```
二、编写Java使用Kafka示例代码
1. 在本地开发环境中创建一个Java项目,并在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.5.0</version>
</dependency>
```
2. 编写一个生产者类Producer.java:
```
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class Producer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "<云服务器IP>:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 10; i++)
producer.send(new ProducerRecord<>("test_topic", Integer.toString(i), Integer.toString(i)));
producer.close();
}
}
```
3. 编写一个消费者类Consumer.java:
```
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.util.Collections;
import java.util.Properties;
public class Consumer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "<云服务器IP>:9092");
props.put("group.id", "test");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
Consumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("test_topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (org.apache.kafka.clients.consumer.ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
}
}
}
```
4. 分别编译Producer.java和Consumer.java,并在命令行中执行以下命令运行Producer:
```
java Producer
```
5. 在另一个命令行中执行以下命令运行Consumer:
```
java Consumer
```
6. 可以在Consumer的命令行中看到Producer发送的消息。
阅读全文