-
Notifications
You must be signed in to change notification settings - Fork 3
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
[BE] fix: 외부 메시지 큐(RabbitMQ) 도입, 분산 환경 채팅 안 되던 버그 수정 #630
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d59ab0
fix: RabbitMQ 도입 -> 분산 환경에서 채팅 가능하도록 구현
takoyakimchi 4653d6a
fix: 누락된 의존성 추가
takoyakimchi 70caeba
fix: converAndSend 시그니처 변경
takoyakimchi 8a618ba
refactor: RabbitMQ 포트 변경
takoyakimchi 2577ee1
chore: CD 스크립트에 RabbitMQ 관련 환경변수 추가
takoyakimchi ce25dc9
refactor: dev 환경에서 포트 default로 변경
takoyakimchi 007feeb
chore: dev CD 스크립트 환경변수 주입
takoyakimchi cb9d792
refactor: 클래스명 rename
takoyakimchi d057381
fix: 인터페이스 bean 등록 제거
takoyakimchi 6f456ce
Merge branch 'develop' into feature/#616
takoyakimchi 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
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/happy/friendogly/chat/config/ChatTemplate.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,6 @@ | ||
package com.happy.friendogly.chat.config; | ||
|
||
public interface ChatTemplate { | ||
|
||
void convertAndSend(Long chatRoomId, Object payload); | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/com/happy/friendogly/chat/config/InMemoryChatTemplate.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,23 @@ | ||
package com.happy.friendogly.chat.config; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Profile("local") | ||
public class InMemoryChatTemplate implements ChatTemplate { | ||
|
||
private static final String TOPIC_CHAT_PREFIX = "/exchange/chat.exchange/room."; | ||
|
||
private final SimpMessagingTemplate template; | ||
|
||
public InMemoryChatTemplate(SimpMessagingTemplate template) { | ||
this.template = template; | ||
} | ||
|
||
@Override | ||
public void convertAndSend(Long chatRoomId, Object payload) { | ||
template.convertAndSend(TOPIC_CHAT_PREFIX + chatRoomId, payload); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/happy/friendogly/chat/config/RabbitChatTemplate.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,21 @@ | ||
package com.happy.friendogly.chat.config; | ||
|
||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Profile("!local") | ||
public class RabbitChatTemplate implements ChatTemplate { | ||
|
||
private final RabbitTemplate rabbitTemplate; | ||
|
||
public RabbitChatTemplate(RabbitTemplate rabbitTemplate) { | ||
this.rabbitTemplate = rabbitTemplate; | ||
} | ||
|
||
@Override | ||
public void convertAndSend(Long chatRoomId, Object payload) { | ||
rabbitTemplate.convertAndSend("chat.exchange", "room." + chatRoomId, payload); | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
backend/src/main/java/com/happy/friendogly/config/RabbitMqConfig.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,108 @@ | ||
package com.happy.friendogly.config; | ||
|
||
import com.fasterxml.jackson.databind.Module; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | ||
import com.rabbitmq.client.ConnectionFactory; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import org.springframework.amqp.core.AmqpAdmin; | ||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.core.TopicExchange; | ||
import org.springframework.amqp.rabbit.annotation.EnableRabbit; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitAdmin; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Configuration | ||
@EnableRabbit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 이런게 있군용 |
||
@Profile("!local") | ||
public class RabbitMqConfig { | ||
|
||
private static final String CHAT_QUEUE_NAME = "chat.queue"; | ||
private static final String CHAT_EXCHANGE_NAME = "chat.exchange"; | ||
private static final String ROUTING_KEY = "*.room.*"; | ||
|
||
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | ||
|
||
@Value("${spring.rabbitmq.host}") | ||
private String host; | ||
|
||
@Value("${spring.rabbitmq.username}") | ||
private String username; | ||
|
||
@Value("${spring.rabbitmq.password}") | ||
private String password; | ||
|
||
@Value("${spring.rabbitmq.stomp-port}") | ||
private int port; | ||
|
||
@Bean | ||
public Queue queue() { | ||
return new Queue(CHAT_QUEUE_NAME, true); | ||
} | ||
|
||
@Bean | ||
public TopicExchange topicExchange() { | ||
return new TopicExchange(CHAT_EXCHANGE_NAME, true, false); | ||
} | ||
|
||
@Bean | ||
public Binding binding(Queue queue, TopicExchange topicExchange) { | ||
return BindingBuilder.bind(queue) | ||
.to(topicExchange) | ||
.with(ROUTING_KEY); | ||
} | ||
|
||
@Bean | ||
public ConnectionFactory connectionFactory() { | ||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); | ||
connectionFactory.setHost(host); | ||
connectionFactory.setPort(port); | ||
connectionFactory.setUsername(username); | ||
connectionFactory.setPassword(password); | ||
connectionFactory.setVirtualHost("/"); | ||
return connectionFactory.getRabbitConnectionFactory(); | ||
} | ||
|
||
@Bean | ||
public AmqpAdmin amqpAdmin(RabbitTemplate rabbitTemplate) { | ||
RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory()); | ||
rabbitAdmin.declareExchange(topicExchange()); | ||
return rabbitAdmin; | ||
} | ||
|
||
@Bean | ||
public RabbitTemplate rabbitTemplate( | ||
org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory) { | ||
|
||
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); | ||
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter()); | ||
return rabbitTemplate; | ||
} | ||
|
||
@Bean | ||
public Jackson2JsonMessageConverter jackson2JsonMessageConverter() { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
objectMapper.registerModule(javaTimeModule()); | ||
return new Jackson2JsonMessageConverter(objectMapper); | ||
} | ||
|
||
@Bean | ||
public Module javaTimeModule() { | ||
return new JavaTimeModule() | ||
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(TIME_FORMAT)) | ||
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(TIME_FORMAT)); | ||
} | ||
Comment on lines
+94
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기본 설정만으로는 LocalDateTime을 직렬화하지 못해서 작성함 |
||
} |
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
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.
일대일 채팅방 만들때 다시 로직 살릴예정!