-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
cf92d19
commit 6b7fb05
Showing
8 changed files
with
201 additions
and
101 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...ain/java/com/alibaba/nacos/config/server/service/datasource/DataSourcePoolProperties.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,84 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.config.server.service.datasource; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.springframework.boot.context.properties.bind.Bindable; | ||
import org.springframework.boot.context.properties.bind.Binder; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* DataSource pool properties. | ||
* | ||
* <p>Nacos server use HikariCP as the datasource pool. So the basic pool properties will based on {@link | ||
* com.zaxxer.hikari.HikariDataSource}. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class DataSourcePoolProperties { | ||
|
||
public static final long DEFAULT_CONNECTION_TIMEOUT = TimeUnit.SECONDS.toMillis(30L); | ||
|
||
public static final long DEFAULT_VALIDATION_TIMEOUT = TimeUnit.SECONDS.toMillis(10L); | ||
|
||
public static final int DEFAULT_MAX_POOL_SIZE = 20; | ||
|
||
public static final int DEFAULT_MINIMUM_IDLE = 2; | ||
|
||
private final HikariDataSource dataSource; | ||
|
||
private DataSourcePoolProperties() { | ||
dataSource = new HikariDataSource(); | ||
dataSource.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT); | ||
dataSource.setValidationTimeout(DEFAULT_VALIDATION_TIMEOUT); | ||
dataSource.setMaximumPoolSize(DEFAULT_MAX_POOL_SIZE); | ||
dataSource.setMinimumIdle(DEFAULT_MINIMUM_IDLE); | ||
} | ||
|
||
/** | ||
* Build new Hikari config. | ||
* | ||
* @return new hikari config | ||
*/ | ||
public static DataSourcePoolProperties build(Environment environment) { | ||
DataSourcePoolProperties result = new DataSourcePoolProperties(); | ||
Binder.get(environment).bind("db.pool.config", Bindable.ofInstance(result.getDataSource())); | ||
return result; | ||
} | ||
|
||
public void setDriverClassName(final String driverClassName) { | ||
dataSource.setDriverClassName(driverClassName); | ||
} | ||
|
||
public void setJdbcUrl(final String jdbcUrl) { | ||
dataSource.setJdbcUrl(jdbcUrl); | ||
} | ||
|
||
public void setUsername(final String username) { | ||
dataSource.setUsername(username); | ||
} | ||
|
||
public void setPassword(final String password) { | ||
dataSource.setPassword(password); | ||
} | ||
|
||
public HikariDataSource getDataSource() { | ||
return dataSource; | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
...java/com/alibaba/nacos/config/server/service/datasource/DataSourcePoolPropertiesTest.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,68 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.config.server.service.datasource; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DataSourcePoolPropertiesTest { | ||
|
||
private static final String JDBC_URL = "jdbc:derby://127.0.0.1:3306/nacos_devtest?characterEncoding=utf8&serverTimezone=UTC"; | ||
|
||
private static final String JDBC_DRIVER_CLASS_NAME = "org.apache.derby.jdbc.EmbeddedDriver"; | ||
|
||
private static final String PASSWORD = "nacos"; | ||
|
||
private static final String USERNAME = "nacos_devtest"; | ||
|
||
private static final Long CONNECTION_TIMEOUT = 10000L; | ||
|
||
private static final Integer MAX_POOL_SIZE = 50; | ||
|
||
private MockEnvironment environment; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
environment = new MockEnvironment(); | ||
environment.setProperty("db.user", USERNAME); | ||
environment.setProperty("db.password", PASSWORD); | ||
environment.setProperty("db.pool.config.connectionTimeout", CONNECTION_TIMEOUT.toString()); | ||
environment.setProperty("db.pool.config.maximumPoolSize", MAX_POOL_SIZE.toString()); | ||
} | ||
|
||
@Test | ||
public void testBuild() { | ||
DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment); | ||
poolProperties.setJdbcUrl(JDBC_URL); | ||
poolProperties.setDriverClassName(JDBC_DRIVER_CLASS_NAME); | ||
poolProperties.setUsername(USERNAME); | ||
poolProperties.setPassword(PASSWORD); | ||
HikariDataSource actual = poolProperties.getDataSource(); | ||
assertEquals(JDBC_URL, actual.getJdbcUrl()); | ||
assertEquals(JDBC_DRIVER_CLASS_NAME, actual.getDriverClassName()); | ||
assertEquals(USERNAME, actual.getUsername()); | ||
assertEquals(PASSWORD, actual.getPassword()); | ||
assertEquals(CONNECTION_TIMEOUT.longValue(), actual.getConnectionTimeout()); | ||
assertEquals(DataSourcePoolProperties.DEFAULT_VALIDATION_TIMEOUT, actual.getValidationTimeout()); | ||
assertEquals(MAX_POOL_SIZE.intValue(), actual.getMaximumPoolSize()); | ||
assertEquals(DataSourcePoolProperties.DEFAULT_MINIMUM_IDLE, actual.getMinimumIdle()); | ||
} | ||
} |
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
Oops, something went wrong.