-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from yomankum-project/feature/88-kafka
feature: kafka 설정
- Loading branch information
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/account/yomankum/config/KafkaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.account.yomankum.config; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.annotation.EnableKafka; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@EnableKafka | ||
@Configuration | ||
public class KafkaConfig { | ||
|
||
@Value("${kafka.bootstrap-server}") | ||
private String bootstrapServer; | ||
@Value("${kafka.group-id}") | ||
private String groupId; | ||
@Value("${kafka.type-mappings}") | ||
private String typeMappings; | ||
|
||
@Bean | ||
public ConsumerFactory<String, Object> consumerFactory() { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer); | ||
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class); | ||
// Json 파싱 신뢰 패키지. 개발 환경에서만 * 하고 프로덕션 환경에서는 패키지 지정하기 -> 내 이벤트 패키지 | ||
props.put(JsonDeserializer.TRUSTED_PACKAGES, "*"); | ||
props.put(JsonDeserializer.TYPE_MAPPINGS, typeMappings); | ||
|
||
return new DefaultKafkaConsumerFactory<>( | ||
props, | ||
new StringDeserializer(), | ||
new ErrorHandlingDeserializer<>(new JsonDeserializer<>())); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(consumerFactory()); | ||
return factory; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/account/yomankum/kafka/AccountBookInputNotice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.account.yomankum.kafka; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record AccountBookInputNotice( | ||
Long id, | ||
String nickname, // 쓰기 시작하는 닉네임 | ||
String event, | ||
LocalDateTime eventDatetime | ||
) { | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/account/yomankum/kafka/KafkaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.account.yomankum.kafka; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
public class KafkaService { | ||
|
||
@KafkaListener(topics = "accountBook_input", groupId = "accountServiceConsumers") | ||
public void listenAccountNotifications(AccountBookInputNotice notice) { | ||
// 처리 로직 | ||
System.out.println("Received Account Notification: " + notice.nickname()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters