提交 ebdba014 编写于 作者: Y Yanick.xia 提交者: Liang Zhang

Add hikari custom configuration support (#3371)

* Add support ext propertie for different datasource

* Fix unit test

* Fix code style

* Fix code style && reserved spi

* Fix code style
上级 69ae136f
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.shardingjdbc.spring.boot;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.util.InlineExpressionParser;
......@@ -28,6 +29,8 @@ import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.spring.boot.common.SpringBootPropertiesConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.datasource.DataSourcePropertiesSetter;
import org.apache.shardingsphere.shardingjdbc.spring.boot.datasource.HikariDataSourcePropertiesSetter;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.EncryptRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.SpringBootEncryptRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.masterslave.MasterSlaveRuleCondition;
......@@ -72,6 +75,9 @@ import java.util.Map;
@RequiredArgsConstructor
public class SpringBootConfiguration implements EnvironmentAware {
private static final List<DataSourcePropertiesSetter> DATA_SOURCE_PROPERTIES_SETTER_LIST =
ImmutableList.<DataSourcePropertiesSetter>of(new HikariDataSourcePropertiesSetter());
private final SpringBootShardingRuleConfigurationProperties shardingRule;
private final SpringBootMasterSlaveRuleConfigurationProperties masterSlaveRule;
......@@ -148,7 +154,14 @@ public class SpringBootConfiguration implements EnvironmentAware {
if (dataSourceProps.containsKey(jndiName)) {
return getJndiDataSource(dataSourceProps.get(jndiName).toString());
}
return DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
DataSource result = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
for (DataSourcePropertiesSetter dataSourcePropertiesSetter : DATA_SOURCE_PROPERTIES_SETTER_LIST) {
if (dataSourcePropertiesSetter.support(result)) {
dataSourcePropertiesSetter.propertiesSet(environment, prefix, dataSourceName, result);
break;
}
}
return result;
}
private DataSource getJndiDataSource(final String jndiName) throws NamingException {
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.spring.boot.datasource;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
/**
* Different datasource properties setter.
*
* @author xiayan
*/
public interface DataSourcePropertiesSetter {
/**
* Set datasource custom properties.
*
* @param environment environment variable
* @param prefix properties prefix
* @param dataSourceName current database name
* @param dataSource dataSource instance
*/
void propertiesSet(Environment environment, String prefix, String dataSourceName, DataSource dataSource);
/**
* support datasource type.
*
* @param type datasource type
* @return is supported
*/
boolean support(DataSource type);
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.spring.boot.datasource;
import lombok.SneakyThrows;
import org.apache.shardingsphere.shardingjdbc.spring.boot.util.PropertyUtil;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
/**
* Hikari datasource properties setter.
*
* @author xiayan
*/
public final class HikariDataSourcePropertiesSetter implements DataSourcePropertiesSetter {
@Override
@SneakyThrows(ReflectiveOperationException.class)
public void propertiesSet(final Environment environment, final String prefix, final String dataSourceName, final DataSource dataSource) {
Properties properties = new Properties();
properties.putAll(PropertyUtil.handle(environment, prefix + dataSourceName.trim() + ".data-source-properties", Map.class));
Method method = dataSource.getClass().getMethod("setDataSourceProperties", Properties.class);
method.invoke(dataSource, properties);
}
@Override
public boolean support(final DataSource type) {
return type.getClass().getName().equals("com.zaxxer.hikari.HikariDataSource");
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.spring.boot.datasource;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import javax.sql.DataSource;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public final class HikariDataSourcePropertiesSetterTest {
private final HikariDataSourcePropertiesSetter dbcpDataSourcePropertiesSetter = new HikariDataSourcePropertiesSetter();
private final HikariDataSource dataSource = new HikariDataSource();
private Environment environment;
@Before
public void setUp() {
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.type", "com.zaxxer.hikari.HikariDataSource");
mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.data-source-properties.cachePrepStmts", "true");
mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.data-source-properties.prepStmtCacheSize", "250");
environment = mockEnvironment;
}
@Test
public void assertPropertiesSet() {
dbcpDataSourcePropertiesSetter.propertiesSet(environment, "spring.shardingsphere.datasource.", "ds_master", dataSource);
assertThat(dataSource.getDataSourceProperties().getProperty("cachePrepStmts"), is("true"));
assertThat(dataSource.getDataSourceProperties().getProperty("prepStmtCacheSize"), is("250"));
}
@Test
public void assertSupport() {
assertThat(dbcpDataSourcePropertiesSetter.support(dataSource), is(true));
assertThat(dbcpDataSourcePropertiesSetter.support(mock(DataSource.class)), is(false));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册