From cda3bbc0dd13f02e9eeccd4bbe2fca0c5cbfb9c3 Mon Sep 17 00:00:00 2001 From: Jonathan Henrique Medeiros Date: Fri, 3 May 2024 00:23:19 -0300 Subject: [PATCH] feature: simplifying configurations and upgrade dependencies --- Makefile | 5 ++- README.md | 2 +- docker-compose.yaml | 2 +- pom.xml | 6 ++-- .../datasource/DataSourceConfiguration.java | 31 ++++++----------- .../DataSourceRoutingConfiguration.java | 9 +++-- .../TransactionRoutingDataSource.java | 3 +- .../{ => master}/MasterDataSource.java | 2 +- .../MasterDataSourceConfiguration.java | 34 +++---------------- .../{ => replica}/ReplicaDataSource.java | 2 +- .../ReplicaDataSourceConfiguration.java | 33 +++--------------- .../config/flyway/FlywayConfiguration.java | 2 +- .../multidatasources/model/Billionaire.java | 6 ++-- .../model/IdempotentEntity.java | 10 +++--- src/test/resources/application-test.yaml | 4 +-- 15 files changed, 48 insertions(+), 103 deletions(-) rename src/main/java/br/com/multidatasources/config/{routing => datasource}/DataSourceRoutingConfiguration.java (78%) rename src/main/java/br/com/multidatasources/config/{routing => datasource}/TransactionRoutingDataSource.java (96%) rename src/main/java/br/com/multidatasources/config/datasource/{ => master}/MasterDataSource.java (88%) rename src/main/java/br/com/multidatasources/config/datasource/{ => master}/MasterDataSourceConfiguration.java (59%) rename src/main/java/br/com/multidatasources/config/datasource/{ => replica}/ReplicaDataSource.java (88%) rename src/main/java/br/com/multidatasources/config/datasource/{ => replica}/ReplicaDataSourceConfiguration.java (62%) diff --git a/Makefile b/Makefile index 809d409..ac616ab 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -.PHONY: install test version up down restart upgrade_otel_agent +.PHONY: clean install test version up down restart upgrade_otel_agent + +clean: + @mvn clean install: @mvn clean install diff --git a/README.md b/README.md index c707c08..d4f216f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Esta arquitetura consiste em diminuir a concorrência entre transações da API
[![node](https://img.shields.io/badge/Azul_Zulu_OpenJDK-21-red.svg)](https://www.azul.com/downloads/?package=jdk#zulu) -[![node](https://img.shields.io/badge/Spring_Boot-3.2.2-green.svg)](https://spring.io/) +[![node](https://img.shields.io/badge/Spring_Boot-3.2.5-green.svg)](https://spring.io/) [![node](https://img.shields.io/badge/MySQL-8.0.28-blue.svg)](https://www.mysql.com/) diff --git a/docker-compose.yaml b/docker-compose.yaml index 476783c..c2ec254 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,4 @@ -version: "3.8" +version: "3.9" services: diff --git a/pom.xml b/pom.xml index 4c6ecb4..7749472 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.2 + 3.2.5 @@ -20,9 +20,9 @@ ${java.version} 1.5.5.Final 7.4 - 2.3.0 + 2.5.0 2.2 - 2.2.220 + 2.2.224 1.35.0 2.9.0 diff --git a/src/main/java/br/com/multidatasources/config/datasource/DataSourceConfiguration.java b/src/main/java/br/com/multidatasources/config/datasource/DataSourceConfiguration.java index ca6ba90..4d19152 100644 --- a/src/main/java/br/com/multidatasources/config/datasource/DataSourceConfiguration.java +++ b/src/main/java/br/com/multidatasources/config/datasource/DataSourceConfiguration.java @@ -1,23 +1,13 @@ package br.com.multidatasources.config.datasource; -import javax.sql.DataSource; - import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; -public interface DataSourceConfiguration { - - String poolName(); - - int minimumIdle(); - - int maximumPoolSize(); - - long connectionTimeout(); +import javax.sql.DataSource; - long idleTimeout(); +public interface DataSourceConfiguration { - long maxLifetime(); + DataSourceType dataSourceType(); default HikariDataSource definePoolDataSourceConnection(final DataSource dataSource) { return new HikariDataSource(hikariConfig(dataSource)); @@ -25,13 +15,14 @@ default HikariDataSource definePoolDataSourceConnection(final DataSource dataSou private HikariConfig hikariConfig(final DataSource dataSource) { final HikariConfig hikariConfig = new HikariConfig(); - - hikariConfig.setPoolName(poolName()); - hikariConfig.setMaximumPoolSize(maximumPoolSize()); - hikariConfig.setMinimumIdle(minimumIdle()); - hikariConfig.setConnectionTimeout(connectionTimeout()); - hikariConfig.setMaxLifetime(maxLifetime()); - hikariConfig.setIdleTimeout(idleTimeout()); + final DataSourceType dataSourceType = dataSourceType(); + + hikariConfig.setPoolName(dataSourceType.poolName()); + hikariConfig.setMaximumPoolSize(dataSourceType.maximumPoolSize()); + hikariConfig.setMinimumIdle(dataSourceType.minimumIdle()); + hikariConfig.setConnectionTimeout(dataSourceType.connectionTimeout()); + hikariConfig.setMaxLifetime(dataSourceType.maxLifetime()); + hikariConfig.setIdleTimeout(dataSourceType.idleTimeout()); hikariConfig.setDataSource(dataSource); hikariConfig.setAutoCommit(false); diff --git a/src/main/java/br/com/multidatasources/config/routing/DataSourceRoutingConfiguration.java b/src/main/java/br/com/multidatasources/config/datasource/DataSourceRoutingConfiguration.java similarity index 78% rename from src/main/java/br/com/multidatasources/config/routing/DataSourceRoutingConfiguration.java rename to src/main/java/br/com/multidatasources/config/datasource/DataSourceRoutingConfiguration.java index b282660..a73d9db 100644 --- a/src/main/java/br/com/multidatasources/config/routing/DataSourceRoutingConfiguration.java +++ b/src/main/java/br/com/multidatasources/config/datasource/DataSourceRoutingConfiguration.java @@ -1,8 +1,7 @@ -package br.com.multidatasources.config.routing; +package br.com.multidatasources.config.datasource; -import br.com.multidatasources.config.datasource.DataSourceType; -import br.com.multidatasources.config.datasource.MasterDataSource; -import br.com.multidatasources.config.datasource.ReplicaDataSource; +import br.com.multidatasources.config.datasource.master.MasterDataSource; +import br.com.multidatasources.config.datasource.replica.ReplicaDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -19,7 +18,7 @@ public class DataSourceRoutingConfiguration { public TransactionRoutingDataSource routingDataSource( @MasterDataSource final DataSource masterDataSource, @ReplicaDataSource final DataSource replicaDataSource - ) { + ) { final var routingDataSource = new TransactionRoutingDataSource(); final Map dataSourceMap = new HashMap<>(); diff --git a/src/main/java/br/com/multidatasources/config/routing/TransactionRoutingDataSource.java b/src/main/java/br/com/multidatasources/config/datasource/TransactionRoutingDataSource.java similarity index 96% rename from src/main/java/br/com/multidatasources/config/routing/TransactionRoutingDataSource.java rename to src/main/java/br/com/multidatasources/config/datasource/TransactionRoutingDataSource.java index d5517a6..ba01996 100644 --- a/src/main/java/br/com/multidatasources/config/routing/TransactionRoutingDataSource.java +++ b/src/main/java/br/com/multidatasources/config/datasource/TransactionRoutingDataSource.java @@ -1,4 +1,4 @@ -package br.com.multidatasources.config.routing; +package br.com.multidatasources.config.datasource; import io.opentelemetry.api.trace.Span; import org.slf4j.Logger; @@ -16,7 +16,6 @@ public class TransactionRoutingDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { - LOGGER.info("Routed to: {}", READ_ONLY); enrichSpan(READ_ONLY.name(), READ_ONLY.poolName()); diff --git a/src/main/java/br/com/multidatasources/config/datasource/MasterDataSource.java b/src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSource.java similarity index 88% rename from src/main/java/br/com/multidatasources/config/datasource/MasterDataSource.java rename to src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSource.java index 5985af6..75ad671 100644 --- a/src/main/java/br/com/multidatasources/config/datasource/MasterDataSource.java +++ b/src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSource.java @@ -1,4 +1,4 @@ -package br.com.multidatasources.config.datasource; +package br.com.multidatasources.config.datasource.master; import org.springframework.beans.factory.annotation.Qualifier; diff --git a/src/main/java/br/com/multidatasources/config/datasource/MasterDataSourceConfiguration.java b/src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSourceConfiguration.java similarity index 59% rename from src/main/java/br/com/multidatasources/config/datasource/MasterDataSourceConfiguration.java rename to src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSourceConfiguration.java index 37054b5..b168623 100644 --- a/src/main/java/br/com/multidatasources/config/datasource/MasterDataSourceConfiguration.java +++ b/src/main/java/br/com/multidatasources/config/datasource/master/MasterDataSourceConfiguration.java @@ -1,5 +1,7 @@ -package br.com.multidatasources.config.datasource; +package br.com.multidatasources.config.datasource.master; +import br.com.multidatasources.config.datasource.DataSourceConfiguration; +import br.com.multidatasources.config.datasource.DataSourceType; import br.com.multidatasources.config.properties.datasource.DatabaseConnectionProperties; import br.com.multidatasources.config.properties.datasource.MasterProperties; import org.springframework.context.annotation.Bean; @@ -8,40 +10,14 @@ import javax.sql.DataSource; -import static br.com.multidatasources.config.datasource.DataSourceType.READ_ONLY; import static br.com.multidatasources.config.datasource.DataSourceType.READ_WRITE; @Configuration public class MasterDataSourceConfiguration implements DataSourceConfiguration { @Override - public String poolName() { - return READ_WRITE.poolName(); - } - - @Override - public int minimumIdle() { - return READ_WRITE.minimumIdle(); - } - - @Override - public int maximumPoolSize() { - return READ_WRITE.maximumPoolSize(); - } - - @Override - public long idleTimeout() { - return READ_ONLY.idleTimeout(); - } - - @Override - public long connectionTimeout() { - return READ_WRITE.connectionTimeout(); - } - - @Override - public long maxLifetime() { - return READ_WRITE.maxLifetime(); + public DataSourceType dataSourceType() { + return READ_WRITE; } @Bean diff --git a/src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSource.java b/src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSource.java similarity index 88% rename from src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSource.java rename to src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSource.java index d85a355..8e08b90 100644 --- a/src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSource.java +++ b/src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSource.java @@ -1,4 +1,4 @@ -package br.com.multidatasources.config.datasource; +package br.com.multidatasources.config.datasource.replica; import org.springframework.beans.factory.annotation.Qualifier; diff --git a/src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSourceConfiguration.java b/src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSourceConfiguration.java similarity index 62% rename from src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSourceConfiguration.java rename to src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSourceConfiguration.java index bb49d52..30b7631 100644 --- a/src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSourceConfiguration.java +++ b/src/main/java/br/com/multidatasources/config/datasource/replica/ReplicaDataSourceConfiguration.java @@ -1,5 +1,7 @@ -package br.com.multidatasources.config.datasource; +package br.com.multidatasources.config.datasource.replica; +import br.com.multidatasources.config.datasource.DataSourceConfiguration; +import br.com.multidatasources.config.datasource.DataSourceType; import br.com.multidatasources.config.properties.datasource.DatabaseConnectionProperties; import br.com.multidatasources.config.properties.datasource.ReplicaProperties; import org.springframework.context.annotation.Bean; @@ -14,33 +16,8 @@ public class ReplicaDataSourceConfiguration implements DataSourceConfiguration { @Override - public String poolName() { - return READ_ONLY.poolName(); - } - - @Override - public int minimumIdle() { - return READ_ONLY.minimumIdle(); - } - - @Override - public int maximumPoolSize() { - return READ_ONLY.maximumPoolSize(); - } - - @Override - public long connectionTimeout() { - return READ_ONLY.connectionTimeout(); - } - - @Override - public long idleTimeout() { - return READ_ONLY.idleTimeout(); - } - - @Override - public long maxLifetime() { - return READ_ONLY.maxLifetime(); + public DataSourceType dataSourceType() { + return READ_ONLY; } @Bean diff --git a/src/main/java/br/com/multidatasources/config/flyway/FlywayConfiguration.java b/src/main/java/br/com/multidatasources/config/flyway/FlywayConfiguration.java index df5a379..1e2bb63 100644 --- a/src/main/java/br/com/multidatasources/config/flyway/FlywayConfiguration.java +++ b/src/main/java/br/com/multidatasources/config/flyway/FlywayConfiguration.java @@ -1,6 +1,6 @@ package br.com.multidatasources.config.flyway; -import br.com.multidatasources.config.datasource.MasterDataSource; +import br.com.multidatasources.config.datasource.master.MasterDataSource; import br.com.multidatasources.config.properties.flyway.FlywayProperties; import org.flywaydb.core.Flyway; import org.flywaydb.core.api.MigrationVersion; diff --git a/src/main/java/br/com/multidatasources/model/Billionaire.java b/src/main/java/br/com/multidatasources/model/Billionaire.java index df6178c..371d06e 100644 --- a/src/main/java/br/com/multidatasources/model/Billionaire.java +++ b/src/main/java/br/com/multidatasources/model/Billionaire.java @@ -9,12 +9,12 @@ @Entity @Table(name = "billionaire") -public class Billionaire extends IdempotentEntity { +public class Billionaire extends IdempotentEntity { - @Column(name = "first_name") + @Column(name = "first_name", nullable = false) private String firstName; - @Column(name = "last_name") + @Column(name = "last_name", nullable = false) private String lastName; private String career; diff --git a/src/main/java/br/com/multidatasources/model/IdempotentEntity.java b/src/main/java/br/com/multidatasources/model/IdempotentEntity.java index b858b20..e2cc753 100644 --- a/src/main/java/br/com/multidatasources/model/IdempotentEntity.java +++ b/src/main/java/br/com/multidatasources/model/IdempotentEntity.java @@ -12,23 +12,23 @@ import java.util.UUID; @MappedSuperclass -public abstract class IdempotentEntity { +public abstract class IdempotentEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private T id; + private Long id; - @Column(name = "idempotency_id") + @Column(name = "idempotency_id", nullable = false, updatable = false) @Convert(converter = IdempotencyIdConverter.class) private UUID idempotencyId; public abstract void generateIdempotencyId(final IdempotencyGenerator generator); - public T getId() { + public Long getId() { return id; } - public void setId(final T id) { + public void setId(final Long id) { this.id = id; } diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index 26642ad..740c9d0 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -8,7 +8,7 @@ schema: master: datasource: host: localhost - url: jdbc:h2:mem:${schema.name};DB_CLOSE_DELAY=-1 + url: jdbc:h2:mem:${schema.name};MODE=MYSQL;DB_CLOSE_DELAY=-1 username: ${schema.user} password: ${schema.pass} @@ -16,6 +16,6 @@ master: replica: datasource: host: localhost - url: jdbc:h2:mem:${schema.name};DB_CLOSE_DELAY=-1 + url: jdbc:h2:mem:${schema.name};MODE=MYSQL;DB_CLOSE_DELAY=-1 username: ${schema.user} password: ${schema.pass}