Jenkins

1. git clone

 

 

2.  gradle build

3. .jar ssh publisher 

aws api-server로 .jar 파일 복사

 

4. ansible playbook 실행

aws ansible-server ssh접속 후 playbook실행 (api-server 도커 제어)

 

Dockerfile

FROM openjdk:17-alpine

VOLUME /tmp

ARG JAR_FILE=./*.jar

COPY ./airbnbApi-0.0.1-SNAPSHOT.jar app.jar

ENTRYPOINT ["java","-jar","-Djasypt.encryptor.password=djswpsrksms","/app.jar"]

 

Playbook.yml

- hosts: all
#   become: true  

  tasks:
  - name: stop current running container
    command: docker stop airbnb-demo
    ignore_errors: yes

  - name: remove stopped cotainer
    command: docker rm airbnb-demo
    ignore_errors: yes

  - name: remove current docker image
    command: docker rmi api-v1
    ignore_errors: yes

  - name: build a docker image with deployed war file
    command: docker build -t api-v1 .
    args: 
        chdir: /root

  - name: create a container using  image
    command: docker run -d --name airbnb-demo -p 8081:8080 api-v1

 

Dockerfile

# nginx 이미지를 사용합니다. 뒤에 tag가 없으면 latest 를 사용합니다.
FROM nginx:latest

# root 에 app 폴더를 생성
RUN mkdir /app

# work dir 고정
WORKDIR /app

# work dir 에 dist 폴더 생성 /app/dist
RUN mkdir ./dist

# host pc의 현재경로의 dist 폴더를 workdir 의 dist 폴더로 복사
ADD ./dist ./dist

# nginx 의 default.conf 를 삭제
RUN rm /etc/nginx/conf.d/default.conf

# host pc 의 default.conf 를 아래 경로에 복사
COPY ./default.conf /etc/nginx/conf.d

# 80 포트 오픈
EXPOSE 80

# container 실행 시 자동으로 실행할 command. nginx 시작함
CMD ["nginx", "-g", "daemon off;"]

 

nginx 설정 파일

 default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root    /app/dist;
        index   index.html;
        try_files $uri $uri/ /index.html;
    }
}

 

pipline

clone - build - ssh publisher

 

 

회원가입 완료하면 인증메일을 전송합니다.

이메일 전송은 ApplicationEventPublisher로 event 발생시켜 비동기적으로 실행합니다.

 

 

 

contorller

AuthController.java

 

service

AuthService.java

회원가입

 

 

ApplicationEventPublisher 빈을 주입하여 publishEvent() 메서드를 통해 생성된 이벤트 객체를 넣어 줍니다.

회원가입 후 이벤트 발행

 

Event

RegistrationCompleteEvent.java

Event listener

@EventListener 어노테이션을 통해 발생하는 이벤트를 캐치합니다

RegistrationCompleteEventListener.java

메일 본문은 templateEngine 사용하여 html 형식으로 전송합니다.

 

Email

 

 

 

비동기 처리

스프링 이벤트는 기본적으로 동기 방식으로 동작합니다.

이메일 전송은 끝나는 시간이 다소 오래 걸리기 때문에 이벤트를 비동기 처리합니다

config/AsyncConfig.java

@EnableAsync 어노테이션을 통해 비동기를 사용하겠다고 선언

프로세스 갯수만큼 스레드풀 설정

+ Recent posts