Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/Dev_CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: dev-cd

on:
push:
branches:
- "develop"

permissions:
contents: read
checks: write
actions: read
pull-requests: write

jobs:
test:
uses: ./.github/workflows/Dev_CI.yml
secrets: inherit

build:
needs: test
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Setting dev-secret.yml
run: |
echo "${{ secrets.DEV_SECRET_YML }}" > ./src/main/resources/dev-secret.yml

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'

- name: Make gradlew executable
run: chmod +x gradlew

- name: bootJar with Gradle
run: ./gradlew bootJar --info

- name: Change artifact file name
run: mv build/libs/*.jar build/libs/app.jar

- name: Upload artifact file
uses: actions/upload-artifact@v4
with:
name: app-artifact
path: ./build/libs/app.jar
if-no-files-found: error

- name: Upload deploy scripts
uses: actions/upload-artifact@v4
with:
name: deploy-scripts
path: ./scripts/dev/
if-no-files-found: error

deploy:
needs: build
runs-on: dev

steps:
- name: Download artifact file
uses: actions/download-artifact@v4
with:
name: app-artifact
path: ~/app

- name: Download deploy scripts
uses: actions/download-artifact@v4
with:
name: deploy-scripts
path: ~/app/scripts

- name: Replace application to latest
run: sudo sh ~/app/scripts/replace-new-version.sh
91 changes: 91 additions & 0 deletions .github/workflows/Dev_CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: dev-ci

on:
pull_request:
branches:
- develop
workflow_call:

permissions:
contents: read
checks: write
pull-requests: write

jobs:
build-and-push:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
TEST_REPORT: true

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_DATABASE: wellmeet_noti_test
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

zookeeper:
image: confluentinc/cp-zookeeper:7.0.1
env:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 2181:2181
options: >-
--health-cmd="curl -f http://localhost:8080/commands/stat || exit 1"
--health-interval=10s
--health-timeout=10s
--health-retries=5

kafka:
image: confluentinc/cp-kafka:7.0.1
env:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_AUTO_CREATE_TOPICS_ENABLE: true
ports:
- 9092:9092
options: >-
--health-cmd="timeout 10s bash -c 'until printf \"\" 2>>/dev/null >>/dev/tcp/localhost/9092; do sleep 1; done'"
--health-interval=10s
--health-timeout=10s
--health-retries=10

steps:
- uses: actions/checkout@v4

- name: Setting local-secret.yml
run: |
echo "${{ secrets.LOCAL_SECRET_YML }}" > ./src/main/resources/local-secret.yml

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Grant Permission
run: chmod +x ./gradlew

- name: Build With Gradle
run: ./gradlew clean build -x test

- name: Run Tests With Gradle
run: ./gradlew test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ out/
### VS Code ###
.vscode/
/src/main/resources/local-secret.yml
.serena/
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.kafka:spring-kafka'
implementation('nl.martijndwars:web-push:5.1.1') {
exclude group: 'org.asynchttpclient', module: 'async-http-client'
}
Expand Down
40 changes: 40 additions & 0 deletions scripts/dev/replace-new-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

PID=$(lsof -t -i:8080)

# 프로세스 종료
if [ -z "$PID" ]; then
echo "No process is using port 8080."
else
echo "Killing process with PID: $PID"
kill -15 "$PID"

# 직전 명령(프로세스 종료 명령)이 정상 동작했는지 확인
if [ $? -eq 0 ]; then
echo "Process $PID terminated successfully."
else
echo "Failed to terminate process $PID."
fi
fi

JAR_FILE=$(ls /home/ubuntu/app/*.jar | head -n 1)

echo "JAR 파일 실행: $JAR_FILE"

# 애플리케이션 로그 파일 설정
APP_LOG_DIR="/home/ubuntu/app/logs"
APP_LOG_FILE="$APP_LOG_DIR/application-$(date +%Y%m%d-%H%M%S).log"

echo "애플리케이션 로그 파일: $APP_LOG_FILE"

sudo nohup java \
-Dspring.profiles.active=dev \
-Duser.timezone=Asia/Seoul \
-Dserver.port=8080 \
-Ddd.service=wellmeet-notification \
-Ddd.env=dev \
-jar "$JAR_FILE" > "$APP_LOG_FILE" 2>&1 &

echo "애플리케이션이 백그라운드에서 실행되었습니다."
echo "로그 확인: tail -f $APP_LOG_FILE"
echo "=== 배포 완료 ==="
49 changes: 49 additions & 0 deletions src/main/java/com/wellmeet/config/KafkaConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.wellmeet.config;

import com.wellmeet.notification.consumer.dto.NotificationMessage;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.serializer.JsonDeserializer;

@EnableKafka
@Configuration
public class KafkaConfig {

@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;

@Value("${spring.kafka.consumer.group-id}")
private String groupId;

@Bean
public ConsumerFactory<String, NotificationMessage> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

props.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
props.put(JsonDeserializer.TRUSTED_PACKAGES, "com.wellmeet.consumer.dto");
props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, NotificationMessage.class);
return new DefaultKafkaConsumerFactory<>(props);
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/wellmeet/notification/Sender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wellmeet.notification;

import com.wellmeet.notification.consumer.dto.NotificationMessage;
import com.wellmeet.notification.domain.NotificationChannel;

public interface Sender {

boolean isEnabled(NotificationChannel channel);

void send(NotificationMessage message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wellmeet.notification.consumer;

import com.wellmeet.notification.consumer.dto.NotificationMessage;
import com.wellmeet.notification.domain.NotificationEnabled;
import com.wellmeet.notification.repository.NotificationEnabledRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationConsumer {

private final NotificationEnabledRepository notificationEnabledRepository;
private final NotificationSender notificationSender;

@KafkaListener(topics = "notification", groupId = "notification-group")
public void consume(NotificationMessage message) {
log.info("Received message: {}", message);
List<NotificationEnabled> enables = notificationEnabledRepository.findByUserIdAndType(
message.getNotification().getRecipient(),
message.getNotification().getType()
);
notificationSender.send(message, enables);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wellmeet.notification.consumer;

import com.wellmeet.notification.Sender;
import com.wellmeet.notification.consumer.dto.NotificationMessage;
import com.wellmeet.notification.domain.NotificationEnabled;
import com.wellmeet.notification.domain.NotificationHistory;
import com.wellmeet.notification.repository.NotificationHistoryRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class NotificationSender {

private final List<Sender> senders;
private final NotificationHistoryRepository notificationHistoryRepository;

public void send(NotificationMessage message, List<NotificationEnabled> enables) {
for (NotificationEnabled enabled : enables) {
notificationHistoryRepository.save(new NotificationHistory(message.getNotification().getRecipient()));
Sender sender = senders.stream()
.filter(low -> low.isEnabled(enabled.getChannel()))
.findFirst()
.orElseThrow();
sender.send(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wellmeet.notification.consumer.dto;

import java.time.LocalDateTime;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class MessageHeader {

private String messageId;
private LocalDateTime timestamp;
private String source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wellmeet.notification.consumer.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class NotificationInfo {

private NotificationType type;
private String recipient;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wellmeet.notification.consumer.dto;

import java.util.Map;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class NotificationMessage {

private MessageHeader header;
private NotificationInfo notification;
private Map<String, Object> payload;
}
Loading
Loading