일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- unknown database
- ajax 통신
- 테스트 필드주입 Null
- 스프링 HTTP 통신
- 배열 컬렉션 변환
- AWS Spring MySQL Redis
- ajax 장점
- Test NullPointerException
- 주요 네트워크 종류
- http 변화
- ajax 단점
- 네트워크 구성 요소
- 데이터베이스 언어 종류
- 데이터베이스 설계 순서
- php 통신
- jwt 특징
- php 통신흐름
- jwt 탄생배경
- Java Roadmap
- http 버전별 개선점
- 화살표 함수 this
- 스프링 값 받아오기
- 스프링 데이터 전달
- php 통신구조
- http 버전별 단점
- 컬렉션 배열 변환
- mod_php vs php-fpm
- Java 로드맵
- ajax 특징
- AWS EC2 Spring 서버
- Today
- Total
zkvn99
[AWS] 스프링 웹 서비스 EC2 배포 본문
OS : Mac(local), ubuntu
Language : Java
Build : Gradle
DB : MySQL, Redis
설명에 앞서 대략적인 흐름은 클라우드에 DB와 실행파일을 직접 설치하여 실행할 것입니다.
DB 설치 및 로컬에 있는 Jar 파일을 클라우드 컴퓨터에 전송 후 실행하면 끝입니다.
먼저 인스턴스를 생성합니다. 처음 AWS를 접하시면 아래 블로그를 참고하시면 됩니다
https://opentutorials.org/course/2717/11273
AWS EC2 - 생활코딩
EC2(Elastic Compute Cloud)는 독립된 컴퓨터를 임대해주는 서비스입니다. 본 수업의 하위 수업에서는 EC2의 개념과 사용방법을 알아봅니다.
opentutorials.org
외부에서 접근이 가능하게 하려면 인바운드 규칙을 꼭 설정해주셔야 합니다 (인스턴스 -> 보안 -> 보안 그룹 -> 인바운드 규칙 편집)
Gradle 설정
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.15'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'laundry'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '11'
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.1'
implementation 'mysql:mysql-connector-java:8.0.32'
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.1'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2'
implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2'
implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}
jar {
enabled = false
}
tasks.named('test') {
useJUnitPlatform()
}
application.yml 설정
server:
address: 0.0.0.0
port: 8080
# database 연동 설정
spring:
redis:
host: localhost
port: 6379
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# 각자 PC에 만들어놓은 Database이름 사용
url: jdbc:mysql://localhost:3306/daeseda_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Seoul&characterEncoding=UTF-8
# mysql에 생성한 사용자 계정 정보를 사용
username: username
password: password
# spring data jpa 설정
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
open-in-view: true
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate.format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
mvc:
pathmatch:
matching-strategy: ant_path_matcher
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
jwt:
header: Authorization
secret: *
token-validity-in-seconds: 86400
custom:
image-directory: /path/on/ec2-instance
logging:
config: classpath:log4j2.xml
level:
org.hibernate.SQL: debug
기본적으로 로컬 환경에서 문제없이 실행되는 경우 Port가 사용 중이지 않고, DB 세팅만 잘되어있으면 문제가 없을 것입니다.
Gradle clean, build를 순서대로 진행합니다. 그러면 jar 파일이 생성될 것입니다
생성된 jar 파일을 클라우드에 보낼건데 터미널을 켜서 개인키가 있는 폴더로 이동 후 다음과 같은 명령어를 실행해줍니다
scp -i 개인키파일이름 jar파일경로 계정@아이피:경로
ex) sudo scp -i key.pem /Users/min/documents/github/daeseda-server/daeseda-laundry/build/libs/daeseda-0.0.1-SNAPSHOT.jar ubuntu@1.1.1.1:~/
성공적으로 파일이 전송되고 있다면
전송된 파일을 확인해봅시다
운영에 필요한 데이터베이스 설정이 있다면 다운하고 설정해봅시다
sudo apt-get update
sudo apt-get install mysql-server
sudo apt-get install redis
위와 같이 설치하고
sudo mysql -u root -p (아마 아무값이나 쳐도 들어가질겁니다)
yml 설정에 맞춰 데이터베이스 구성을 진행합니다 (데이터베이스 생성, 유저 생성)
JRE 설치 (버전에 맞게 설치)
sudo apt-get install openjdk-11-jre
Java 버전 확인
실행
sudo java -jar snapshot.jar
블로그 작성하면서 이전에 했던 프로젝트를 다시 올려봤는데 로컬에서 실행이 되는데 클라우드에서 안되는 경우 한가지만 알아보겠습니다.
db가 없을 경우
이건 데이터베이스 생성을 안했을 경우에 발생하는데, 로컬 컴퓨터의 터미널에서 해결하도록 합시다.
ssh -i 키이름 아이디@퍼블릭ip
ex) ssh -i key.pem ubuntu@1.1.1.1
데이터베이스 생성을 해주도록 합시다
그 후 실행을 해보면?
정상적으로 실행됩니다 !
다음에는 도커를 활용해서 진행해보도록 하겠습니다.