-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.patch
181 lines (181 loc) · 7.04 KB
/
kafka.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Index: docker-compose.yml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
--- /dev/null (date 1645911225805)
+++ b/docker-compose.yml (date 1645911225805)
@@ -0,0 +1,23 @@
+version: '2'
+services:
+ zookeeper:
+ image: confluentinc/cp-zookeeper:latest
+ environment:
+ ZOOKEEPER_CLIENT_PORT: 2181
+ ZOOKEEPER_TICK_TIME: 2000
+ ports:
+ - 2181:2181
+
+ kafka:
+ image: confluentinc/cp-kafka:latest
+ depends_on:
+ - zookeeper
+ ports:
+ - 9092:9092
+ environment:
+ KAFKA_BROKER_ID: 1
+ KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
+ KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
+ KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
+ KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
+ KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
Index: src/main/java/com/handson/sentiment/kafka/KafkaTopicConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/sentiment/kafka/KafkaTopicConfig.java b/src/main/java/com/handson/sentiment/kafka/KafkaTopicConfig.java
new file mode 100644
--- /dev/null (date 1645136593248)
+++ b/src/main/java/com/handson/sentiment/kafka/KafkaTopicConfig.java (date 1645136593248)
@@ -0,0 +1,31 @@
+package com.handson.sentiment.kafka;
+
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.kafka.core.KafkaAdmin;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Configuration
+public class KafkaTopicConfig {
+ public static final String APP_TOPIC = "twitter";
+
+ @Value(value = "${spring.kafka.bootstrap-servers}")
+ private String bootstrapAddress;
+
+ @Bean
+ public KafkaAdmin kafkaAdmin() {
+ Map<String, Object> configs = new HashMap<>();
+ configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
+ return new KafkaAdmin(configs);
+ }
+
+ @Bean
+ public NewTopic topic1() {
+ return new NewTopic(APP_TOPIC, 1, (short) 1);
+ }
+}
Index: src/main/java/com/handson/sentiment/kafka/KafkaConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/sentiment/kafka/KafkaConfig.java b/src/main/java/com/handson/sentiment/kafka/KafkaConfig.java
new file mode 100644
--- /dev/null (date 1645137334440)
+++ b/src/main/java/com/handson/sentiment/kafka/KafkaConfig.java (date 1645137334440)
@@ -0,0 +1,56 @@
+package com.handson.sentiment.kafka;
+
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.common.serialization.StringDeserializer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+import reactor.kafka.receiver.KafkaReceiver;
+import reactor.kafka.receiver.ReceiverOptions;
+import reactor.kafka.receiver.internals.ConsumerFactory;
+import reactor.kafka.receiver.internals.DefaultKafkaReceiver;
+import reactor.kafka.sender.KafkaSender;
+import reactor.kafka.sender.SenderOptions;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.handson.sentiment.kafka.KafkaTopicConfig.APP_TOPIC;
+
+@Configuration
+@Component
+public class KafkaConfig {
+
+ @Autowired
+ private KafkaProperties kafkaProperties;
+
+ @Bean("simpleProducer")
+ public KafkaSender<String,String> simpleProducer(){
+ Map<String,Object> configs = new HashMap<>();
+ configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
+ configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+ configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
+ SenderOptions<String,String> senderOptions = SenderOptions.<String,String>create(configs).maxInFlight(1024);
+ return KafkaSender.create(senderOptions);
+ }
+
+ @Bean
+ public KafkaReceiver kafkaReceiver(){
+
+ Map<String, Object> props = new HashMap<>();
+ props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
+ props.put(ConsumerConfig.GROUP_ID_CONFIG, "sentiment_group");
+ props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
+ props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
+ props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
+ props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
+
+ return new DefaultKafkaReceiver(ConsumerFactory.INSTANCE, ReceiverOptions.create(props).subscription(Collections.singleton(APP_TOPIC)));
+ }
+
+}
Index: src/main/java/com/handson/sentiment/kafka/AppKafkaSender.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/sentiment/kafka/AppKafkaSender.java b/src/main/java/com/handson/sentiment/kafka/AppKafkaSender.java
new file mode 100644
--- /dev/null (date 1645137334477)
+++ b/src/main/java/com/handson/sentiment/kafka/AppKafkaSender.java (date 1645137334477)
@@ -0,0 +1,31 @@
+package com.handson.sentiment.kafka;
+
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.kafka.sender.KafkaSender;
+import reactor.kafka.sender.SenderRecord;
+import reactor.kafka.sender.SenderResult;
+
+import java.util.UUID;
+
+@Component
+public class AppKafkaSender {
+
+ @Autowired
+ @Qualifier("simpleProducer")
+ private KafkaSender<String, String> kafkaSender;
+
+ public boolean send(String msg, String topic) {
+ ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, null, UUID.randomUUID().toString(),
+ msg);
+ Mono<SenderRecord<String, String, String>> mono = Mono.just(SenderRecord.create(record, null));
+ Flux<SenderResult<String>> res = kafkaSender.send(mono);
+ res.collectList().subscribe();
+
+ return true;
+ }
+}