-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
93 additions
and
22 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/outstagram/outstagram/common/strategy/ShardingStrategy.java
This file contains 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,9 @@ | ||
package com.outstagram.outstagram.common.strategy; | ||
|
||
import static com.outstagram.outstagram.common.constant.DBConst.DB_COUNT; | ||
|
||
public class ShardingStrategy { | ||
public static int getShardId(long userId) { | ||
return (int) (userId % DB_COUNT); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/main/java/com/outstagram/outstagram/config/DatabaseConfig.java
This file was deleted.
Oops, something went wrong.
This file contains 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/outstagram/outstagram/config/database/DataSourceContextHolder.java
This file contains 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,17 @@ | ||
package com.outstagram.outstagram.config.database; | ||
|
||
public class DataSourceContextHolder { | ||
private static final ThreadLocal<Integer> contextHolder = new ThreadLocal<>(); | ||
|
||
public static void setShardId(int shardId) { | ||
contextHolder.set(shardId); | ||
} | ||
|
||
public static Integer getShardId() { | ||
return contextHolder.get(); | ||
} | ||
|
||
public static void clearShardId() { | ||
contextHolder.remove(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/outstagram/outstagram/config/database/DataSourceRouting.java
This file contains 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,14 @@ | ||
package com.outstagram.outstagram.config.database; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
@Slf4j | ||
public class DataSourceRouting extends AbstractRoutingDataSource { | ||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
Integer shardId = DataSourceContextHolder.getShardId(); | ||
log.info("=============================== Current Shard ID: {}", shardId); | ||
return shardId; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/outstagram/outstagram/config/database/DatabaseConfig.java
This file contains 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,40 @@ | ||
package com.outstagram.outstagram.config.database; | ||
|
||
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.Primary; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
@Configuration | ||
public class DatabaseConfig { | ||
|
||
@Bean(name = "shard0DataSource") | ||
@ConfigurationProperties(prefix = "spring.datasource.shard0") | ||
public DataSource shard0DataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(name = "shard1DataSource") | ||
@ConfigurationProperties(prefix = "spring.datasource.shard1") | ||
public DataSource shard1DataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSourceRouting dataSourceRouting() { | ||
DataSourceRouting dataSourceRouting = new DataSourceRouting(); | ||
Map<Object, Object> dataSources = new HashMap<>(); | ||
dataSources.put(0, shard0DataSource()); | ||
dataSources.put(1, shard1DataSource()); | ||
dataSourceRouting.setTargetDataSources(dataSources); | ||
dataSourceRouting.setDefaultTargetDataSource(shard1DataSource()); | ||
return dataSourceRouting; | ||
} | ||
} |
This file contains 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