인스턴스와 데이터베이스를 생성 하였고 톰캣 까지 구동시켰습니다.
이제는 저희가 만든 애플리케이션을 로컬서버가 아닌 원격서버에 올려서
다른 사용자들이 접근 할 수 있게 하기위해 Bank app에 코드를 조금씩 수정 해주어야 합니다.
yml 수정
우선 db와의 연동을 위해
yml에 있는 datasource 부분을 수정해주어야 합니다
기존 yml
server:
port: 80
servlet:
encoding:
charset: utf-8
force: true
spring:
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
datasource:
url: jdbc:mysql://localhost:3306/bank?serverTimeZone=Asia/Seoul
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: asd1234
sql:
init:
schema-locations:
- classpath:db/table.sql
data-locations:
- classpath:db/data.sql
h2:
console:
enabled: true
output:
ansi:
enabled: always
mybatis:
mapper-locations:
- classpath:mapper/**.xml
configuration:
map-underscore-to-camel-case: true
workbench로 원격 데이터베이스에 접속했을때와 비슷하게
url,username,password를 변경해주셔야 합니다.

변경후 yml
server:
port: 80
servlet:
encoding:
charset: utf-8
force: true
spring:
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
datasource:
url: jdbc:mysql://ls-a6472f869b05488700699286cfc5bd6018d37d95.cqazell9nui7.ap-northeast-2.rds.amazonaws.com:3306/bank?serverTimeZone=Asia/Seoul
driver-class-name: com.mysql.cj.jdbc.Driver
username: dbmasteruser
password: "!X#O5dnKXr^!<2jl;mk<7_7JwjXH}$9F"
sql:
init:
schema-locations:
- classpath:db/table.sql
data-locations:
- classpath:db/data.sql
h2:
console:
enabled: true
output:
ansi:
enabled: always
mybatis:
mapper-locations:
- classpath:mapper/**.xml
configuration:
map-underscore-to-camel-case: true
저 같은 password에 경우 특수문자가 포함되어 있기 때문에
yml 파서가 제대로 해석하기 위해 따옴표로 감싸두었습니다.
이제 이 상태로 시작하시게 된다면 원격에 존재하는 데이터베이스와 연동이 되었을 것입니다.
gradle 수정
수정전
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'war'
}
group = 'com.tencoding'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation group: 'org.glassfish.web', name: 'jakarta.servlet.jsp.jstl', version: '2.0.0'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
// 커넥트 J
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.security:spring-security-crypto'
}
tasks.named('test') {
useJUnitPlatform()
}
수정후
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'war'
}
group = 'com.tencoding'
version = '0.0.1-SNAPSHOT'
// bootWar 패키징을 실행하지 않도록 설정
bootWar.enabled = false
// war 패키징 작업 활성화
war.enabled= true
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation group: 'org.glassfish.web', name: 'jakarta.servlet.jsp.jstl', version: '2.0.0'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// 추가
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
// 커넥트 J
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.security:spring-security-crypto'
}
tasks.named('test') {
useJUnitPlatform()
}
배포 환경에서 구동시키기 위해 gradle에 의존성을 추가,변경해줍니다.
BankApplication 수정
SpringBootServletInitializer를 상속받아, WAR 파일로 패키징하여 서블릿 컨테이너에서 실행할 수 있게 해야하며
configure 메소드를 재정의 하여 배포 환경에서 실행되게끔 변경해주어야 합니다.
package com.tencoding.bank;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class BankApplication extends SpringBootServletInitializer {
// 추가
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BankApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(BankApplication.class, args);
}
}
코드 수정은 여기 까지 입니다.
'AWS Lightsail' 카테고리의 다른 글
| 배포하기 (0) | 2024.12.21 |
|---|---|
| Database 생성 및 연동 (0) | 2024.12.21 |
| Tomcat 설치 (1) | 2024.12.19 |
| Java 설치 (0) | 2024.12.19 |
| 간단한 서버 설정 (0) | 2024.12.19 |