Skip to content

Commit

Permalink
Optimize reading file and load balancing selectors name
Browse files Browse the repository at this point in the history
  • Loading branch information
BerryWang1996 committed Aug 28, 2018
1 parent 108ebba commit b3ff06b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
3 changes: 1 addition & 2 deletions demo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
spring:
datasource:
mysplitter:
configuration-file: mysplitter.yml
configuration-file: classpath:mysplitter.yml
logging:
level:
com.alibaba.druid: fatal
root: info
16 changes: 6 additions & 10 deletions demo/src/main/resources/mysplitter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,35 @@ mysplitter:
enabled: true
strategy: polling # random
databases:
database-a: # database-DatabaseName e.g.database-master database-192.168.1.1:3306
# dataSourceClass: com.alibaba.druid.pool.DruidDataSource
database-a:
readers:
reader-read-slave-1:
# dataSourceClass: (optional high priority)
configuration: # dataSource configuration
configuration:
url: jdbc:mysql://localhost:3306/user?useSSL=false
username: root
password: UtDAi2eqmspIDSHqpoGQU5JC9kpfFeZPBhUxkPnWtNwsTEYFkTh/QAa5wyU7LDufruSYN+0WCUTE6F5X++5tDA==
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKVbfAja9r0HF29S/ph/T+f6UbeNxn4giAzgxweKABRsJ2sI/MNhV8x7jTsCM15xDHKM4G++QqC1Bx0tdgG/BI0CAwEAAQ==
driverClassName: com.mysql.jdbc.Driver
reader-read-slave-2:
# dataSourceClass: (optional high priority)
configuration: # dataSource configuration
configuration:
url: jdbc:mysql://localhost:3306/user?useSSL=false
username: root
password: Oe7fcF2TLqytAlvy37C/IWfBhNhBFXmMGceE6GRxYyjJXh3TUdmq8EvebiFb0pB1hF9aH7thnnkthFiy5n3M8Q==
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMWmL+AzrbsKwfrtP/a/aQpQplNsoySxCHUQb0aJw2t8iemRtbxtxJhXmQqPMlAZdYppyK0wB48HTArD2am3/NMCAwEAAQ==
driverClassName: com.mysql.jdbc.Driver
writers:
writer-write-master-1:
configuration: # dataSource configuration
configuration:
url: jdbc:mysql://localhost:3306/user?useSSL=false
username: root
password: MAtsEynrB5qJp6oDfmae2Z2Hx1lqPwFDNMKnwUr/P7+HvYy8ZXIm6DKI5VWfLO34Bjcdy+Jsr4+/N++Bxx0Y5w==
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIvm9Ez/X3VOLUGNfATqtyQsK5+TOR66uK6MvHdX89N1K8S3l3bNVB2BKiPZ1hDxZNZfYtbQNUUHKjDyV+eUtq8CAwEAAQ==
driverClassName: com.mysql.jdbc.Driver
database-b: # database-DatabaseName e.g.database-master database-192.168.1.1:3306
# dataSourceClass: (optional)
database-b:
integrates:
integrate-slave-1:
dataSourceClass: com.zaxxer.hikari.HikariDataSource
configuration: # dataSource configuration
configuration:
jdbcUrl: jdbc:mysql://localhost:3306/dept?useSSL=false
username: root
password: iFIge745Qd4z76TgAWMZgIL+Nw0IRBRWbUi+xaGdiPGrHzhT+rfeJojPnnAjJWKazjQ1wMea2fRA/dod6VlRhQ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

/**
* Spring boot 自动配置类
Expand All @@ -37,8 +40,12 @@ public class MySplitterAutoConfigure {

@Bean(initMethod = "init")
@ConditionalOnMissingBean
public MySplitterDataSource mySplitterDataSource() {
return new MySplitterDataSource(properties.getConfigurationFile());
public MySplitterDataSource mySplitterDataSource() throws IOException {
return new MySplitterDataSource(
new PathMatchingResourcePatternResolver()
.getResource(properties.getConfigurationFile())
.getFile()
.getPath());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.mysplitter.config.MySplitterLoadBalanceConfig;
import com.mysplitter.exceptions.NoHealthyDataSourceException;
import com.mysplitter.selector.AbstractLoadBalanceSelector;
import com.mysplitter.selector.NoneLoadBalanceSelector;
import com.mysplitter.selector.NoLoadBalanceSelector;
import com.mysplitter.selector.RandomLoadBalanceSelector;
import com.mysplitter.selector.RoundRobinLoadBalanceSelector;
import com.mysplitter.util.ClassLoaderUtil;
Expand Down Expand Up @@ -363,9 +363,9 @@ private void createIntegratesDataSource(String dbKey,
// 创建选择器
String selectorName = generateDataSourceSelectorName(dbKey, "integrates");
// 整合数据源不进行负载均衡
healthyDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
illDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
standbyDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
healthyDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
illDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
standbyDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
// 获取整合数据源的配置
MySplitterHighAvailableConfig integrateHaConfig = highAvailableConfig.get("integrate");
// 如果高可用启动,懒加载不启动,创建所有的数据源到healthyDataSourceSelector
Expand Down Expand Up @@ -461,9 +461,9 @@ private void createLoadBalanceSelector(String selectorName, MySplitterLoadBalanc
standbyDataSourceSelectorMap.put(selectorName, new RandomLoadBalanceSelector<DataSourceWrapper>());
}
} else {
healthyDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
illDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
standbyDataSourceSelectorMap.put(selectorName, new NoneLoadBalanceSelector<DataSourceWrapper>());
healthyDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
illDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
standbyDataSourceSelectorMap.put(selectorName, new NoLoadBalanceSelector<DataSourceWrapper>());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
* 无负载均衡选择器
*/
public class NoneLoadBalanceSelector<T> extends AbstractLoadBalanceSelector<T> {
public class NoLoadBalanceSelector<T> extends AbstractLoadBalanceSelector<T> {

private List<T> list = new CopyOnWriteArrayList<T>();

private AtomicInteger atomicFetch = new AtomicInteger(0);

@Override
public synchronized void register(T object, int weight) {
if (!list.contains(object)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
Expand Down Expand Up @@ -57,12 +59,12 @@ private ConfigurationUtil() {
}

public static MySplitterRootConfig getMySplitterConfig(String fileName) throws Exception {
// 读取配置文件
final InputStream resource = ConfigurationUtil.class.getClassLoader().getResourceAsStream(fileName);
// 如果配置文件不存在报错
if (resource == null) {
if (fileName == null || !new File(fileName).exists()) {
throw new FileNotFoundException("MySplitter configuration file " + fileName + " not found!");
}
// 读取配置文件
final InputStream resource = new FileInputStream(new File(fileName));
// 饿汉式加载配置对象
Yaml yaml = new Yaml(new Constructor() {
@Override
Expand Down

0 comments on commit b3ff06b

Please sign in to comment.