Init Check In with modules of Swagger, Entity, Database and Configuration
This commit is contained in:
26
configuration/pom.xml
Normal file
26
configuration/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>sydney.cheng</groupId>
|
||||||
|
<artifactId>ec-microservice-commons</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>ec-microservice-commons-configuration</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import sydney.cheng.microservice.commons.configuration.properties.auth.SecurityProperties;
|
||||||
|
import sydney.cheng.microservice.commons.configuration.properties.database.DatabaseProperties;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ConfigurationPropertiesScan(basePackages = {"sydney.cheng.**.properties"})
|
||||||
|
@ConfigurationProperties("app")
|
||||||
|
@Configuration
|
||||||
|
public class AppConfiguration {
|
||||||
|
private ApplicationConfiguration application;
|
||||||
|
private SecurityProperties security;
|
||||||
|
private DatabaseProperties database;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public static class ApplicationConfiguration {
|
||||||
|
private String deploymentTarget;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.auth;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ConfigurationProperties(prefix = "app.security.cors")
|
||||||
|
public class CorsProperties {
|
||||||
|
private String allowedUrls;
|
||||||
|
private String allowedOrigins;
|
||||||
|
private List<String> allowedHeaders;
|
||||||
|
private List<String> allowedMethods;
|
||||||
|
private long allowedMaxAge = 3600;
|
||||||
|
private boolean allowCredentials = true;
|
||||||
|
|
||||||
|
public List<String> getAllowedUrlList() {
|
||||||
|
return Arrays.asList(allowedUrls.split(","));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getAllowedOriginList() {
|
||||||
|
return Arrays.asList(allowedOrigins.split(","));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.auth;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ConfigurationProperties(prefix = "app.security.oauth2")
|
||||||
|
public class OAuth2Properties {
|
||||||
|
private RemoteTokenCheckProperties remoteTokenCheck;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class RemoteTokenCheckProperties {
|
||||||
|
String checkTokenUrl;
|
||||||
|
String clientId;
|
||||||
|
String clientSecret;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.auth;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties("app.security")
|
||||||
|
public class SecurityProperties {
|
||||||
|
private UrlProperties url;
|
||||||
|
private CorsProperties cors;
|
||||||
|
private OAuth2Properties oauth2;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.auth;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "app.security.url")
|
||||||
|
public class UrlProperties {
|
||||||
|
private String frontend;
|
||||||
|
private String backend;
|
||||||
|
private String gateway;
|
||||||
|
private String frontendDefaultLogin;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Profile("database")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class DataSourceProperties extends org.springframework.boot.autoconfigure.jdbc.DataSourceProperties implements Serializable {
|
||||||
|
private HikariDataSourceProperties hikari;
|
||||||
|
|
||||||
|
public DataSourceProperties() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Profile("database")
|
||||||
|
@Data
|
||||||
|
public class DatabaseNodeProperties implements Serializable {
|
||||||
|
DataSourceProperties datasource;
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Profile("database")
|
||||||
|
@Data
|
||||||
|
@PropertySource("classpath:application.yml")
|
||||||
|
@ConfigurationProperties(prefix = "app.database")
|
||||||
|
public class DatabaseProperties {
|
||||||
|
EntityManagerProperties entityManager;
|
||||||
|
JpaProperties jpa;
|
||||||
|
DatabaseNodeProperties master;
|
||||||
|
DatabaseNodeProperties replica;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class EntityManagerProperties implements Serializable {
|
||||||
|
String packages;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Profile(value = {"database & hikari"})
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class HikariDataSourceProperties extends HikariConfig implements Serializable {
|
||||||
|
private String urlPrefix;
|
||||||
|
|
||||||
|
public HikariDataSourceProperties() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HikariDataSourceProperties(Properties properties) {
|
||||||
|
super(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HikariDataSourceProperties(String propertyFileName) {
|
||||||
|
super(propertyFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Profile(value = {"database & hikari"})
|
||||||
|
@ConfigurationProperties("app.database.primary.datasource.hikari")
|
||||||
|
public class PrimaryHikariDataSourceProperties extends HikariDataSourceProperties {
|
||||||
|
public PrimaryHikariDataSourceProperties() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrimaryHikariDataSourceProperties(Properties properties) {
|
||||||
|
super(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrimaryHikariDataSourceProperties(String propertyFileName) {
|
||||||
|
super(propertyFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package sydney.cheng.microservice.commons.configuration.properties.database;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Profile(value = {"database & hikari"})
|
||||||
|
@ConfigurationProperties("app.database.replica.datasource.hikari")
|
||||||
|
public class ReplicaHikariDataSourceProperties extends HikariDataSourceProperties {
|
||||||
|
public ReplicaHikariDataSourceProperties() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReplicaHikariDataSourceProperties(String propertyFileName) {
|
||||||
|
super(propertyFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReplicaHikariDataSourceProperties(Properties properties) {
|
||||||
|
super(properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
36
database/pom.xml
Normal file
36
database/pom.xml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>sydney.cheng</groupId>
|
||||||
|
<artifactId>ec-microservice-commons</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>ec-microservice-commons-database</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>sydney.cheng</groupId>
|
||||||
|
<artifactId>ec-microservice-commons-configuration</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
<version>${caffeine.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.annotation;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Profile("database")
|
||||||
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Transactional("ecTransactionManager")
|
||||||
|
public @interface DatabaseTransactional {
|
||||||
|
boolean readOnly() default false;
|
||||||
|
|
||||||
|
Propagation propagation() default Propagation.REQUIRED;
|
||||||
|
|
||||||
|
Class<? extends Throwable>[] noRollbackFor() default {};
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface ReadOnlyConnection {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.annotation;
|
||||||
|
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import sydney.cheng.microservice.commons.database.constant.DbType;
|
||||||
|
import sydney.cheng.microservice.commons.database.datasource.DbContextHolder;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@ToString
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Component
|
||||||
|
public class ReadOnlyConnectionInterceptor implements Ordered {
|
||||||
|
private int order;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Value("20")
|
||||||
|
public void setOrder(int order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* handle interceptor for any public method execution
|
||||||
|
*/
|
||||||
|
@Pointcut(value = "execution(public * *(..))")
|
||||||
|
public void anyPublicMethod() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around("@annotation(readOnlyConnection)")
|
||||||
|
public Object proceed(ProceedingJoinPoint pjp, ReadOnlyConnection readOnlyConnection) throws Throwable {
|
||||||
|
try {
|
||||||
|
DbContextHolder.setDbType(DbType.REPLICA);
|
||||||
|
Object result = pjp.proceed();
|
||||||
|
DbContextHolder.clearDbType();
|
||||||
|
return result;
|
||||||
|
} finally {
|
||||||
|
// restore state
|
||||||
|
DbContextHolder.clearDbType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.config;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@EnableCaching
|
||||||
|
@Configuration
|
||||||
|
public class CacheManagerConfiguration {
|
||||||
|
@Bean
|
||||||
|
public Caffeine<Object, Object> caffeineConfig() {
|
||||||
|
return Caffeine.newBuilder().expireAfterWrite(12, TimeUnit.HOURS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CacheManager cacheManager(Caffeine<Object, Object> caffeine) {
|
||||||
|
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
|
||||||
|
caffeineCacheManager.setCaffeine(caffeine);
|
||||||
|
return caffeineCacheManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.config;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import lombok.*;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import sydney.cheng.microservice.commons.configuration.properties.database.PrimaryHikariDataSourceProperties;
|
||||||
|
import sydney.cheng.microservice.commons.configuration.properties.database.ReplicaHikariDataSourceProperties;
|
||||||
|
import sydney.cheng.microservice.commons.database.constant.DbType;
|
||||||
|
import sydney.cheng.microservice.commons.database.datasource.RoutingDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static sydney.cheng.microservice.commons.database.constant.DatabaseBeanConstant.*;
|
||||||
|
|
||||||
|
@Profile(value = {"database & hikari"})
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
|
||||||
|
@Configuration
|
||||||
|
public class HikariDataSourceConfiguration {
|
||||||
|
private final PrimaryHikariDataSourceProperties primaryHikariDataSourceProperties;
|
||||||
|
private final ReplicaHikariDataSourceProperties replicaHikariDataSourceProperties;
|
||||||
|
|
||||||
|
@Bean(name = PRIMARY_DS_BEAN_NAME)
|
||||||
|
public DataSource primaryDataSource() {
|
||||||
|
if (this.primaryHikariDataSourceProperties == null) throw new AssertionError();
|
||||||
|
this.primaryHikariDataSourceProperties.setPoolName(PRIMARY_DS_BEAN_NAME);
|
||||||
|
return new HikariDataSource(this.primaryHikariDataSourceProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = REPLICA_DS_BEAN_NAME)
|
||||||
|
public DataSource replicaDataSource() {
|
||||||
|
if (this.replicaHikariDataSourceProperties == null) throw new AssertionError();
|
||||||
|
this.replicaHikariDataSourceProperties.setPoolName(REPLICA_DS_BEAN_NAME);
|
||||||
|
return new HikariDataSource(this.replicaHikariDataSourceProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure data source routing for MRCENTRAL.
|
||||||
|
*
|
||||||
|
* @return data source
|
||||||
|
* @see RoutingDataSource
|
||||||
|
*/
|
||||||
|
@Bean(name = {"dataSource", DS_BEAN_NAME})
|
||||||
|
public DataSource centralDataSource() {
|
||||||
|
RoutingDataSource rds = new RoutingDataSource();
|
||||||
|
rds.setTargetDataSources(Map
|
||||||
|
.of(DbType.PRIMARY, this.primaryDataSource(), DbType.REPLICA, this.replicaDataSource()));
|
||||||
|
rds.setDefaultTargetDataSource(this.primaryDataSource());
|
||||||
|
return rds;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.config;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.orm.hibernate5.SpringBeanContainer;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import sydney.cheng.microservice.commons.configuration.properties.database.DatabaseProperties;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static sydney.cheng.microservice.commons.database.constant.DatabaseBeanConstant.*;
|
||||||
|
|
||||||
|
@Profile(value = {"database & hikari"})
|
||||||
|
@Configuration
|
||||||
|
@EnableJpaRepositories(
|
||||||
|
basePackages = "sydney.cheng.**.repository",
|
||||||
|
entityManagerFactoryRef = "entityManagerFactory",
|
||||||
|
transactionManagerRef = "transactionManager"
|
||||||
|
)
|
||||||
|
public class JPAPersistenceConfiguration {
|
||||||
|
private final ConfigurableListableBeanFactory beanFactory;
|
||||||
|
|
||||||
|
private final DatabaseProperties databaseProperties;
|
||||||
|
|
||||||
|
public JPAPersistenceConfiguration(
|
||||||
|
ConfigurableListableBeanFactory beanFactory,
|
||||||
|
DatabaseProperties databaseProperties
|
||||||
|
) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
this.databaseProperties = databaseProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier(DS_BEAN_NAME) DataSource dataSource) {
|
||||||
|
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
|
||||||
|
em.setPersistenceUnitName("persistence-unit");
|
||||||
|
em.setPackagesToScan(this.databaseProperties.getEntityManager().getPackages());
|
||||||
|
em.setDataSource(dataSource);
|
||||||
|
|
||||||
|
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
|
||||||
|
Map<String, Object> properties = new HashMap<>(this.databaseProperties.getJpa().getProperties());
|
||||||
|
properties.put(AvailableSettings.PHYSICAL_NAMING_STRATEGY, "org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy");
|
||||||
|
properties.put(AvailableSettings.IMPLICIT_NAMING_STRATEGY, "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
|
||||||
|
properties.put(AvailableSettings.BEAN_CONTAINER, new SpringBeanContainer(this.beanFactory));
|
||||||
|
em.setJpaPropertyMap(properties);
|
||||||
|
|
||||||
|
return em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JpaTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
|
||||||
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(emf);
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.constant;
|
||||||
|
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
|
||||||
|
public class DatabaseBeanConstant {
|
||||||
|
public static final String DS_BEAN_NAME = "databaseDataSource";
|
||||||
|
public static final String PRIMARY_DS_BEAN_NAME = "databaseDataSourcePrimary";
|
||||||
|
public static final String REPLICA_DS_BEAN_NAME = "databaseDataSourceReplica";
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.constant;
|
||||||
|
|
||||||
|
public enum DbType {
|
||||||
|
PRIMARY, REPLICA
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.datasource;
|
||||||
|
|
||||||
|
import sydney.cheng.microservice.commons.database.constant.DbType;
|
||||||
|
|
||||||
|
public class DbContextHolder {
|
||||||
|
|
||||||
|
private static final ThreadLocal<DbType> contextHolder = new ThreadLocal<>();
|
||||||
|
|
||||||
|
private DbContextHolder() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DbType getDbType() {
|
||||||
|
return contextHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setDbType(DbType dbType) {
|
||||||
|
if (dbType == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
contextHolder.set(dbType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearDbType() {
|
||||||
|
contextHolder.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package sydney.cheng.microservice.commons.database.datasource;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
public class RoutingDataSource extends AbstractRoutingDataSource {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object determineCurrentLookupKey() {
|
||||||
|
return DbContextHolder.getDbType();
|
||||||
|
}
|
||||||
|
}
|
||||||
26
entity/pom.xml
Normal file
26
entity/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>sydney.cheng</groupId>
|
||||||
|
<artifactId>ec-microservice-commons</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>ec-microservice-commons-entity</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
31
entity/src/main/java/sydney/cheng/entity/AbstractUser.java
Normal file
31
entity/src/main/java/sydney/cheng/entity/AbstractUser.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package sydney.cheng.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.hibernate.annotations.UuidGenerator;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public abstract class AbstractUser implements Serializable {
|
||||||
|
@Id
|
||||||
|
@UuidGenerator(style = UuidGenerator.Style.TIME)
|
||||||
|
@Column(name = "id")
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@Column(name = "username", nullable = false)
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Column(name = "email", nullable = false, unique = true)
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Column(name = "password", nullable = false)
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
30
pom.xml
30
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>sydney.cheng</groupId>
|
<groupId>sydney.cheng</groupId>
|
||||||
<artifactId>ec-super-pom</artifactId>
|
<artifactId>ec-super-pom</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.4</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>ec-microservice-commons</artifactId>
|
<artifactId>ec-microservice-commons</artifactId>
|
||||||
@@ -39,10 +39,21 @@
|
|||||||
</issueManagement>
|
</issueManagement>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
|
<module>database</module>
|
||||||
<module>swagger</module>
|
<module>swagger</module>
|
||||||
|
<module>configuration</module>
|
||||||
|
<module>entity</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<!-- Dependency Versions -->
|
||||||
|
<spring-context-support.version>6.1.12</spring-context-support.version>
|
||||||
|
<springdoc-openapi-starter-common.version>2.6.0</springdoc-openapi-starter-common.version>
|
||||||
|
<atomikos.version>6.0.0</atomikos.version>
|
||||||
|
<caffeine.version>3.1.8</caffeine.version>
|
||||||
<!-- Sonar Properties -->
|
<!-- Sonar Properties -->
|
||||||
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
|
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
|
||||||
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
|
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
|
||||||
@@ -52,4 +63,21 @@
|
|||||||
<sonar.organization>3dwardch3ng</sonar.organization>
|
<sonar.organization>3dwardch3ng</sonar.organization>
|
||||||
<sonar.host.url>https://sonarqube.cluster.edward.sydney</sonar.host.url>
|
<sonar.host.url>https://sonarqube.cluster.edward.sydney</sonar.host.url>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -18,8 +18,6 @@
|
|||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<!-- Dependency Versions -->
|
|
||||||
<springdoc-openapi-starter-common.version>2.6.0</springdoc-openapi-starter-common.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
Reference in New Issue
Block a user