-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($DataSource): reduce instances of data source
- Loading branch information
1 parent
7993f40
commit 581ac97
Showing
6 changed files
with
72 additions
and
76 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -12,44 +12,44 @@ | |
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 12:07 AM | ||
**/ | ||
@Slf4j | ||
public class DataSourceContextHolder { | ||
private static final ThreadLocal<DataSourceTypeEnum> CONTEXT_HOLDER = new ThreadLocal<>(); | ||
public final class DataSourceContextHolder { | ||
protected static final ThreadLocal<DataSourceEnum> CONTEXT_HOLDER = | ||
ThreadLocal.withInitial(() -> DataSourceEnum.MASTER); | ||
private static final AtomicInteger COUNTER = new AtomicInteger(-1); | ||
private static final int MAX_COUNT = 9999; | ||
|
||
public static DataSourceTypeEnum get() { | ||
return Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(DataSourceTypeEnum.MASTER); | ||
private DataSourceContextHolder() { | ||
} | ||
|
||
public static void set(DataSourceTypeEnum dbType) { | ||
public static DataSourceEnum get() { | ||
return Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(DataSourceEnum.MASTER); | ||
} | ||
|
||
private static void set(DataSourceEnum dbType) { | ||
CONTEXT_HOLDER.set(dbType); | ||
} | ||
|
||
static void master() { | ||
set(DataSourceTypeEnum.MASTER); | ||
public static void master() { | ||
set(DataSourceEnum.MASTER); | ||
if (log.isDebugEnabled()) { | ||
log.debug("Current data source -> {}", DataSourceTypeEnum.MASTER); | ||
log.debug("Current data source -> {}", DataSourceEnum.MASTER); | ||
} | ||
} | ||
|
||
static void slave() { | ||
// Simple load-balance for more slave clusters | ||
public static void slave() { | ||
// Simple load-balance for more slave clusters, assumed we got 2 replicas | ||
val index = COUNTER.getAndIncrement() % 2; | ||
if (COUNTER.get() > MAX_COUNT) { | ||
COUNTER.set(-1); | ||
} | ||
// if (index == 0) { | ||
// set(DataSourceTypeEnum.SLAVE1); | ||
// }else { | ||
// set(DataSourceTypeEnum.SLAVE2); | ||
// } | ||
set(DataSourceTypeEnum.SLAVE1); | ||
// if replica data sources are more then 1, the data source needs load balance by index | ||
set(DataSourceEnum.SLAVE1); | ||
if (log.isDebugEnabled()) { | ||
log.debug("Current data source -> {}, index: {}", DataSourceTypeEnum.MASTER, index); | ||
log.debug("Current data source -> {}, index: {}", DataSourceEnum.MASTER, index); | ||
} | ||
} | ||
|
||
static void clear() { | ||
public static void clear() { | ||
CONTEXT_HOLDER.remove(); | ||
if (log.isDebugEnabled()) { | ||
log.debug("Cleared CONTEXT_HOLDER"); | ||
|
31 changes: 31 additions & 0 deletions
31
...-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceEnum.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,31 @@ | ||
package com.jmsoftware.maf.springcloudstarter.database; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
|
||
/** | ||
* Description: DataSourceEnum, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 12:03 AM | ||
**/ | ||
@Getter | ||
@ToString | ||
@RequiredArgsConstructor | ||
public enum DataSourceEnum { | ||
/** | ||
* Master | ||
*/ | ||
MASTER("master"), | ||
/** | ||
* Slave 1 | ||
*/ | ||
SLAVE1("slave1"), | ||
/** | ||
* Quartz | ||
*/ | ||
QUARTZ("quartz"), | ||
; | ||
|
||
private final String dataSourceName; | ||
} |
17 changes: 0 additions & 17 deletions
17
...rter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceTypeEnum.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
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 |
---|---|---|
|
@@ -3,11 +3,11 @@ | |
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
/** | ||
* Description: DynamicDataSource, change description here. | ||
* Description: ReadWriteIsolationDynamicRoutingDataSource, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 6/27/2021 12:06 AM | ||
**/ | ||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource { | ||
public class ReadWriteIsolationDynamicRoutingDataSource extends AbstractRoutingDataSource { | ||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
return DataSourceContextHolder.get(); | ||
|