-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 카프카 설정 및 사용자 설정 정보값 / Median 테이블 전송 (#81)
feat: 카프카 설정 및 사용자 설정 정보값 / Median 테이블 전송 (#81)
- Loading branch information
Showing
23 changed files
with
415 additions
and
60 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
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
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,22 @@ | ||
version: '3' | ||
|
||
services: | ||
zookeeper: | ||
container_name: zookeeper | ||
image: wurstmeister/zookeeper | ||
expose: | ||
- "2181" | ||
|
||
kafka: | ||
container_name: kafka | ||
image: wurstmeister/kafka:latest | ||
depends_on: | ||
- "zookeeper" | ||
ports: | ||
- "9092:9092" | ||
environment: | ||
KAFKA_ADVERTISED_HOST_NAME: localhost | ||
KAFKA_ADVERTISED_PORT: 9092 | ||
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock |
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
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
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
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
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
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
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
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
2 changes: 1 addition & 1 deletion
2
...ither/userservice/util/CalculateUtil.java → ...serservice/global/util/CalculateUtil.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
86 changes: 86 additions & 0 deletions
86
user-service/src/main/java/com/waither/userservice/kafka/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,86 @@ | ||
package com.waither.userservice.kafka; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
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.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.*; | ||
import org.springframework.kafka.listener.ContainerProperties; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@EnableKafka | ||
@Configuration | ||
public class KafkaConfig { | ||
|
||
@Value("${spring.kafka.bootstrap-servers}") | ||
private String bootstrapServer; | ||
|
||
@Value("${spring.kafka.consumer.group-id}") | ||
private String groupId; | ||
|
||
// Producer 관련 설정 | ||
private <T> ProducerFactory<String, T> producerFactory(Class<T> valueClass) { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer); | ||
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
|
||
if (valueClass == String.class) { | ||
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
} else { | ||
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); | ||
} | ||
|
||
return new DefaultKafkaProducerFactory<>(config); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, Object> kafkaTemplate() { | ||
return new KafkaTemplate<>(producerFactory(Object.class)); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, String> kafkaStringTemplate() { | ||
return new KafkaTemplate<>(producerFactory(String.class)); | ||
} | ||
|
||
// Consumer 관련 설정 | ||
private <T> ConsumerFactory<String, T> consumerFactory(Class<T> valueClass) { | ||
HashMap<String, Object> config = new HashMap<>(); | ||
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer); | ||
config.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
|
||
return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(), | ||
new JsonDeserializer<>(valueClass)); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(consumerFactory(Object.class)); | ||
factory.setConcurrency(3); | ||
factory.setBatchListener(true); | ||
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.BATCH); | ||
return factory; | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaStringListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(consumerFactory(String.class)); | ||
factory.setConcurrency(3); | ||
factory.setBatchListener(true); | ||
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.BATCH); | ||
return factory; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
user-service/src/main/java/com/waither/userservice/kafka/KafkaConsumer.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,24 @@ | ||
//package com.waither.userservice.kafka; | ||
// | ||
//import lombok.RequiredArgsConstructor; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.springframework.kafka.annotation.KafkaListener; | ||
//import org.springframework.stereotype.Component; | ||
// | ||
//@Slf4j | ||
//@Component | ||
//@RequiredArgsConstructor | ||
//public class KafkaConsumer { | ||
// | ||
// // @KafkaListener 통해 topic, group 구독 | ||
// @KafkaListener(topics = "${spring.kafka.template.topic}", groupId = "${spring.kafka.consumer.group-id}") | ||
// public void consume(KafkaMessage message) { | ||
// log.info("[*] Kafka Consumer(Object) Message : {} ", message); | ||
// } | ||
// | ||
// // @KafkaListener(topics = "${spring.kafka.template.default-topic}", groupId = "${spring.kafka.consumer.group-id}") | ||
// // public void consume(String message) { | ||
// // log.info("[*] Kafka Consumer(String) Message : {} ", message); | ||
// // } | ||
//} | ||
// |
48 changes: 48 additions & 0 deletions
48
user-service/src/main/java/com/waither/userservice/kafka/KafkaConverter.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,48 @@ | ||
package com.waither.userservice.kafka; | ||
|
||
import com.waither.userservice.entity.UserMedian; | ||
import com.waither.userservice.entity.Setting; | ||
import com.waither.userservice.entity.User; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class KafkaConverter { | ||
|
||
public static KafkaDto.InitialDataDto toInitialData(User user, Setting setting, UserMedian userMedian | ||
) { | ||
return KafkaDto.InitialDataDto.builder() | ||
.nickName(user.getNickname()) | ||
.climateAlert(setting.isClimateAlert()) | ||
.userAlert(setting.isUserAlert()) | ||
.snowAlert(setting.isSnowAlert()) | ||
.windAlert(setting.isWindAlert()) | ||
.windDegree(setting.getWindDegree()) | ||
.regionReport(setting.isRegionReport()) | ||
.weight(setting.getWeight()) | ||
.medianOf1And2(userMedian.getMedianOf1And2()) | ||
.medianOf2And3(userMedian.getMedianOf2And3()) | ||
.medianOf3And4(userMedian.getMedianOf3And4()) | ||
.medianOf4And5(userMedian.getMedianOf4And5()) | ||
.build(); | ||
} | ||
|
||
public static KafkaDto.UserMedianDto toUserMedianDto(User user, UserMedian userMedian) { | ||
List<Map<String, Double>> medians = Arrays.asList( | ||
Map.of("medianOf1And2", userMedian.getMedianOf1And2()), | ||
Map.of("medianOf2And3", userMedian.getMedianOf2And3()), | ||
Map.of("medianOf3And4", userMedian.getMedianOf3And4()), | ||
Map.of("medianOf4And5", userMedian.getMedianOf4And5()) | ||
); | ||
return new KafkaDto.UserMedianDto(user.getId(), medians); | ||
} | ||
|
||
public static KafkaDto.UserSettingsDto toSettingDto(User user, String key, String value) { | ||
return new KafkaDto.UserSettingsDto(user.getId(), key, value); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
user-service/src/main/java/com/waither/userservice/kafka/KafkaDto.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,39 @@ | ||
package com.waither.userservice.kafka; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class KafkaDto { | ||
|
||
@Builder | ||
public record InitialDataDto( | ||
|
||
String nickName, | ||
boolean climateAlert, | ||
boolean userAlert, | ||
boolean snowAlert, | ||
boolean windAlert, | ||
Integer windDegree, | ||
boolean regionReport, | ||
Double weight, | ||
Double medianOf1And2, | ||
Double medianOf2And3, | ||
Double medianOf3And4, | ||
Double medianOf4And5 | ||
) {} | ||
|
||
public record UserMedianDto( | ||
Long userId, | ||
List<Map<String, Double>> medians | ||
|
||
) {} | ||
|
||
public record UserSettingsDto( | ||
Long userId, | ||
String key, | ||
String value | ||
) {} | ||
|
||
} |
Oops, something went wrong.