forked from networknt/light-4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes networknt#1692 add db-provider module
- Loading branch information
Showing
7 changed files
with
326 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
## data-source module | ||
|
||
The module provides one database configuration and wraps it up with an interface so that application can extend it and implement all the db access along with caches. Unlike the data-source module that supports multiple data sources, it only support one database. Also, it provides an interface with default provider implementation to allow the developer to quickly start a simple database related API. | ||
|
||
|
||
|
||
### Implementation detail: | ||
|
||
light-4j db-provider module use lightweight java DB connection library (HikariCP) as datasource base: | ||
|
||
``` | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
</dependency> | ||
``` | ||
|
||
You also need to add a database driver as part of your application dependency. For example, here is the postgresql. | ||
|
||
|
||
``` | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
</dependency> | ||
``` | ||
|
||
In most cases, you need to cache some of the rows from the database in order to speed up the response from your API. You should include Caffeine as the cache layer. | ||
|
||
``` | ||
<dependency> | ||
<groupId>com.github.ben-manes.caffeine</groupId> | ||
<artifactId>caffeine</artifactId> | ||
</dependency> | ||
``` | ||
|
||
### Database Configuration | ||
|
||
There is a config file db-provider.yml that contains all the information that you can create a HiKariCP datasource. | ||
|
||
Here is an example. | ||
|
||
``` | ||
# For postgres database running in a docker container, you have to use the driverClassName. By | ||
# using the dataSourceClassName, you cannot connect to the database from another docker container. | ||
driverClassName: ${db.driverClassName:org.postgresql.Driver} | ||
jdbcUrl: ${db.jdbcUrl:jdbc:postgresql://timescale:5432/configserver} | ||
username: ${db.username:postgres} | ||
password: ${db.password:secret} | ||
maximumPoolSize: ${db.maximumPoolSize:3} | ||
``` | ||
|
||
### Module usage detail: | ||
|
||
There are two config files involve withe the setting (datasource.yml & service.yml) and both two be extend to use values.yml" | ||
|
||
For example: | ||
|
||
datasource.yml | ||
|
||
``` | ||
MysqlDataSource: ${datasource.MysqlDataSource:} | ||
``` | ||
|
||
service.yml | ||
|
||
``` | ||
singletons: ${service.singletons:} | ||
``` | ||
|
||
values.yml | ||
|
||
``` | ||
# Service Singletons | ||
service.singletons: | ||
- com.networknt.decrypt.Decryptor: | ||
- com.networknt.decrypt.AESDecryptor | ||
- com.networknt.db.GenericDataSource: | ||
- com.networknt.db.MysqlDataSource: | ||
- java.lang.String: | ||
- com.networknt.accountservic.dao.AccountDao: | ||
- com.networknt.accountservic.dao.AccountDaoImpl | ||
# datasource.yml | ||
datasource.MysqlDataSource: | ||
DriverClassName: com.mysql.jdbc.Driver | ||
jdbcUrl: jdbc:mysql://localhost:3308/account_db?useSSL=false | ||
username: account_user | ||
password: CRYPT:odPqWOazjDxeVcOU3j0YCc2+LdwfgiJmoFcWTSoKRUw= | ||
maximumPoolSize: 2 | ||
connectionTimeout: 5000 | ||
settings: | ||
idleTimeout: 50000 | ||
minimumIdle: 1 | ||
parameters: | ||
useServerPrepStmts: 'true' | ||
cachePrepStmts: 'true' | ||
cacheCallableStmts: 'true' | ||
prepStmtCacheSize: '4096' | ||
prepStmtCacheSqlLimit: '2048' | ||
verifyServerCertificate: 'false' | ||
useSSL: 'true' | ||
requireSSL: 'true' | ||
``` | ||
The Database config value include three parts: | ||
|
||
- main section which include major config for HikariDataSource, for example connection string,username/password, etc. | ||
|
||
- settings section, which used to invoke HikariDataSource set methods for the specified parameters. | ||
for example, if there is a parameter: idleTimeout: 50000 | ||
it will call HikariDataSource setIdleTimeout() method for specified value | ||
|
||
- parameters section which used to specify the datasource config properties for HikariDataSource(addDataSourceProperty()) | ||
|
||
DAO java class: | ||
|
||
``` | ||
private DataSource dataSource; | ||
public AccountDaoImpl(GenericDataSource genericDataSource) { | ||
dataSource = genericDataSource.getDataSource(); | ||
} | ||
``` | ||
|
||
- By using datasource factory | ||
|
||
service.yml | ||
``` | ||
- com.networknt.db.factory.DataSourceFactory: | ||
- com.networknt.db.factory.DefaultDataSourceFactory | ||
``` | ||
|
||
Get the datasource by default datasource factory | ||
|
||
``` | ||
DataSource dataSource = SingletonServiceFactory.getBean(DataSourceFactory.class).getDataSource("OracleDataSource"); | ||
``` | ||
|
||
|
||
### For the detail document, pleases refer to | ||
|
||
* [Tutorial document](https://doc.networknt.com/concern/datasource/#readout) with step by step guide to set and use datasources |
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,77 @@ | ||
<!-- | ||
~ Copyright (c) 2016 Network New Technologies Inc. | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.networknt</groupId> | ||
<artifactId>light-4j</artifactId> | ||
<version>2.1.10-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>db-provider</artifactId> | ||
<packaging>jar</packaging> | ||
<description>A provider interface to help developers to start an API that connection to one database.</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.networknt</groupId> | ||
<artifactId>config</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.ben-manes.caffeine</groupId> | ||
<artifactId>caffeine</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.networknt</groupId> | ||
<artifactId>service</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
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,8 @@ | ||
# db-provider.yml | ||
# For postgres database running in a docker container, you have to use the driverClassName. By | ||
# using the dataSourceClassName, you cannot connect to the database from another docker container. | ||
driverClassName: ${db-provider.driverClassName:org.postgresql.Driver} | ||
jdbcUrl: ${db-provider.jdbcUrl:jdbc:postgresql://timescale:5432/configserver} | ||
username: ${db-provider.username:postgres} | ||
password: ${db-provider.password:secret} | ||
maximumPoolSize: ${db-provider.maximumPoolSize:3} |
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,35 @@ | ||
# Singleton service factory configuration | ||
|
||
singletons: | ||
- javax.sql.DataSource: com.networknt.db.H2DataSource::getDataSource | ||
|
||
- com.networknt.db.MysqlDataSource: | ||
- com.networknt.db.MysqlDataSource: | ||
- java.lang.String: MysqlDataSource | ||
|
||
- com.networknt.db.CustomMysqlDataSource: | ||
- com.networknt.db.CustomMysqlDataSource: | ||
- java.lang.String: CustomMysqlDataSource | ||
|
||
- com.networknt.db.MariaDataSource: | ||
- com.networknt.db.MariaDataSource: | ||
- java.lang.String: MariaDataSource | ||
|
||
- com.networknt.db.PostgresDataSource: | ||
- com.networknt.db.PostgresDataSource: | ||
- java.lang.String: PostgresDataSource | ||
|
||
- com.networknt.db.SqlServerDataSource: | ||
- com.networknt.db.SqlServerDataSource: | ||
- java.lang.String: SqlServerDataSource | ||
|
||
- com.networknt.db.OracleDataSource: | ||
- com.networknt.db.OracleDataSource: | ||
- java.lang.String: OracleDataSource | ||
|
||
- com.networknt.db.H2DataSource: | ||
- com.networknt.db.H2DataSource: | ||
- java.lang.String: H2DataSource | ||
|
||
- com.networknt.db.factory.DataSourceFactory: | ||
- com.networknt.db.factory.DefaultDataSourceFactory |
Empty file.
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,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2016 Network New Technologies Inc. | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<configuration> | ||
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter"> | ||
<Marker>PROFILER</Marker> | ||
<OnMatch>NEUTRAL</OnMatch> | ||
</turboFilter> | ||
|
||
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5marker %-5level %class{36}:%L %M - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<appender name="log" class="ch.qos.logback.core.FileAppender"> | ||
<File>target/test.log</File> | ||
<Append>false</Append> | ||
<layout class="ch.qos.logback.classic.PatternLayout"> | ||
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %class{36}:%L %M - %msg%n</Pattern> | ||
</layout> | ||
</appender> | ||
|
||
<root level="trace"> | ||
<appender-ref ref="stdout" /> | ||
</root> | ||
|
||
<logger name="com.networknt" level="trace" additivity="false"> | ||
<appender-ref ref="stdout"/> | ||
</logger> | ||
|
||
</configuration> |
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