-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] RabbitMQ Redis STOMP 세팅 및 도커 추가
- Loading branch information
Showing
14 changed files
with
371 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.DS_Store | ||
backend/src/main/resources/application-env.yml | ||
|
||
.env |
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,4 @@ | ||
FROM gradle:7.5.1-jdk17 | ||
WORKDIR /app | ||
COPY . /app | ||
CMD ["gradle", "bootRun"] |
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
65 changes: 65 additions & 0 deletions
65
backend/src/main/java/com/twtw/backend/config/rabbitmq/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,65 @@ | ||
package com.twtw.backend.config.rabbitmq; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.twtw.backend.global.properties.RabbitMQProperties; | ||
import lombok.RequiredArgsConstructor; | ||
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.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@EnableRabbit | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RabbitMQConfig { | ||
|
||
private static final String CHAT_QUEUE_NAME = "map.queue"; | ||
private static final String CHAT_EXCHANGE_NAME = "map.exchange"; | ||
private static final String ROUTING_KEY = "party.*"; | ||
private final RabbitMQProperties rabbitMQProperties; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Bean | ||
public Queue queue() { | ||
return new Queue(CHAT_QUEUE_NAME, true); | ||
} | ||
|
||
@Bean | ||
public TopicExchange topicExchange() { | ||
return new TopicExchange(CHAT_EXCHANGE_NAME); | ||
} | ||
|
||
@Bean | ||
public Binding binding(final Queue queue, final TopicExchange exchange) { | ||
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY); | ||
} | ||
|
||
@Bean | ||
public RabbitTemplate rabbitTemplate() { | ||
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); | ||
rabbitTemplate.setMessageConverter(jsonMessageConverter()); | ||
rabbitTemplate.setRoutingKey(CHAT_QUEUE_NAME); | ||
return rabbitTemplate; | ||
} | ||
|
||
@Bean | ||
public ConnectionFactory connectionFactory() { | ||
CachingConnectionFactory factory = new CachingConnectionFactory(); | ||
factory.setHost(rabbitMQProperties.getHost()); | ||
factory.setUsername(rabbitMQProperties.getUsername()); | ||
factory.setPassword(rabbitMQProperties.getPassword()); | ||
return factory; | ||
} | ||
|
||
@Bean | ||
public Jackson2JsonMessageConverter jsonMessageConverter() { | ||
return new Jackson2JsonMessageConverter(objectMapper); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/com/twtw/backend/config/redis/RedisConfig.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.twtw.backend.config.redis; | ||
|
||
import com.twtw.backend.global.properties.RedisProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@EnableRedisRepositories | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
private static final Long TIME_TO_LIVE = 1L; | ||
private final RedisProperties redisProperties; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort()); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
final RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setEnableTransactionSupport(false); | ||
return redisTemplate; | ||
} | ||
|
||
@Bean | ||
public CacheManager cacheManager(final RedisConnectionFactory cf) { | ||
final RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) | ||
.entryTtl(Duration.ofHours(TIME_TO_LIVE)); | ||
|
||
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf).cacheDefaults(redisCacheConfiguration).build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/twtw/backend/config/socket/StompConfig.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,33 @@ | ||
package com.twtw.backend.config.socket; | ||
|
||
import com.twtw.backend.global.properties.RabbitMQProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
@EnableWebSocketMessageBroker | ||
public class StompConfig implements WebSocketMessageBrokerConfigurer { | ||
private final RabbitMQProperties rabbitMQProperties; | ||
|
||
@Override | ||
public void registerStompEndpoints(final StompEndpointRegistry registry) { | ||
registry.addEndpoint("/socket") | ||
.setAllowedOrigins("*"); | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(final MessageBrokerRegistry registry) { | ||
registry.enableStompBrokerRelay("/topic", "/queue", "/exchange", "/amq/queue") | ||
.setRelayHost(rabbitMQProperties.getHost()) | ||
.setRelayPort(61613) | ||
.setClientPasscode(rabbitMQProperties.getPassword()) | ||
.setClientLogin(rabbitMQProperties.getUsername()); | ||
|
||
registry.setApplicationDestinationPrefixes("/pub"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/twtw/backend/global/properties/RabbitMQProperties.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,15 @@ | ||
package com.twtw.backend.global.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties(prefix = "spring.rabbitmq") | ||
public class RabbitMQProperties { | ||
private final String host; | ||
private final Integer port; | ||
private final String username; | ||
private final String password; | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/twtw/backend/global/properties/RedisProperties.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,13 @@ | ||
package com.twtw.backend.global.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties(prefix = "spring.data.redis") | ||
public class RedisProperties { | ||
private final String host; | ||
private final Integer port; | ||
} |
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,31 @@ | ||
server: | ||
servlet: | ||
context-path: /api/v1 | ||
spring: | ||
datasource: | ||
driver-class-name: org.mariadb.jdbc.Driver | ||
url: jdbc:mariadb://${MYSQL_HOST}:${MYSQL_PORT}/${SCHEMA}?serverTimezone=Asia/Seoul | ||
username: ${MYSQL_USER} | ||
password: ${MYSQL_ROOT_PASSWORD} | ||
jpa: | ||
database: mysql | ||
generate-ddl: true | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: create | ||
main: | ||
allow-bean-definition-overriding: true | ||
config: | ||
import: | ||
- classpath:application-env.yml | ||
rabbitmq: | ||
host: rabbitmq | ||
port: 5672 | ||
cache: | ||
type: redis | ||
redis: | ||
cache-null-values: true | ||
data: | ||
redis: | ||
host: redis | ||
port: 6379 |
Oops, something went wrong.