Skip to content

Commit

Permalink
for apache#658 sharding-config.yaml changed
Browse files Browse the repository at this point in the history
  • Loading branch information
tuohai666 committed Apr 25, 2018
1 parent b4fd823 commit 4976d35
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 io.shardingjdbc.core.yaml.sharding;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Data source parameters.
*
* @author zhangyonglun
*/
@NoArgsConstructor
@Getter
@Setter
public final class DataSourceParameter {

private String url;

private String username;

private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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 io.shardingjdbc.core.yaml.sharding;

import io.shardingjdbc.core.rule.ShardingRule;
import lombok.Getter;
import lombok.Setter;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Yaml sharding configuration for proxy.
*
* @author zhangyonglun
*/
@Getter
@Setter
public class YamlShardingConfigurationForProxy {

private Map<String, DataSourceParameter> dataSourceParameters = new HashMap<>();

private YamlShardingRuleConfiguration shardingRule;

/**
* Unmarshal yaml sharding configuration from yaml file.
*
* @param yamlFile yaml file
* @return yaml sharding configuration
* @throws IOException IO Exception
*/
public static YamlShardingConfigurationForProxy unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamlShardingConfigurationForProxy.class)).loadAs(inputStreamReader, YamlShardingConfigurationForProxy.class);
}
}

/**
* Unmarshal yaml sharding configuration from yaml bytes.
*
* @param yamlBytes yaml bytes
* @return yaml sharding configuration
* @throws IOException IO Exception
*/
public static YamlShardingConfigurationForProxy unmarshal(final byte[] yamlBytes) throws IOException {
try (InputStream inputStream = new ByteArrayInputStream(yamlBytes)) {
return new Yaml(new Constructor(YamlShardingConfigurationForProxy.class)).loadAs(inputStream, YamlShardingConfigurationForProxy.class);
}
}

/**
* Get sharding rule from yaml.
*
* @param dataSourceNames data source names
* @return sharding rule from yaml
*/
public ShardingRule getShardingRule(final Collection<String> dataSourceNames) {
return new ShardingRule(shardingRule.getShardingRuleConfiguration(), dataSourceNames.isEmpty() ? dataSourceParameters.keySet() : dataSourceNames);
}
}
11 changes: 3 additions & 8 deletions sharding-proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,9 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<scope>runtime</scope>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package io.shardingjdbc.proxy.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.metadata.ShardingMetaData;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration;
import io.shardingjdbc.core.yaml.sharding.DataSourceParameter;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfigurationForProxy;
import io.shardingjdbc.proxy.metadata.ProxyShardingMetaData;
import lombok.Getter;

Expand All @@ -29,6 +32,7 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -48,14 +52,18 @@ public final class ShardingRuleRegistry {
private final ShardingMetaData shardingMetaData;

private ShardingRuleRegistry() {
YamlShardingConfiguration yamlShardingConfig;
YamlShardingConfigurationForProxy yamlShardingConfigurationForProxy;
try {
yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(getClass().getResource("/conf/sharding-config.yaml").getFile()));
yamlShardingConfigurationForProxy = YamlShardingConfigurationForProxy.unmarshal(new File(getClass().getResource("/conf/sharding-config.yaml").getFile()));
} catch (final IOException ex) {
throw new ShardingJdbcException(ex);
}
dataSourceMap = yamlShardingConfig.getDataSources();
shardingRule = yamlShardingConfig.getShardingRule(Collections.<String>emptyList());
dataSourceMap = new HashMap<>(128, 1);
Map<String, DataSourceParameter> dataSourceParameters = yamlShardingConfigurationForProxy.getDataSourceParameters();
for (String each : dataSourceParameters.keySet()) {
dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each)));
}
shardingRule = yamlShardingConfigurationForProxy.getShardingRule(Collections.<String>emptyList());
try {
shardingMetaData = new ProxyShardingMetaData(dataSourceMap);
shardingMetaData.init(shardingRule);
Expand All @@ -64,6 +72,22 @@ private ShardingRuleRegistry() {
}
}

private DataSource getDataSource(final DataSourceParameter dataSourceParameter) {
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.jdbc.Driver");
config.setJdbcUrl(dataSourceParameter.getUrl());
config.setUsername(dataSourceParameter.getUsername());
config.setPassword(dataSourceParameter.getPassword());
config.setAutoCommit(true);
config.setConnectionTimeout(30000);
config.setIdleTimeout(60000);
config.setMaxLifetime(1800000);
config.setMaximumPoolSize(100);
config.addDataSourceProperty("useServerPrepStmts", "true");
config.addDataSourceProperty("cachePrepStmts", "true");
return new HikariDataSource(config);
}

/**
* Get instance of sharding rule registry.
*
Expand Down
6 changes: 2 additions & 4 deletions sharding-proxy/src/main/resources/conf/sharding-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
dataSources:
demo_ds_0: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
demo_ds_0:
url: jdbc:mysql://localhost:3306/demo_ds_0
username: root
password:
demo_ds_1: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
demo_ds_1:
url: jdbc:mysql://localhost:3306/demo_ds_1
username: root
password:
Expand Down

0 comments on commit 4976d35

Please sign in to comment.