Skip to content

Commit

Permalink
feat: 인프라 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
kor-Chipmunk committed Feb 13, 2024
1 parent d3abada commit 05037f1
Show file tree
Hide file tree
Showing 518 changed files with 4,757 additions and 2,220 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ SG(스마일게이트)에서 목표를 이루고(워너비)야 말겠다는 포
<tr align="center">
<td><B>강수아(Server)</B></td>
<td><B>김선재(Server)</B></td>
<td><B>박민주(Web)</B></td>
<td><B>정소연(Server)</B></td>
<td><B>박민주(Web)</B></td>
</tr>
<tr align="center">
<td>
Expand Down
27 changes: 3 additions & 24 deletions src/backend/alarm-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
# Build - https://findstar.pe.kr/2022/05/13/gradle-docker-cache/
FROM gradle:8.6.0-jdk17 as builder
WORKDIR /build

# 그레이들 파일이 변경되었을 때만 새로운 의존성 다운로드
COPY build.gradle settings.gradle /build/
COPY alarm-api/build.gradle /build/alarm-api/
RUN gradle build -x test --parallel --continue > /dev/null 2>&1 || true

# 빌더 이미지에서 애플리케이션 빌드
COPY . /build
RUN gradle build -x test --parallel

# Run
FROM openjdk:17-jdk-slim
WORKDIR /app

COPY --from=builder /build/alarm-api/build/libs/*.jar app.jar

ARG DOCKER_YML_FILE=src/main/resources/application.yml
COPY ${DOCKER_YML_FILE} application.yml

EXPOSE 28000

ENTRYPOINT ["java", "-jar", "app.jar"]
ARG JAR_FILE=build/libs/*.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
5 changes: 4 additions & 1 deletion src/backend/alarm-server/alarm-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ dependencies {

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

implementation 'org.springframework.kafka:spring-kafka'
// RabbitMQ
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.3'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.lalala.alarm.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
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.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

@Configuration
public class RabbitMQConfig {

private static final String EXCHANGE_NAME = "retry-exchange";
public static final String QUEUE = "mainQueue";
private static final String RETRY_QUEUE = "mainQueue.retry";
public static final String UNDELIVERED_QUEUE = "mainQueue.undelivered";
private static final String MAIN_ROUTING_KEY = "Routing_Key";

@Value("${rabbitmq.retry.delay-in-ms}")
private Integer retryDelay;

@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
return rabbitTemplate;
}

@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
messageHandlerMethodFactory.setMessageConverter(new MappingJackson2MessageConverter());
return messageHandlerMethodFactory;
}

@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
TopicExchange exchange() {
return new TopicExchange(EXCHANGE_NAME);
}

@Bean
Queue mainQueue() {
return QueueBuilder.durable(QUEUE)
.deadLetterExchange(EXCHANGE_NAME)
.deadLetterRoutingKey(RETRY_QUEUE)
.quorum()
.build();
}

@Bean
Queue retryQueue() {
return QueueBuilder.durable(RETRY_QUEUE)
.deadLetterExchange(EXCHANGE_NAME)
.deadLetterRoutingKey(MAIN_ROUTING_KEY)
.ttl(retryDelay)
.quorum()
.build();
}

@Bean
Queue undeliveredQueue() {
return QueueBuilder.durable(UNDELIVERED_QUEUE)
.quorum()
.build();
}

@Bean
Binding mainBinding(Queue mainQueue, TopicExchange exchange) {
return BindingBuilder.bind(mainQueue).to(exchange).with(MAIN_ROUTING_KEY);
}

@Bean
Binding retryBinding(Queue retryQueue, TopicExchange exchange) {
return BindingBuilder.bind(retryQueue).to(exchange).with(RETRY_QUEUE);
}

@Bean
Binding undeliveredBinding(Queue undeliveredQueue, TopicExchange exchange) {
return BindingBuilder.bind(undeliveredQueue).to(exchange).with(UNDELIVERED_QUEUE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lalala.alarm.dto;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Email {

private String subject;
private String recipient;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.lalala.alarm.listener;

import static com.lalala.alarm.config.RabbitMQConfig.QUEUE;
import static com.lalala.alarm.config.RabbitMQConfig.UNDELIVERED_QUEUE;

import com.lalala.alarm.dto.Email;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class RetryingRabbitListener {

@Autowired
private RabbitTemplate rabbitTemplate;

@Value("${rabbitmq.retry.count}")
private Integer retryCount;

@RabbitListener(queues = QUEUE)
public void primary(Email email, @Header(required = false, name = "x-death") Map<String, ?> xDeath) throws Exception {
log.info("Message read from Queue: " + email);
if (checkRetryCount(xDeath)) {
sendToUndelivered(email);
return;
}

throw new Exception("Random error");
}


private boolean checkRetryCount(Map<String, ?> xDeath) {

if (xDeath != null && !xDeath.isEmpty()) {
Long count = (Long) xDeath.get("count");
return count >= retryCount;
}

return false;
}

private void sendToUndelivered(Email email) {
log.warn("maximum retry reached, send message to the undelivered queue, msg: {}", email);
this.rabbitTemplate.convertAndSend(UNDELIVERED_QUEUE, email);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
server:
port: 8080

rabbitmq:
retry:
delay-in-ms: 1000
count: 3

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: "jdbc:mysql://host.docker.internal:29000/alarm"
url: "jdbc:mysql://localhost:29000/alarm"
username: root
password: admin

rabbitmq:
host: localhost
username: admin
password: admin
port: '28300'
listener:
simple:
default-requeue-rejected: 'false'

jpa:
database-platform: org.hibernate.dialect.MySQLDialect
Expand All @@ -21,9 +34,22 @@ spring:
use_sql_comments: true

logging:
pattern:
level: "%5p [%X{traceId:-},%X{spanId:-}]"
level:
org:
hibernate:
type:
descriptor:
sql: trace

management:
tracing:
sampling:
probability: 1.0
propagation:
consume: b3_single
produce: b3_multi
zipkin:
tracing:
endpoint: "http://localhost:43000/api/v2/spans"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AlarmApplicationTests {

@Test
void contextLoads() {
}

}
//@SpringBootTest
//class AlarmApplicationTests {
//
// @Test
// void contextLoads() {
// }
//
//}
11 changes: 6 additions & 5 deletions src/backend/alarm-server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
}

Expand All @@ -11,6 +11,7 @@ repositories {
bootJar {
enabled = false
}

jar {
enabled = true
}
Expand Down Expand Up @@ -42,13 +43,13 @@ subprojects {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// Lombok
compileOnly "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
compileOnly 'jakarta.annotation:jakarta.annotation-api:1.3.5'
}

tasks.named('test') {
useJUnitPlatform()
}

}
81 changes: 81 additions & 0 deletions src/backend/alarm-server/docker-compose-hub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
version: "3.9"
services:
alarm-app:
image: smilegatewannabe/alarm
restart: always
ports:
- 28000:28000
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://alarm-mysql:3306/user?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&serverTimezone=Asia/Seoul
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: admin
SPRING_DATASOURCE_DRIVER-CLASS-NAME: com.mysql.cj.jdbc.Driver

SPRING_JPA_PROPERTIES_DIALECT: org.hibernate.dialect.MySQLDialect

SPRING_MAIL_HOST: smtp.gmail.com
SPRING_MAIL_USERNAME: [email protected]
SPRING_MAIL_PASSWORD: b3^weK*zh47gZxm5

SPRING_DATA_REDIS_HOST: alarm-redis
SPRING_DATA_REDIS_PORT: 6379

SPRING_KAFKA_BOOTSTRAP-SERVERS: Kafka00Service:9092,Kafka01Service:9092,Kafka02Service:9092

EUREKA_CLIENT_SERVICE-URL_DEFAULTZONE: http://discovery-app:8761/eureka

PASSPORT_ALGORITHM: SHA-256
PASSPORT_KEY: SECRET_KEY

MANAGEMENT_ZIPKIN_TRACING_ENDPOINT: http://infra-zipkin:9411/api/v2/spans

SPRING_RABBITMQ_HOST: alarm-rabbitmq
SPRING_RABBITMQ_PORT: 5672
depends_on:
- alarm-mysql
- alarm-redis
- alarm-rabbitmq
networks:
- lalala-network

alarm-mysql:
image: mysql:8.2.0
restart: always
ports:
- 28100:3306
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: user
TZ: Asia/Seoul
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
volumes:
- ./.docker/mysql/:/var/lib/mysql
- ./infra/:/docker-entrypoint-initdb.d
networks:
- lalala-network

alarm-redis:
image: redis:7.0
ports:
- 28200:6379

alarm-adminer:
image: adminer
restart: always
ports:
- 29900:8080

alarm-rabbitmq:
image: rabbitmq:3.10-management
ports:
- "28300:5672"
environment:
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: admin

networks:
lalala-network:
driver: bridge
external: true
Loading

0 comments on commit 05037f1

Please sign in to comment.