-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 카프카 설정 및 사용자 설정 정보값 / Median 테이블 전송 (#81) #88
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
65c12de
♻️ refactor: 로컬 yml 파일 삭제
jinho7 2ad5423
♻️ refactor: mysql
jinho7 fb64371
♻️ feature : kafka 테스트 의존성 추가
jinho7 850f3c3
♻️ refactor: @Configuration 어노테이션 변경
jinho7 b19921d
♻️ refactor: Util 패키지 구조 변경
jinho7 ce69ca5
✨ feature: 카프카 환경 설정
jinho7 ab7c879
♻️ refactor:
jinho7 5a49782
♻️ refactor: import SurveyRequestDto 패키지 구조 변
jinho7 17f155a
♻️ refactor: import calculateMedian 패키지 구조 변
jinho7 5c2eee4
♻️ refactor: 카프카 전송
jinho7 b3c88b4
♻️ refactor: JwtAuthorizationFilter.java 제거
jinho7 3a94ba5
♻️ refactor: .gitignore 수정
jinho7 74a4f92
🗑️delete : Delete user-service/data/mysql directory
DDonghyeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제네릭 타입 좋은 아이디어인 것 같아요~~~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다!!!
(엎드려 절받기.. 🙇♂️)