10. AWS EC2 + Spring boot (7) - Spring boot 프로젝트 실행파일 변경기: WAR to JAR

2021. 3. 3. 17:52프로젝트/Salle(살래) 중고거래 웹

현재 Spring boot 프로젝트를 Maven을 이용해 WAR 파일로 build 한다음 Tomcat Manager GUI에 업로드 하는 방식으로 배포했습니다. 하지만 너무 번거롭습니다. 코드를 변경할때마다 로컬에 WAR파일을 Tomcat 페이지에 클릭 업로드 해야하기 때문입니다. 그러던 중 jojoldu님의 AWS EC2 &  Spring boot로 만드는 웹 애플리케이션 블로그를 보게 되었고 ubuntu에서 Github를 연동해시켜 코드만으로 JAR 파일을 서버에 배포하는 방법을 보고 채택했습니다. 정말 편합니다!!

참고자료: jojoldu님의 Spring boot 프로젝트 

저도 Linux를 잘 모르지만 JAR파일 실행 커맨드를 shell script 파일로 만들어 모든 코드를 칠 필요없이 호출해주기만 하면 되는 것도 알게되었습니다. 아래 사진이 deploy.sh 파일인데 실행시키면 단계별로 작성한 커맨드가 실행됩니다. 자세한 건  참고자료 사이트에서 보시면 되고, 간략하게 1: Repository 치환값 생성 2:  치환자로 이동 3: Github pull(업데이트) 4: Maven build(JAR파일 생성) 5:CURRENT_PID 치환자 생성(salle란 이름으로 실행 중인 프로세스의 pid) 6: 구동중인 salle 이름으로 된 애플리케이션 수가 0인지 확인(if [ -z ...] 띄어쓰기 주의!!) -> kill -[숫자] 는 프로세스를 종료시킬 수 있는 커맨드, sleep은 스크립트 동작을 숫자 시간만큼 딜레이시킴(s:seconds, m:minutes... 커스텀 가능), 7: JAR_NAME 치환값 생성, 8: jar 파일 실행 (nohup은 명령을 백그라운드에서 실행시켜 커맨드 라인을 계속 작성할 수 있도록 도와줍니다. 또한 .out을 붙이면 로그를 저장하는 파일을 생성해줍니다. 이를 통해 오류나 디버깅을 확인할 수 있습니다.)

* nohup 구문은 반드시 마지막에 &를 붙여야 됩니다!!!!!!!!

 


Main manifest 오류 발생


이미 WAR 파일로 build 해준 프로젝트여서 그런지 구글에 나온 모든 솔루션을 적용시켜도 해결이 되지 않았던 

missing out main manifest

오류는 프로젝트를 새로운 프로젝트를 생성해 옮겨 주니 해결 되었습니다. --JAR 파일 build시 발생

써 본 솔루션들


Hikari Datasource url exception 오류


Spring boot에 DB를 지정해주지 않으면 기본으로 내장된 HIkari Datasource를 구동시킵니다. 따라서 application.properties에 해당 설정을 해주지 않으면 오류가 발생합니다. 해결방법은 jojoldu님 블로그를 참고했습니다. Spring boot 프로젝트를 설명한 블로그 중 가장 잘 정리해놓은 블로그라고 생각됩니다. 


현재 서버 실행 오류로 교착상태


참고로 현재 AWS S3와 RDS까지 설정을 마쳤지만 원인 미상 extShutDown: 시작과 동시에 application이 종료되는 오류가 나고 있어서 멈춰있습니다. StackOverflow와 OKKY에도 올려봤는데 일단 OKKY에선 답글이 없습니다. 서버 배포 오류가 해결되기 전까지 작성된 코드들을 다시 보며 리팩토링 해볼 생각입니다. 그래도 정 안되면 예상되는 클래스들을 빼고 하나씩 추가해가며 배포해 볼 생각입니다. 

210601 추가 작성

