Skip to content

Commit

Permalink
[FEAT] RabbitMQ Redis STOMP 세팅 및 도커 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Sep 18, 2023
1 parent f9f0cce commit a019b41
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
backend/src/main/resources/application-env.yml

.env
4 changes: 4 additions & 0 deletions backend/Dockerfile
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"]
17 changes: 17 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.1.2'
id 'io.spring.dependency-management' version '1.1.2'
id 'com.google.cloud.tools.jib' version '3.2.1'
id 'org.asciidoctor.jvm.convert' version '3.3.2'
}

jib {
from {
image = "openjdk:11-jre-slim"
}
to {
image = "ohksj77/twtw-backend"
tags = ["latest"]
}
container {
jvmFlags = ["-Xms128m", "-Xmx128m"]
}
}

group = 'com.twtw'
version = '0.0.1-SNAPSHOT'

Expand Down Expand Up @@ -37,6 +51,9 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-reactor-netty'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
runtimeOnly 'com.h2database:h2:1.4.200'
Expand Down
41 changes: 41 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,54 @@ version: "3"
services:
db:
image: mariadb:10
container_name: db
ports:
- 3306:3306
env_file: .env
environment:
TZ: Asia/Seoul
volumes:
- data_db:/var/lib/mysql
networks:
- backend
restart: always

rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
entrypoint: >
/bin/bash -c "
rabbitmq-server &
sleep 10 && rabbitmq-plugins enable rabbitmq_web_stomp &
tail -f /dev/null
"
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
ports:
- 5672:5672
- 15672:15672
- 61613:61613
volumes:
- data_rabbitmq:/rabbitmq
networks:
- backend
restart: always
tty: true

redis:
image: redis:latest
container_name: redis
ports:
- 6379:6379
networks:
- backend
restart: always
tty: true

networks:
backend:

volumes:
data_rabbitmq: null
data_db: null
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
Expand All @@ -17,6 +18,7 @@ public ObjectMapper objectMapper() {
return new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.setPropertyNamingStrategy(
new CompositePropertyNamingStrategy(
PropertyNamingStrategies.SNAKE_CASE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import com.twtw.backend.global.properties.KakaoProperties;
import com.twtw.backend.global.properties.NaverProperties;

import com.twtw.backend.global.properties.RabbitMQProperties;
import com.twtw.backend.global.properties.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({NaverProperties.class, KakaoProperties.class})
@EnableConfigurationProperties(
{NaverProperties.class,
KakaoProperties.class,
RedisProperties.class,
RabbitMQProperties.class})
public class PropertiesConfig {}
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);
}
}
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();
}
}
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");
}
}
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;
}
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;
}
11 changes: 11 additions & 0 deletions backend/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ spring:
config:
import:
- classpath:application-env.yml
rabbitmq:
host: localhost
port: 5672
cache:
type: redis
redis:
cache-null-values: true
data:
redis:
host: localhost
port: 6379
31 changes: 31 additions & 0 deletions backend/src/main/resources/application-prod.yml
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
Loading

0 comments on commit a019b41

Please sign in to comment.