-
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.
feature: configure redis with chat service
- Loading branch information
Showing
75 changed files
with
318 additions
and
171 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
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
Empty file modified
0
sns_service/src/main/kotlin/joryu/sns_service/SnsServiceApplication.kt
100644 → 100755
Empty file.
34 changes: 34 additions & 0 deletions
34
sns_service/src/main/kotlin/joryu/sns_service/channel/entity/Channel.kt
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,34 @@ | ||
package joryu.sns_service.channel.entity | ||
|
||
import jakarta.persistence.* | ||
import joryu.sns_service.channel.enums.ChannelType | ||
import java.io.Serializable | ||
import java.util.* | ||
|
||
|
||
@Table(name = "channel") | ||
@Entity | ||
class Channel( | ||
channelName: String = "", | ||
|
||
@Column(name = "channel_type") | ||
val channelType: ChannelType = ChannelType.PERSONAL | ||
) : Serializable { | ||
@Id | ||
val id: String = UUID.randomUUID().toString() | ||
|
||
@OneToMany(mappedBy = "channel") | ||
val channelUsers: MutableList<ChannelProfile> = mutableListOf() | ||
|
||
@Column(name = "channel_name") | ||
var channelName: String = channelName | ||
private set | ||
|
||
fun changeChannelName(newName: String) { | ||
channelName = newName | ||
} | ||
|
||
fun addUserToChannel(user: ChannelProfile) { | ||
channelUsers.add(user) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
sns_service/src/main/kotlin/joryu/sns_service/channel/entity/ChannelProfile.kt
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 joryu.sns_service.channel.entity | ||
|
||
import jakarta.persistence.* | ||
import joryu.sns_service.profile.entity.Profile | ||
import java.io.Serializable | ||
|
||
@Table(name = "channel_profile") | ||
@Entity | ||
class ChannelProfile( | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "channel_id") | ||
var channel: Channel? = null, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "profile_id") | ||
var profile: Profile? = null | ||
): Serializable { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0 | ||
} |
5 changes: 5 additions & 0 deletions
5
sns_service/src/main/kotlin/joryu/sns_service/channel/enums/ChannelType.kt
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,5 @@ | ||
package joryu.sns_service.channel.enums | ||
|
||
enum class ChannelType { | ||
PERSONAL, GROUP | ||
} |
7 changes: 7 additions & 0 deletions
7
sns_service/src/main/kotlin/joryu/sns_service/channel/repository/ChannelProfileRepository.kt
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,7 @@ | ||
package joryu.sns_service.channel.repository | ||
|
||
import joryu.sns_service.channel.entity.ChannelProfile | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface ChannelProfileRepository: JpaRepository<ChannelProfile, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
sns_service/src/main/kotlin/joryu/sns_service/channel/repository/ChannelRepository.kt
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,7 @@ | ||
package joryu.sns_service.channel.repository | ||
|
||
import joryu.sns_service.channel.entity.Channel | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface ChannelRepository: JpaRepository<Channel, String> { | ||
} |
17 changes: 0 additions & 17 deletions
17
sns_service/src/main/kotlin/joryu/sns_service/chat/config/ChatRedisConfig.kt
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
sns_service/src/main/kotlin/joryu/sns_service/chat/config/EmbeddedRedisConfig.kt
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,30 @@ | ||
package joryu.sns_service.chat.config | ||
|
||
import jakarta.annotation.PostConstruct | ||
import jakarta.annotation.PreDestroy | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import redis.embedded.RedisServer | ||
|
||
|
||
@Profile("local") // profile이 local일때만 활성화 | ||
@Configuration | ||
class EmbeddedRedisConfig { | ||
|
||
@Value("\${spring.data.redis.port}") | ||
private val redisPort = 6379 | ||
|
||
private var redisServer: RedisServer? = null | ||
|
||
@PostConstruct | ||
fun redisServer() { | ||
redisServer = RedisServer(redisPort) | ||
redisServer?.start() | ||
} | ||
|
||
@PreDestroy | ||
fun stopRedis() { | ||
redisServer?.stop() | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
sns_service/src/main/kotlin/joryu/sns_service/chat/config/RedisConfig.kt
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 joryu.sns_service.chat.config | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Primary | ||
import org.springframework.data.redis.connection.MessageListener | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.listener.ChannelTopic | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer | ||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer | ||
import org.springframework.data.redis.serializer.StringRedisSerializer | ||
|
||
|
||
/** | ||
* 채팅에 사용되는 redis 설정 관리 | ||
*/ | ||
@Configuration | ||
@EnableRedisRepositories | ||
class RedisConfig( | ||
@Value("\${spring.data.redis.port:6379}") | ||
private val port: Int, | ||
|
||
@Value("\${spring.data.redis.host:localhost}") | ||
private val host: String | ||
) { | ||
|
||
/** | ||
* ChannelTopic 에 발행된 메시지를 처리하는 Listner 들을 설정한다. | ||
*/ | ||
@Bean | ||
@Primary | ||
fun redisMessageListenerContainer( | ||
redisConnectionFactory: RedisConnectionFactory, | ||
chatMessageListenerAdapter: MessageListenerAdapter, | ||
@Qualifier("chatChannelTopic") chatChannelTopic: ChannelTopic | ||
): RedisMessageListenerContainer { | ||
val container = RedisMessageListenerContainer() | ||
container.setConnectionFactory(redisConnectionFactory) | ||
container.addMessageListener(chatMessageListenerAdapter, chatChannelTopic) | ||
return container | ||
} | ||
|
||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
val redisStandaloneConfiguration = RedisStandaloneConfiguration() | ||
redisStandaloneConfiguration.hostName = host | ||
redisStandaloneConfiguration.port = port | ||
return LettuceConnectionFactory(redisStandaloneConfiguration) | ||
} | ||
|
||
/** | ||
* RedisMessageListenerContainer 로부터 메시지를 전달받는다. | ||
* 메시지 처리 비즈니스 로직을 담은 Subscriber Bean 을 추가해준다. | ||
*/ | ||
@Bean | ||
fun chatMessageListenerAdapter(listener: MessageListener): MessageListenerAdapter { | ||
return MessageListenerAdapter(listener, "onMessage") | ||
} | ||
|
||
/** | ||
* 채팅 채널 토픽을 반환한다. | ||
*/ | ||
@Bean(value = ["chatChannelTopic"]) | ||
fun chatChannelTopic(): ChannelTopic { | ||
return ChannelTopic("chat") | ||
} | ||
|
||
/** | ||
* Redis 데이터에 접근하는 redisTemplate 를 반환한다. | ||
*/ | ||
@Bean | ||
fun redisTemplate(connectionFactory: RedisConnectionFactory): RedisTemplate<String, Any> { | ||
val redisTemplate = RedisTemplate<String, Any>() | ||
redisTemplate.connectionFactory = connectionFactory | ||
redisTemplate.keySerializer = StringRedisSerializer() | ||
redisTemplate.valueSerializer = Jackson2JsonRedisSerializer(String::class.java) | ||
return redisTemplate | ||
} | ||
} |
Empty file modified
0
sns_service/src/main/kotlin/joryu/sns_service/chat/config/WebSocketConfig.kt
100644 → 100755
Empty file.
29 changes: 0 additions & 29 deletions
29
sns_service/src/main/kotlin/joryu/sns_service/chat/controller/ChatController.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
sns_service/src/main/kotlin/joryu/sns_service/chat/controller/ChatMessageController.kt
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 joryu.sns_service.chat.controller | ||
|
||
import joryu.sns_service.chat.dto.ChatMessage | ||
import joryu.sns_service.chat.service.ChatPublisher | ||
import org.springframework.messaging.handler.annotation.MessageMapping | ||
import org.springframework.stereotype.Controller | ||
|
||
@Controller | ||
class ChatMessageController( | ||
private val chatPublisher: ChatPublisher | ||
) { | ||
|
||
/** | ||
* /pub/chat/message 로 들어오는 메시징을 처리한다. | ||
*/ | ||
@MessageMapping("/chat/message") | ||
fun sendMessage(message: ChatMessage) { | ||
|
||
chatPublisher.publish(message) | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
sns_service/src/main/kotlin/joryu/sns_service/chat/controller/GreetingController.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.