Skip to content

Commit

Permalink
feat : rds 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
maseungmin authored and maseungmin committed Nov 28, 2024
1 parent 5a865da commit 1ae1ab0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
//관리자 페이지를 위한 타임리프 추가
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

//rds
implementation 'mysql:mysql-connector-java:8.0.33' // 최신 버전 사용 가능

//ssh tunnel
implementation 'com.github.mwiede:jsch:0.2.16'

Expand Down
2 changes: 1 addition & 1 deletion config
59 changes: 59 additions & 0 deletions src/main/java/icurriculum/global/config/SSHDataSourceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package icurriculum.global.config;


import javax.sql.DataSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class SSHDataSourceConfig {

private final SSHConfig initializer;

@Value("${server}")
private String isServer;

@Value("${cloud.aws.ec2.database_endpoint}")
private String databaseEndpoint;
@Value("${cloud.aws.ec2.database_port}")
private int databasePort;


@Bean("dataSource")
@Primary
public DataSource dataSource(DataSourceProperties properties) {
String url = properties.getUrl().replace("localhost", databaseEndpoint);
url = url.replace("[forwardedPort]", String.valueOf(3306));
Integer forwardedPort = null;

// SSH 터널을 통해 RDS에 연결해야 할 경우
if (isServer.equals("false")) {
url = properties.getUrl(); // jdbc:mysql://localhost:[forwardedPort]/dev
forwardedPort = initializer.buildSshConnection(databaseEndpoint, databasePort);
url = url.replace("[forwardedPort]", String.valueOf(forwardedPort));
}

log.info("Datasource Properties URL: {}", url);
log.info("Datasource Properties Username: {}", properties.getUsername());
log.info("Datasource Properties Driver ClassName: {}", properties.getDriverClassName());

log.info(url);

DataSource dataSourceInstance = DataSourceBuilder.create()
.url(url)
.username(properties.getUsername())
.password(properties.getPassword())
.driverClassName(properties.getDriverClassName())
.build();
return dataSourceInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@RequiredArgsConstructor
public class SSHMongoConfig {

@Value("${cloud.aws.ec2.database_endpoint}")
@Value("${cloud.aws.ec2.mongo_endpoint}")
private String databaseEndpoint;
@Value("${spring.data.mongodb.username}")
private String mongoUser;
Expand Down

0 comments on commit 1ae1ab0

Please sign in to comment.