감사하게도 댓글로 해결여부를 물어봐주셔서 미처 오류해결에 대한 부분을 작성하지 않았단 것을 알았고, 로그를 남깁니다. nohup으로 jar 파일실행 명령어를 작성했지만 명령어 끝에 &를 붙여주지 않아 추가 명령을 현재 커맨드 라인에서 대기 중인 상태였던 것입니다. 그걸 모른체 저는 커맨드 라인에 곧바로 명령어를 작성했고 서버는 실행취소가 된 것입니다.

* nohup 구문은 반드시 마지막에 &를 붙여야 됩니다!!!!!!!!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.3)
 
2021-03-03 00:44:34.278  INFO 5898 --- [           main] com.example.salle.SalleApplication       : Starting SalleApplication v0.0.1-SNAPSHOT using Java 1.8.0_282 on ip-172-31-46-33 with PID 5898 (/home/ubuntu/salle/salle/target/test-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/salle/salle)
2021-03-03 00:44:34.283  INFO 5898 --- [           main] com.example.salle.SalleApplication       : No active profile set, falling back to default profiles: default
2021-03-03 00:44:36.435  INFO 5898 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-03-03 00:44:36.489  INFO 5898 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 31 ms. Found 0 JPA repository interfaces.
2021-03-03 00:44:38.298  INFO 5898 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-03 00:44:38.330  INFO 5898 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-03 00:44:38.330  INFO 5898 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-03 00:44:39.363  INFO 5898 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2021-03-03 00:44:39.969  INFO 5898 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-03 00:44:39.969  INFO 5898 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5536 ms
2021-03-03 00:44:41.016  INFO 5898 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-03-03 00:44:41.247  INFO 5898 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-03-03 00:44:41.408  INFO 5898 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-03-03 00:44:41.600  INFO 5898 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.28.Final
2021-03-03 00:44:41.900  INFO 5898 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-03-03 00:44:42.217  INFO 5898 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MariaDB103Dialect
2021-03-03 00:44:43.125  INFO 5898 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
ct.MariaDB103Dialect
2021-03-03 00:44:43.155  INFO 5898 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-03-03 00:44:45.786  INFO 5898 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2021-03-03 00:44:45.860  INFO 5898 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'messageBrokerTaskScheduler'
2021-03-03 00:44:46.023  INFO 5898 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientInboundChannelExecutor'
2021-03-03 00:44:46.025  INFO 5898 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'clientOutboundChannelExecutor'
2021-03-03 00:44:46.035  INFO 5898 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'brokerChannelExecutor'
2021-03-03 00:44:46.140  WARN 5898 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-03-03 00:44:47.221  INFO 5898 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-03 00:44:47.227  INFO 5898 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : Starting...
2021-03-03 00:44:47.227  INFO 5898 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
2021-03-03 00:44:47.233  INFO 5898 --- [           main] o.s.m.s.b.SimpleBrokerMessageHandler     : Started.
2021-03-03 00:44:47.252  INFO 5898 --- [           main] com.example.salle.SalleApplication       : Started SalleApplication in 14.288 seconds (JVM running for 15.469)
2021-03-03 00:45:00.256  INFO 5898 --- [extShutdownHook] o.s.m.s.b.SimpleBrokerMessageHandler     : Stopping...
2021-03-03 00:45:00.258  INFO 5898 --- [extShutdownHook] o.s.m.s.b.SimpleBrokerMessageHandler     : BrokerAvailabilityEvent[available=false, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
2021-03-03 00:45:00.258  INFO 5898 --- [extShutdownHook] o.s.m.s.b.SimpleBrokerMessageHandler     : Stopped.
2021-03-03 00:45:00.284  INFO 5898 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'brokerChannelExecutor'
2021-03-03 00:45:00.284  INFO 5898 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'clientOutboundChannelExecutor'
2021-03-03 00:45:00.284  INFO 5898 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'clientInboundChannelExecutor'
2021-03-03 00:45:00.284  INFO 5898 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'messageBrokerTaskScheduler'
2021-03-03 00:45:00.296  INFO 5898 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-03-03 00:45:00.299  INFO 5898 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...```
cs