Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] aurora db 세팅 #121

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.gachontable.gachontablebe.global.config.aurora;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class CustomRoutingDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
return TransactionSynchronizationManager.isCurrentTransactionReadOnly() ? "slave" : "master";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOF 추가 부탁드립니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package site.gachontable.gachontablebe.global.config.aurora;

import com.zaxxer.hikari.HikariDataSource;
import java.util.HashMap;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;

@Configuration
public class TransactionConfig {

private static final String MASTER_DATASOURCE = "masterDataSource";
private static final String SLAVE_DATASOURCE = "slaveDataSource";

@Bean(name = MASTER_DATASOURCE)
@ConfigurationProperties(prefix = "spring.datasource.master.hikari")
public HikariDataSource masterDataSource() {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
}

@Bean(name = SLAVE_DATASOURCE)
@ConfigurationProperties("spring.datasource.slave.hikari")
public HikariDataSource slaveDataSource() {
HikariDataSource dataSource = DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
dataSource.setReadOnly(true);
return dataSource;
}

@Bean
public CustomRoutingDataSource routingDataSource() {
CustomRoutingDataSource routingDataSource = new CustomRoutingDataSource();
HashMap<Object, Object> dataSources = new HashMap<>();
dataSources.put("master", masterDataSource());
dataSources.put("slave", slaveDataSource());
routingDataSource.setTargetDataSources(dataSources);
routingDataSource.setDefaultTargetDataSource(masterDataSource());
return routingDataSource;
}

@Bean
@Primary
@DependsOn("routingDataSource")
public DataSource dataSource(CustomRoutingDataSource routingDataSource) {
return new LazyConnectionDataSourceProxy(routingDataSource);
}
}

32 changes: 28 additions & 4 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
spring:
datasource:
url: ENC(rXVivRZ2+Z7AZQnwYxYVsXbLr2e4fifb/REZbpxgSoGW2Vm6u0Gj0/VJOfO1E+emfwXeCoU9N8H0cVuqYuoEWbdPU7kekYIIUG/nL1G4RmxrUPPttsPZGM2WrYG6sWy0)
username: ENC(QTTusIQmcVUiM+fDY8kvubSWvMLTmNCa)
password: ENC(7QKbd+ZODf/6SLG+D+Q15Or6EBw55I6KKYF0UnTqie8=)
driver-class-name: com.mysql.cj.jdbc.Driver
master:
hikari:
jdbc-url: ENC(2HeMB9z5gdgdMvunGQaAJ8Zxl/B6oWTLLhH4BlyhvnpRf9UoowL3dutYW1FcDRz5vt+LU69/Dnt9T3xjJ4oWaiK5fMvbJiC2DqSMpUrYQjuZIc5aA45/IOfiUrMOlXGHjHmcbLqQPzpdfYGoSFy7fMAZEJHHEbYJzarDEF/sujk2GbCmW1JN5qPq9AXYkNOMUULDJilD2hlvoO09qMUOOyaDJiMMB8SxzZlv18gG5ts2tPQvMU8DFly60x1ay8VbhpFqiQrskFI=)
username: ENC(Z89GsautuOl2p6BefL146A==)
password: ENC(4IWCgJuGPqbikO4xgN8jZRekUWDhUoyuafdh60pKN5M=)
driver-class-name: com.mysql.cj.jdbc.Driver
maximum-pool-size: 10
connection-timeout: 5000
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후 운영 서버 스펙에 맞추어 해당 값들을 변경해주어야 할 것 같습니다!

connection-init-sql: SELECT 1
validation-timeout: 2000
minimum-idle: 10
idle-timeout: 600000
max-lifetime: 1800000
slave:
hikari:
jdbc-url: ENC(xZaFqSkKkTPP3Ph7GgmqEwoqq4lmn+Me25G2IOIVCA1M1j/6o6VJuwX+/zPx6XHQFFjqAhIuICQQPpgWS/fr2sTYWbfXveGMHDjxxOcZ67ygeAM8+Ee+Vy3LIpOm6nL2zJLwADmdq8zBKu5nQZxlKNotvbSfG4lDME+45lbU9FKX5n/0ysGkc6RIFpO8gWsdzqp3eCkd7NpZGTwXfvgwyWzwV2SCI0ROS7JiDgGd8fV08kmWK8asHg==)
username: ENC(Z89GsautuOl2p6BefL146A==)
password: ENC(4IWCgJuGPqbikO4xgN8jZRekUWDhUoyuafdh60pKN5M=)
driver-class-name: com.mysql.cj.jdbc.Driver
maximum-pool-size: 10
connection-timeout: 5000
connection-init-sql: SELECT 1
validation-timeout: 2000
minimum-idle: 10
idle-timeout: 600000
max-lifetime: 1800000

jpa:
show-sql: false
hibernate:
Expand All @@ -16,6 +39,7 @@ spring:
jdbc:
time_zone: Asia/Seoul
open-in-view: false

security:
oauth2:
client:
Expand Down