-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 additions
and
31 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
45 changes: 45 additions & 0 deletions
45
.../dangdang/ddframe/rdb/sharding/api/strategy/slave/MasterSlaveLoadBalanceStrategyType.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,45 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* Master-slave database load-balance strategy type. | ||
* | ||
* @author zhangliang | ||
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public enum MasterSlaveLoadBalanceStrategyType { | ||
|
||
RoundRobin(new RoundRobinMasterSlaveLoadBalanceStrategy()), | ||
Random(new RandomMasterSlaveLoadBalanceStrategy()); | ||
|
||
private final MasterSlaveLoadBalanceStrategy strategy; | ||
|
||
/** | ||
* Get default master-slave database load-balance strategy. | ||
* | ||
* @return default master-slave database load-balance strategy | ||
*/ | ||
public static MasterSlaveLoadBalanceStrategy getDefaultStrategy() { | ||
return RoundRobin.strategy; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...angdang/ddframe/rdb/sharding/api/strategy/slave/RandomMasterSlaveLoadBalanceStrategy.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,34 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
|
||
/** | ||
* Random slave database load-balance strategy. | ||
* | ||
* @author zhangliang | ||
*/ | ||
public final class RandomMasterSlaveLoadBalanceStrategy implements MasterSlaveLoadBalanceStrategy { | ||
|
||
@Override | ||
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) { | ||
return slaveDataSourceNames.get(new Random().nextInt(slaveDataSourceNames.size())); | ||
} | ||
} |
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
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
24 changes: 24 additions & 0 deletions
24
...ang/ddframe/rdb/sharding/api/strategy/slave/RandomMasterSlaveLoadBalanceStrategyTest.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,24 @@ | ||
package com.dangdang.ddframe.rdb.sharding.api.strategy.slave; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public final class RandomMasterSlaveLoadBalanceStrategyTest { | ||
|
||
private final RandomMasterSlaveLoadBalanceStrategy randomMasterSlaveLoadBalanceStrategy = new RandomMasterSlaveLoadBalanceStrategy(); | ||
|
||
@Test | ||
public void assertGetDataSource() { | ||
String masterDataSourceName = "test_ds_master"; | ||
String slaveDataSourceName1 = "test_ds_slave_1"; | ||
String slaveDataSourceName2 = "test_ds_slave_2"; | ||
List<String> slaveDataSourceNames = Arrays.asList(slaveDataSourceName1, slaveDataSourceName2); | ||
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames))); | ||
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames))); | ||
assertTrue(slaveDataSourceNames.contains(randomMasterSlaveLoadBalanceStrategy.getDataSource("ds", masterDataSourceName, slaveDataSourceNames))); | ||
} | ||
} |
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
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