This repository was archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
[#43] Mysql Master/Slave Replication 적용 #60
Open
tjdrnr0557
wants to merge
9
commits into
develop
Choose a base branch
from
feature/43
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8592846
Routing DataSource 로직 추가
tjdrnr0557 fd8d40b
mysql replication 설정 완료
tjdrnr0557 9a8814e
SetDataSource AOP 적용
tjdrnr0557 ce06c5a
어노테이션 추가
tjdrnr0557 d48f7f9
오류 수정
tjdrnr0557 ddeb800
thorws 처리 로직 변경
tjdrnr0557 3cd30fa
.
tjdrnr0557 4aa8936
lazyRoutingDataSource 적용
tjdrnr0557 69ea5b6
주석 제거
tjdrnr0557 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/flab/makedel/annotation/SetDataSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.flab.makedel.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface SetDataSource { | ||
|
|
||
| DataSourceType dataSourceType(); | ||
|
|
||
| enum DataSourceType { | ||
| MASTER, SLAVE; | ||
| } | ||
|
|
||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/flab/makedel/aop/SetDataSourceAspect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.flab.makedel.aop; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.exception.WrongDataSourceException; | ||
| import com.flab.makedel.routingdatasource.RoutingDataSourceManager; | ||
| import java.sql.SQLException; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.aspectj.lang.annotation.Before; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| public class SetDataSourceAspect { | ||
|
|
||
| @Before("@annotation(com.flab.makedel.annotation.SetDataSource) && @annotation(target)") | ||
| public void setDataSource(SetDataSource target) throws WrongDataSourceException { | ||
|
|
||
| if (target.dataSourceType() == DataSourceType.MASTER | ||
| || target.dataSourceType() == DataSourceType.SLAVE) { | ||
| RoutingDataSourceManager.setCurrentDataSourceName(target.dataSourceType()); | ||
| } else { | ||
| throw new WrongDataSourceException("Wrong DataSource Type : Should Check Exception"); | ||
| } | ||
|
|
||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약 DataSource Type이 Master나 Slave가 아니라면 개발자들이 어노테이션에 오타를 내거나 이름을 잘못지은 오류일 것입니다. 따라서 아예 컴파일타임에 개발자들이 exception을 알아채고 처리를 해주어야 할 것 같아서 checked Exception인 SQLException을 사용하였습니다. |
||
| } | ||
| } | ||
86 changes: 86 additions & 0 deletions
86
src/main/java/com/flab/makedel/config/DataSourceConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.flab.makedel.config; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.routingdatasource.RoutingDataSourceManager; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import javax.sql.DataSource; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| 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.PropertySource; | ||
| import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
| import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
| import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
| import org.springframework.transaction.PlatformTransactionManager; | ||
| import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
|
||
| @Configuration | ||
| @PropertySource("classpath:/application-dev.properties") | ||
| public class DataSourceConfig { | ||
|
|
||
| @Bean | ||
| @ConfigurationProperties(prefix = "spring.datasource.master") | ||
| public DataSource masterDataSource() { | ||
| return DataSourceBuilder.create().build(); | ||
| } | ||
|
|
||
| @Bean | ||
| @ConfigurationProperties(prefix = "spring.datasource.slave") | ||
| public DataSource slaveDataSource() { | ||
| return DataSourceBuilder.create().build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public DataSource routingDataSource( | ||
| @Qualifier(value = "masterDataSource") DataSource masterDataSource, | ||
| @Qualifier(value = "slaveDataSource") DataSource slaveDataSource) { | ||
|
|
||
| AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { | ||
| @Override | ||
| protected Object determineCurrentLookupKey() { | ||
| DataSourceType dataSourceType = RoutingDataSourceManager.getCurrentDataSourceName(); | ||
|
|
||
| if (TransactionSynchronizationManager | ||
| .isActualTransactionActive()) { | ||
| boolean readOnly = TransactionSynchronizationManager | ||
| .isCurrentTransactionReadOnly(); | ||
| if (readOnly) { | ||
| dataSourceType = DataSourceType.SLAVE; | ||
| } else { | ||
| dataSourceType = DataSourceType.MASTER; | ||
| } | ||
| } | ||
|
|
||
| RoutingDataSourceManager.removeCurrentDataSourceName(); | ||
| return dataSourceType; | ||
| } | ||
| }; | ||
|
|
||
| Map<Object, Object> targetDataSources = new HashMap<>(); | ||
|
|
||
| targetDataSources.put(DataSourceType.MASTER, masterDataSource); | ||
| targetDataSources.put(DataSourceType.SLAVE, slaveDataSource); | ||
|
|
||
| routingDataSource.setTargetDataSources(targetDataSources); | ||
| routingDataSource.setDefaultTargetDataSource(masterDataSource); | ||
|
|
||
| return routingDataSource; | ||
| } | ||
|
|
||
| @Bean | ||
| public DataSource lazyRoutingDataSource( | ||
| @Qualifier(value = "routingDataSource") DataSource routingDataSource) { | ||
| return new LazyConnectionDataSourceProxy(routingDataSource); | ||
| } | ||
|
|
||
| @Bean | ||
| public PlatformTransactionManager transactionManager( | ||
| @Qualifier(value = "lazyRoutingDataSource") DataSource lazyRoutingDataSource) { | ||
| DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); | ||
| transactionManager.setDataSource(lazyRoutingDataSource); | ||
| return transactionManager; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...edel/Exception/DuplicatedIdException.java → ...edel/exception/DuplicatedIdException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...akedel/Exception/NotExistIdException.java → ...akedel/exception/NotExistIdException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/flab/makedel/exception/WrongDataSourceException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.flab.makedel.exception; | ||
|
|
||
| import java.sql.SQLException; | ||
|
|
||
| public class WrongDataSourceException extends SQLException { | ||
|
|
||
| public WrongDataSourceException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.MenuDTO; | ||
| import java.util.List; | ||
|
|
||
| public interface MenuMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertMenu(MenuDTO menu); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void deleteMenu(long menuId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| List<MenuDTO> selectStoreMenu(long storeId); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.OrderMenuDTO; | ||
| import java.util.List; | ||
|
|
||
| public interface OrderMenuMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertOrderMenu(List<OrderMenuDTO> orderMenuList); | ||
|
|
||
| } |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/flab/makedel/mapper/OrderMenuOptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.OrderMenuOptionDTO; | ||
| import java.util.List; | ||
|
|
||
| public interface OrderMenuOptionMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertOrderMenuOption(List<OrderMenuOptionDTO> orderMenuOptionList); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.PayDTO; | ||
|
|
||
| public interface PayMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertPay(PayDTO payDTO); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.StoreInfoDTO; | ||
| import com.flab.makedel.dto.StoreDTO; | ||
| import java.util.List; | ||
|
|
||
| public interface StoreMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertStore(StoreDTO store); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| List<StoreDTO> selectStoreList(String ownerId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| boolean isMyStore(long storeId, String ownerId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| StoreDTO selectStore(long storeId, String ownerId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void closeMyStore(long storeId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void openMyStore(long storeId); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| StoreInfoDTO selectStoreInfo(long storeId); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,31 @@ | ||
| package com.flab.makedel.mapper; | ||
|
|
||
| import com.flab.makedel.annotation.LoginCheck.UserLevel; | ||
| import com.flab.makedel.annotation.SetDataSource; | ||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
| import com.flab.makedel.dto.UserDTO; | ||
| import com.flab.makedel.dto.UserInfoDTO; | ||
| import org.apache.ibatis.annotations.Mapper; | ||
|
|
||
| @Mapper | ||
| public interface UserMapper { | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| boolean isExistsId(String id); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| UserInfoDTO selectUserInfo(String id); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void insertUser(UserDTO user); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.SLAVE) | ||
| UserDTO selectUserById(String id); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void deleteUser(String id); | ||
|
|
||
| @SetDataSource(dataSourceType = DataSourceType.MASTER) | ||
| void updateUserPassword(String id, String newPassword); | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/flab/makedel/routingdatasource/RoutingDataSourceManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.flab.makedel.routingdatasource; | ||
|
|
||
| import com.flab.makedel.annotation.SetDataSource.DataSourceType; | ||
|
|
||
| public class RoutingDataSourceManager { | ||
|
|
||
| private static final ThreadLocal<DataSourceType> currentDataSourceName = new ThreadLocal<>(); | ||
|
|
||
| public static void setCurrentDataSourceName(DataSourceType dataSourceType) { | ||
| currentDataSourceName.set(dataSourceType); | ||
| } | ||
|
|
||
| public static DataSourceType getCurrentDataSourceName() { | ||
| return currentDataSourceName.get(); | ||
| } | ||
|
|
||
| public static void removeCurrentDataSourceName() { | ||
| currentDataSourceName.remove(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 처리해줄 필요없이 스프링에서 제공하는 기능이 있을 것 같습니다~
readOnly트랜잭션이면 slave에서 읽는다거나요~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Transactional을 서비스에 붙히고 readOnly인지 아닌지에 따라 DataSoruce들을 분기해줄 수 있는 방식과 지금처럼 AOP방식을 고민을 했엇습니다.하지만 트랜잭션 방식을 사용하여 라우팅해준다면 모든 서비스에
@Transactional을 붙혀야 하고 단일연산에도 트랜잭션 설정을 해줘야 해서 AOP방식으로 데이터소스를 나누도록 구현 하였습니다.