-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verifier-data-app): config multi datasource
- Loading branch information
1 parent
05e220b
commit 8c9b43f
Showing
8 changed files
with
237 additions
and
0 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
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,38 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' version '3.2.2' | ||
id 'io.spring.dependency-management' version '1.1.4' | ||
} | ||
|
||
group = 'org.cardanofoundation.ledgersync' | ||
version = '0.0.1-SNAPSHOT' | ||
|
||
java { | ||
sourceCompatibility = '21' | ||
} | ||
|
||
configurations { | ||
compileOnly { | ||
extendsFrom annotationProcessor | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
compileOnly 'org.projectlombok:lombok' | ||
developmentOnly 'org.springframework.boot:spring-boot-devtools' | ||
runtimeOnly 'org.postgresql:postgresql' | ||
annotationProcessor 'org.projectlombok:lombok' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
|
||
implementation project(':components:consumer-common') | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
13 changes: 13 additions & 0 deletions
13
...n/java/org/cardanofoundation/ledgersync/verifier/data/app/VerifierDataAppApplication.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,13 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class VerifierDataAppApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(VerifierDataAppApplication.class, args); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ava/org/cardanofoundation/ledgersync/verifier/data/app/config/DbSyncDatasourceConfig.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,51 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef = "dbSyncEntityManagerFactory", | ||
transactionManagerRef = "dbSyncTransactionManager", | ||
basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.dbsync"}) | ||
@RequiredArgsConstructor | ||
public class DbSyncDatasourceConfig { | ||
|
||
private final MultiDataSourceProperties multiDataSourceProperties; | ||
|
||
@Bean(name = "dbSyncDataSource") | ||
public DataSource ledgerSyncDataSource() { | ||
return multiDataSourceProperties.buildDataSource( | ||
multiDataSourceProperties.getDatasourceDbSync()); | ||
} | ||
|
||
@Bean(name = "dbSyncEntityManagerFactory") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
EntityManagerFactoryBuilder builder, @Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages( | ||
"org.cardanofoundation.ledgersync.verifier.data.app.entity.dbsync") | ||
.build(); | ||
} | ||
|
||
@Bean(name = "dbSyncTransactionManager") | ||
public PlatformTransactionManager dbSyncTransactionManager( | ||
@Qualifier("dbSyncEntityManagerFactory") | ||
LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
return new JpaTransactionManager( | ||
Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...org/cardanofoundation/ledgersync/verifier/data/app/config/LedgerSyncDatasourceConfig.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,60 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef = "ledgerSyncEntityManagerFactory", | ||
transactionManagerRef = "ledgerSyncTransactionManager", | ||
basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.ledgersync"}) | ||
public class LedgerSyncDatasourceConfig { | ||
|
||
private final MultiDataSourceProperties multiDataSourceProperties; | ||
|
||
public LedgerSyncDatasourceConfig(MultiDataSourceProperties multiDataSourceProperties) { | ||
this.multiDataSourceProperties = multiDataSourceProperties; | ||
} | ||
|
||
@Primary | ||
@Bean(name = "ledgerSyncDataSource") | ||
public DataSource ledgerSyncDataSource() { | ||
return multiDataSourceProperties.buildDataSource( | ||
multiDataSourceProperties.getDatasourceLedgerSync()); | ||
} | ||
|
||
@Primary | ||
@Bean(name = "ledgerSyncEntityManagerFactory") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
EntityManagerFactoryBuilder builder, | ||
@Qualifier("ledgerSyncDataSource") DataSource dataSource) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages( | ||
"org.cardanofoundation.ledgersync.consumercommon.entity", | ||
"org.cardanofoundation.ledgersync.consumercommon.enumeration", | ||
"org.cardanofoundation.ledgersync.consumercommon.validation") | ||
.build(); | ||
} | ||
|
||
@Primary | ||
@Bean(name = "ledgerSyncTransactionManager") | ||
public PlatformTransactionManager ledgerSyncTransactionManager( | ||
@Qualifier("ledgerSyncEntityManagerFactory") | ||
LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
return new JpaTransactionManager( | ||
Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../org/cardanofoundation/ledgersync/verifier/data/app/config/MultiDataSourceProperties.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,33 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.FieldDefaults; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Getter | ||
@Setter | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
@Configuration | ||
@ConfigurationProperties(prefix = "multi-datasource") | ||
public class MultiDataSourceProperties { | ||
DataSourceConfig datasourceLedgerSync; | ||
DataSourceConfig datasourceDbSync; | ||
@Data | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public static class DataSourceConfig { | ||
HikariConfig hikariConfig; | ||
} | ||
|
||
public DataSource buildDataSource(DataSourceConfig dataSourceConfig) { | ||
return new HikariDataSource(dataSourceConfig.getHikariConfig()); | ||
} | ||
} |
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,28 @@ | ||
#first db | ||
multi-datasource: | ||
datasource-ledger-sync: | ||
hikari-config: | ||
jdbc-url: jdbc:postgresql://${LEDGER_SYNC_HOST:10.4.10.112}:${LEDGER_SYNC_PORT:54333}/${LEDGER_SYNC_DB:ledger_sync} | ||
username: ${LEDGER_SYNC_USER_NAME:cardano-master} | ||
password: ${LEDGER_SYNC_PASSWORD:postgres_master_pw} | ||
driver-class-name: org.postgresql.Driver | ||
schema: ${LEDGER_SYNC_SCHEMA:mainnet} | ||
datasource-db-sync: | ||
hikari-config: | ||
jdbc-url: jdbc:postgresql://${DB_SYNC_HOST:10.4.10.231}:${DB_SYNC_PORT:5434}/${DB_SYNC_DB:cardano_mainnet} | ||
username: ${DB_SYNC_USER_NAME:cardano_mainnet_read} | ||
password: ${DB_SYNC_PASSWORD:1} | ||
driver-class-name: org.postgresql.Driver | ||
schema: ${DB_SYNC_SCHEMA:public} | ||
spring: | ||
jpa: | ||
open-in-view: false | ||
database-platform: org.hibernate.dialect.PostgreSQLDialect | ||
properties: | ||
hibernate: | ||
dialect: org.hibernate.dialect.PostgreSQLDialect | ||
show_sql: true | ||
format_sql: true | ||
|
||
|
||
|
13 changes: 13 additions & 0 deletions
13
...a/org/cardanofoundation/ledgersync/verifier/data/app/VerifierDataAppApplicationTests.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,13 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class VerifierDataAppApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |