From c4ef68430938579e7f173a61afa6c3593a72c191 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Wed, 5 Apr 2023 12:48:58 -0400 Subject: [PATCH] fixes #1692 add db-provider module --- db-provider/README.md | 152 ++++++++++++++++++ db-provider/pom.xml | 77 +++++++++ .../src/main/resources/config/db-provider.yml | 8 + .../src/test/resources/config/service.yml | 35 ++++ .../src/test/resources/config/values.yml | 0 .../src/test/resources/logback-test.xml | 46 ++++++ pom.xml | 10 +- 7 files changed, 326 insertions(+), 2 deletions(-) create mode 100644 db-provider/README.md create mode 100644 db-provider/pom.xml create mode 100644 db-provider/src/main/resources/config/db-provider.yml create mode 100644 db-provider/src/test/resources/config/service.yml create mode 100644 db-provider/src/test/resources/config/values.yml create mode 100644 db-provider/src/test/resources/logback-test.xml diff --git a/db-provider/README.md b/db-provider/README.md new file mode 100644 index 0000000000..bac816b2db --- /dev/null +++ b/db-provider/README.md @@ -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: + +``` + + com.zaxxer + HikariCP + + +``` + +You also need to add a database driver as part of your application dependency. For example, here is the postgresql. + + +``` + + org.postgresql + postgresql + +``` + +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. + +``` + + com.github.ben-manes.caffeine + caffeine + +``` + +### 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 diff --git a/db-provider/pom.xml b/db-provider/pom.xml new file mode 100644 index 0000000000..168e8ad179 --- /dev/null +++ b/db-provider/pom.xml @@ -0,0 +1,77 @@ + + + + 4.0.0 + + + com.networknt + light-4j + 2.1.10-SNAPSHOT + ../pom.xml + + + db-provider + jar + A provider interface to help developers to start an API that connection to one database. + + + + com.networknt + config + + + org.slf4j + slf4j-api + + + com.github.ben-manes.caffeine + caffeine + + + com.zaxxer + HikariCP + + + + com.networknt + service + test + + + ch.qos.logback + logback-classic + test + + + junit + junit + test + + + org.postgresql + postgresql + test + + + com.h2database + h2 + test + + + + diff --git a/db-provider/src/main/resources/config/db-provider.yml b/db-provider/src/main/resources/config/db-provider.yml new file mode 100644 index 0000000000..346940b316 --- /dev/null +++ b/db-provider/src/main/resources/config/db-provider.yml @@ -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} diff --git a/db-provider/src/test/resources/config/service.yml b/db-provider/src/test/resources/config/service.yml new file mode 100644 index 0000000000..506029f16a --- /dev/null +++ b/db-provider/src/test/resources/config/service.yml @@ -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 \ No newline at end of file diff --git a/db-provider/src/test/resources/config/values.yml b/db-provider/src/test/resources/config/values.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/db-provider/src/test/resources/logback-test.xml b/db-provider/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..aa80300f93 --- /dev/null +++ b/db-provider/src/test/resources/logback-test.xml @@ -0,0 +1,46 @@ + + + + + + PROFILER + NEUTRAL + + + + + %d{HH:mm:ss.SSS} [%thread] %-5marker %-5level %class{36}:%L %M - %msg%n + + + + + target/test.log + false + + %d{HH:mm:ss.SSS} [%thread] %-5level %class{36}:%L %M - %msg%n + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 196b63dcb3..dfa793a8df 100644 --- a/pom.xml +++ b/pom.xml @@ -87,9 +87,9 @@ 0.6.0 1.6.1 5.0.1 - 2.1.210 + 2.1.214 8.0.32 - 42.5.3 + 42.6.0 11.2.0.3 7.0.0.jre10 4.5.13 @@ -171,6 +171,7 @@ api-key ldap-util cache-manager + db-provider @@ -438,6 +439,11 @@ cache-manager ${project.version} + + com.networknt + db-provider + ${project.version} +