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 45ccea0
Show file tree
Hide file tree
Showing 21 changed files with 105 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 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -40,6 +41,18 @@ public void gauge(final String metricName, final Map<String, String> tags, final
.register(this.meterRegistry);
}

@Override
public void summary(final String metricName, final Map<String, String> tags, final Number value) {
final var appendedTags = appendDefaultTags(tags);
this.meterRegistry.summary(metricName, toTags(appendedTags)).record(value.doubleValue());
}

@Override
public void timer(final String metricName, final Map<String, String> tags, final Duration value) {
final var appendedTags = appendDefaultTags(tags);
this.meterRegistry.timer(metricName, toTags(appendedTags)).record(value);
}

private Map<String, String> appendDefaultTags(final Map<String, String> tags) {
final var tagsAppender = new HashMap<>(tags);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;

import java.time.Duration;
import java.util.Map;

@Validated
Expand All @@ -12,5 +13,7 @@ public interface MetricSender {
void increment(@NotBlank final String metricName, @NotNull final Map<String, String> tags);
void increment(@NotBlank final String metricName, @NotNull final Map<String, String> tags, final double incrementValue);
void gauge(@NotBlank final String metricName, @NotNull final Map<String, String> tags, @NotNull final Number value);
void summary(@NotBlank final String metricName, @NotNull final Map<String, String> tags, @NotNull final Number value);
void timer(@NotBlank final String metricName, @NotNull final Map<String, String> tags, @NotNull final Duration value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -13,4 +16,16 @@ public ModelResolver modelResolver(final ObjectMapper objectMapper) {
return new ModelResolver(objectMapper);
}

@Bean
public OpenAPI openAPI(
@Value("${spring.application.name}") final String applicationName,
@Value("${spring.application.version}") final String version
) {
final var info = new Info()
.title(applicationName)
.version(version);

return new OpenAPI().info(info);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.multidatasources.exception;

public class BusinessException extends RuntimeException{

private static final boolean ENABLE_SUPPRESSION = true;
private static final boolean WRITEABLE_STACK_TRACE = false;

public BusinessException(final String message) {
super(message);
}

public BusinessException(final String message, final Throwable cause) {
super(message, cause, ENABLE_SUPPRESSION, WRITEABLE_STACK_TRACE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package br.com.multidatasources.exception;

public class JsonProcessingException extends BusinessException{

public JsonProcessingException(final String message, final Throwable cause){
super(message, cause);
}

}
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
3 changes: 2 additions & 1 deletion src/main/java/br/com/multidatasources/util/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.multidatasources.util;

import br.com.multidatasources.config.objectmapper.ObjectMapperConfig;
import br.com.multidatasources.exception.JsonProcessingException;

import java.util.concurrent.Callable;

Expand All @@ -20,7 +21,7 @@ private static <T> T execute(final Callable<T> callable) {
try {
return callable.call();
} catch (final Exception ex) {
throw new RuntimeException("An unexpected error occurred");
throw new JsonProcessingException("An unexpected error occurred", ex);
}
}

Expand Down
Loading

0 comments on commit 45ccea0

Please sign in to comment.