Skip to content

Commit

Permalink
feature: simplifying configurations and upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Henrique Medeiros committed May 3, 2024
1 parent 940c9dd commit cda3bbc
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 103 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Esta arquitetura consiste em diminuir a concorrência entre transações da API
</br>

[![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/)


Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.8"
version: "3.9"

services:

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<version>3.2.5</version>
<relativePath/>
</parent>

Expand All @@ -20,9 +20,9 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<logstash-logback-encoder.version>7.4</logstash-logback-encoder.version>
<springdoc-openapi-starter-webmvc-ui.version>2.3.0</springdoc-openapi-starter-webmvc-ui.version>
<springdoc-openapi-starter-webmvc-ui.version>2.5.0</springdoc-openapi-starter-webmvc-ui.version>
<snakeyaml.version>2.2</snakeyaml.version>
<h2.version>2.2.220</h2.version>
<h2.version>2.2.224</h2.version>
<opentelemetry.version>1.35.0</opentelemetry.version>
<json-path.version>2.9.0</json-path.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
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));
}

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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object, Object> dataSourceMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.multidatasources.config.datasource;
package br.com.multidatasources.config.datasource.master;

import org.springframework.beans.factory.annotation.Qualifier;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.multidatasources.config.datasource;
package br.com.multidatasources.config.datasource.replica;

import org.springframework.beans.factory.annotation.Qualifier;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/br/com/multidatasources/model/Billionaire.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

@Entity
@Table(name = "billionaire")
public class Billionaire extends IdempotentEntity<Long> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
import java.util.UUID;

@MappedSuperclass
public abstract class IdempotentEntity<T> {
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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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}

# DATABASE REPLICA PROPERTIES
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}

0 comments on commit cda3bbc

Please sign in to comment.