From 8bc53deddd7bf25d36c81de60310cefb7a89b71f Mon Sep 17 00:00:00 2001 From: limju Date: Mon, 30 Oct 2023 09:32:00 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20db=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/support/DatasourceConfig.java | 7 ++-- .../support/SecondaryMybatisConfig.java | 35 ++++--------------- .../config/support/TransactionConfig.java | 13 +++++-- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java index 290867b..5ecae09 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java @@ -2,15 +2,12 @@ package kr.xit.core.spring.config.support; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; - -import javax.sql.DataSource; //FIXME :: 재설정이 필요한 경우 해당 프로젝트에 동일한 파일로 재정의 하여 사용 /** @@ -36,7 +33,6 @@ import javax.sql.DataSource; * @see PrimaryMybatisConfig */ //@ConditionalOnProperty(value = "spring", havingValue = "datasource", matchIfMissing = false) -@Slf4j @Configuration public class DatasourceConfig { @Bean(name = "primaryHikariConfig") @@ -56,6 +52,7 @@ public class DatasourceConfig { return new HikariDataSource(primaryHikariConfig()); } + //@ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) @Bean(name = "secondaryHikariConfig") @ConfigurationProperties(prefix = "spring.datasource.hikari.oracle") public HikariConfig secondaryHikariConfig() { diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java index 808e8f0..18022fb 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java @@ -1,55 +1,31 @@ package kr.xit.core.spring.config.support; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.support.lob.DefaultLobHandler; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.sql.DataSource; //FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 -/** - *
- * description : Mybatis 설정 - FIXME:: @DependsOn(value = {"dataSource"}) loading
- *               - 조건 : @DependsOn(value = {"dataSource"})
- * packageName : kr.xit.core.spring.config.support
- * fileName    : MybatisConfig
- * author      : julim
- * date        : 2023-04-28
- * ======================================================================
- * 변경일         변경자        변경 내용
- * ----------------------------------------------------------------------
- * 2023-04-28    julim       최초 생성
- *
- * 
- * @see DatasourceConfig - */ -@Slf4j + + @Configuration @MapperScan( basePackages = {"kr.xit.biz.sms.mapper"}, sqlSessionFactoryRef = Constants.SECONDARY_SQL_SESSION ) public class SecondaryMybatisConfig { - static final String MYBATIS_CONFIG_FILE = "classpath:/egovframework/mapper/mapper-config.xml"; - @Bean(name = Constants.SECONDARY_SQL_SESSION) + @ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) public SqlSessionFactory secondarySqlSession(@Qualifier(Constants.SECONDARY_DATA_SOURCE) DataSource dataSource) throws Exception { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); @@ -59,6 +35,7 @@ public class SecondaryMybatisConfig { return sessionFactory.getObject(); } + @ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier(Constants.SECONDARY_SQL_SESSION) SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java index 9ab3c26..2cb8e99 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java @@ -1,5 +1,6 @@ package kr.xit.core.spring.config.support; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; @@ -11,8 +12,6 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; -import javax.sql.DataSource; - //FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 /** @@ -35,6 +34,15 @@ import javax.sql.DataSource; @Configuration @EnableTransactionManagement public class TransactionConfig { +// @ConditionalOnMissingBean(SecondaryMybatisConfig.class) +// @Primary +// @Bean +// public PlatformTransactionManager primaryTransactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource dataSource) { +// DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource); +// dstm.setGlobalRollbackOnParticipationFailure(false); +// return dstm; +// } + //////////////////////////////////////////////////////////////////////////////////////////// // ChainedTransactionManager : trsnsaction binding /////////////////////////////////////////////////////////////////////////////////////////// @@ -45,6 +53,7 @@ public class TransactionConfig { * @return PlatformTransactionManager * @throws Exception Exception */ + //@ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) @Primary @Bean public PlatformTransactionManager transactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource mariaDS, From 8b3821f6a38352e856e4a30a107aeaa3024c2859 Mon Sep 17 00:00:00 2001 From: limju Date: Mon, 30 Oct 2023 14:38:33 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20db=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/support/DatasourceConfig.java | 2 +- .../resources/config/application-local.yml | 2 +- .../java/kr/xit/biz/ens/model/EnsDTO.java | 4 +- .../biz/sms/service/ISmsMessageService.java | 2 +- .../biz/sms/service/SmsMessageService.java | 11 ++- .../xit/biz/sms/web/SmsMessageController.java | 55 +++++++++++++++ .../PrimaryDatabaseAutoConfiguration.java | 36 ++++++++++ .../SecondaryDatabaseAutoConfiguration.java | 38 ++++++++++ .../config/db/DataSourceProperties.java | 32 +++++++++ .../spring/config/db/DatasourceConfig.java | 69 +++++++++++++++++++ .../db/PrimaryDataSourceProperties.java | 30 ++++++++ .../PrimaryDatasourceConfig.java} | 28 +++----- .../{support => db}/PrimaryMybatisConfig.java | 18 ++--- .../db/SecondaryDataSourceProperties.java | 27 ++++++++ .../config/db/SecondaryDatasourceConfig.java | 53 ++++++++++++++ .../SecondaryMybatisConfig.java | 25 +++++-- .../{support => db}/TransactionConfig.java | 14 +++- .../main/resources/META-INF/spring.factories | 3 + .../resources/config/application-local.yml | 4 +- .../mapper/biz/ens-mysql-mapper.xml | 21 +++++- .../xit/core/init/custom/AppInitHelper.java | 20 +++--- 21 files changed, 434 insertions(+), 60 deletions(-) create mode 100644 mens-batch/src/main/java/kr/xit/biz/sms/web/SmsMessageController.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/auto/PrimaryDatabaseAutoConfiguration.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/auto/SecondaryDatabaseAutoConfiguration.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/config/db/DataSourceProperties.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/config/db/DatasourceConfig.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDataSourceProperties.java rename mens-batch/src/main/java/kr/xit/core/spring/config/{support/DatasourceConfig.java => db/PrimaryDatasourceConfig.java} (75%) rename mens-batch/src/main/java/kr/xit/core/spring/config/{support => db}/PrimaryMybatisConfig.java (85%) create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDataSourceProperties.java create mode 100644 mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java rename mens-batch/src/main/java/kr/xit/core/spring/config/{support => db}/SecondaryMybatisConfig.java (66%) rename mens-batch/src/main/java/kr/xit/core/spring/config/{support => db}/TransactionConfig.java (84%) create mode 100644 mens-batch/src/main/resources/META-INF/spring.factories diff --git a/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java b/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java index ebfd8c8..ceb36d7 100644 --- a/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java +++ b/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java @@ -41,7 +41,7 @@ import javax.sql.DataSource; public class DatasourceConfig { @Bean(name = "primaryHikariConfig") @Primary - @ConfigurationProperties(prefix = "spring.datasource.hikari.maria") + @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") public HikariConfig primaryHikariConfig() { // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); // hikariConfig.setAutoCommit(false); diff --git a/mens-api/src/main/resources/config/application-local.yml b/mens-api/src/main/resources/config/application-local.yml index 6a7e069..ba79dd2 100644 --- a/mens-api/src/main/resources/config/application-local.yml +++ b/mens-api/src/main/resources/config/application-local.yml @@ -5,7 +5,7 @@ spring: datasource: hikari: # 9 server - maria: + primary: driver-class-name: org.mariadb.jdbc.Driver jdbc-url: jdbc:mariadb://211.119.124.9:4407/mens?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true username: root diff --git a/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java b/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java index 1c0ff89..2a22b14 100644 --- a/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java +++ b/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java @@ -1,6 +1,7 @@ package kr.xit.biz.ens.model; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import io.swagger.v3.oas.annotations.media.Schema; @@ -841,7 +842,8 @@ public class EnsDTO { * 등록 일시 */ @JsonDeserialize(using = LocalDateDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd kk:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd kk:mm:ss.SSS") + @JsonIgnore private LocalDateTime registDt; /** * 등록자 diff --git a/mens-batch/src/main/java/kr/xit/biz/sms/service/ISmsMessageService.java b/mens-batch/src/main/java/kr/xit/biz/sms/service/ISmsMessageService.java index 0505bb4..4989bf4 100644 --- a/mens-batch/src/main/java/kr/xit/biz/sms/service/ISmsMessageService.java +++ b/mens-batch/src/main/java/kr/xit/biz/sms/service/ISmsMessageService.java @@ -20,5 +20,5 @@ import java.util.List; * */ public interface ISmsMessageService { - void sendSmsList(List list); + void sendSmsList(final List list); } diff --git a/mens-batch/src/main/java/kr/xit/biz/sms/service/SmsMessageService.java b/mens-batch/src/main/java/kr/xit/biz/sms/service/SmsMessageService.java index b61c5bc..9e03c93 100644 --- a/mens-batch/src/main/java/kr/xit/biz/sms/service/SmsMessageService.java +++ b/mens-batch/src/main/java/kr/xit/biz/sms/service/SmsMessageService.java @@ -1,14 +1,13 @@ package kr.xit.biz.sms.service; +import java.util.List; +import kr.xit.biz.ens.model.EnsDTO; import kr.xit.biz.sms.mapper.ISmsMessageMapper; import lombok.RequiredArgsConstructor; import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import java.util.List; - /** *
  * description : xit SMS 발송 서비스 - Oracle 11
@@ -35,8 +34,8 @@ public class SmsMessageService extends EgovAbstractServiceImpl implements ISmsMe
      * @param list List SMS 발송 목록
      */
     @Override
-    @Transactional(propagation = Propagation.REQUIRES_NEW)
-    public  void sendSmsList(List list) {
-        for(T t : list) mapper.insertScTran(t);
+    @Transactional//(propagation = Propagation.REQUIRES_NEW)
+    public void sendSmsList(List list) {
+        for(EnsDTO.SmsSndng dto : list) mapper.insertScTran(dto);
     }
 }
diff --git a/mens-batch/src/main/java/kr/xit/biz/sms/web/SmsMessageController.java b/mens-batch/src/main/java/kr/xit/biz/sms/web/SmsMessageController.java
new file mode 100644
index 0000000..c2d2160
--- /dev/null
+++ b/mens-batch/src/main/java/kr/xit/biz/sms/web/SmsMessageController.java
@@ -0,0 +1,55 @@
+package kr.xit.biz.sms.web;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import java.util.List;
+import kr.xit.biz.ens.model.EnsDTO.SmsSndng;
+import kr.xit.biz.ens.model.EnsDTO.SndngMssageParam;
+import kr.xit.biz.ens.service.EnsBatchRequireNewService;
+import kr.xit.biz.sms.service.ISmsMessageService;
+import kr.xit.core.model.ApiResponseDTO;
+import kr.xit.core.model.IApiResponse;
+import lombok.RequiredArgsConstructor;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 
+ * description :
+ *
+ * packageName : kr.xit.biz.sms.web
+ * fileName    : SmsMessageController
+ * author      : limju
+ * date        : 2023-10-30
+ * ======================================================================
+ * 변경일         변경자        변경 내용
+ * ----------------------------------------------------------------------
+ * 2023-10-30    limju       최초 생성
+ *
+ * 
+ */ +@Tag(name = " SmsMessageController", description = "SMS(xit) 연계 서비스") +@RequiredArgsConstructor +@RestController +@RequestMapping("/batch/ens/v1") +public class SmsMessageController { + private final ISmsMessageService service; + private final EnsBatchRequireNewService requireNewService; + + @Operation(summary = "SMS 메세지 연계", description = "SMS 메세지 연계") + @PostMapping(value = "/sendMessages", produces = MediaType.APPLICATION_JSON_VALUE) + public IApiResponse sendMessages(@RequestBody final List lists) { + service.sendSmsList(lists); + return ApiResponseDTO.success(); + } + + @Operation(summary = "SMS 메세지 연계", description = "SMS 메세지 연계") + @PostMapping(value = "/sendMessages2", produces = MediaType.APPLICATION_JSON_VALUE) + public IApiResponse sendMessages2(@RequestBody final SndngMssageParam reqDTO) { + requireNewService.sendSms(reqDTO); + return ApiResponseDTO.success(); + } +} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/auto/PrimaryDatabaseAutoConfiguration.java b/mens-batch/src/main/java/kr/xit/core/spring/auto/PrimaryDatabaseAutoConfiguration.java new file mode 100644 index 0000000..ffc8262 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/auto/PrimaryDatabaseAutoConfiguration.java @@ -0,0 +1,36 @@ +//package kr.xit.core.spring.auto; +// +//import kr.xit.core.spring.config.db.PrimaryDatasourceConfig; +//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +//import org.springframework.context.annotation.Bean; +// +///** +// *
+// * description :
+// *
+// * packageName : kr.xit.core.spring.config.auto
+// * fileName    : SecondaryDatabaseAutoConfiguration
+// * author      : limju
+// * date        : 2023-10-30
+// * ======================================================================
+// * 변경일         변경자        변경 내용
+// * ----------------------------------------------------------------------
+// * 2023-10-30    limju       최초 생성
+// *
+// * 
+// */ +////@Configuration +////@ConditionalOnClass(PrimaryDatasourceConfig.class) +//@ConditionalOnProperty("spring.datasource.hikari.primary") +//public class PrimaryDatabaseAutoConfiguration { +// @Bean +// public PrimaryDatasourceConfig primaryDatasourceConfig() { +// return new PrimaryDatasourceConfig(); +// } +// +// //@ConditionalOnMissingBean(SecondaryMybatisConfig.class) +//// @Bean +//// public TransactionConfig transactionConfig() { +//// return new TransactionConfig(); +//// } +//} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/auto/SecondaryDatabaseAutoConfiguration.java b/mens-batch/src/main/java/kr/xit/core/spring/auto/SecondaryDatabaseAutoConfiguration.java new file mode 100644 index 0000000..3350ed5 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/auto/SecondaryDatabaseAutoConfiguration.java @@ -0,0 +1,38 @@ +//package kr.xit.core.spring.auto; +// +//import kr.xit.core.spring.config.db.SecondaryMybatisConfig; +//import kr.xit.core.spring.config.db.TransactionConfig; +//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +//import org.springframework.context.annotation.Bean; +// +///** +// *
+// * description :
+// *
+// * packageName : kr.xit.core.spring.config.auto
+// * fileName    : SecondaryDatabaseAutoConfiguration
+// * author      : limju
+// * date        : 2023-10-30
+// * ======================================================================
+// * 변경일         변경자        변경 내용
+// * ----------------------------------------------------------------------
+// * 2023-10-30    limju       최초 생성
+// *
+// * 
+// */ +////@Configuration +//@ConditionalOnClass(SecondaryMybatisConfig.class) +//@ConditionalOnProperty("spring.datasource.hikari.secondary") +//public class SecondaryDatabaseAutoConfiguration { +// @Bean +// public SecondaryMybatisConfig secondaryMybatisConfig() { +// return new SecondaryMybatisConfig(); +// } +// +// //@ConditionalOnMissingBean(SecondaryMybatisConfig.class) +// @Bean +// public TransactionConfig transactionConfig() { +// return new TransactionConfig(); +// } +//} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/DataSourceProperties.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/DataSourceProperties.java new file mode 100644 index 0000000..9024052 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/DataSourceProperties.java @@ -0,0 +1,32 @@ +package kr.xit.core.spring.config.db; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +/** + *
+ * description :
+ *
+ * packageName : kr.xit.core.spring.config.db
+ * fileName    : DataSourceProperties
+ * author      : limju
+ * date        : 2023-10-30
+ * ======================================================================
+ * 변경일         변경자        변경 내용
+ * ----------------------------------------------------------------------
+ * 2023-10-30    limju       최초 생성
+ *
+ * 
+ */ +@Setter +@Getter +@AllArgsConstructor +public class DataSourceProperties { + private String driverClassName; + private String jdbcUrl; + private String username; + private String password; + private String readonly; + +} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/DatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/DatasourceConfig.java new file mode 100644 index 0000000..a0efcba --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/DatasourceConfig.java @@ -0,0 +1,69 @@ +//package kr.xit.core.spring.config.db; +// +//import com.zaxxer.hikari.HikariConfig; +//import com.zaxxer.hikari.HikariDataSource; +//import javax.sql.DataSource; +//import kr.xit.core.consts.Constants; +//import org.springframework.boot.context.properties.ConfigurationProperties; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Primary; +// +////FIXME :: 재설정이 필요한 경우 해당 프로젝트에 동일한 파일로 재정의 하여 사용 +///** +// *
+// * description : Datasource 설정 - FIXME:: spring.datasource 설정이 있는 경우만 loading
+// *               - 조건 : spring.datasource
+// *               실제 필요한 경우만 커넥션을 점유하도록
+// *               LazyConnectionDataSourceProxy 사용
+// *               일반 Datasource 사용시
+// *               - Spring은 트랜잭션에 진입시 데이타 소스의 커넥션을 get
+// *               - ehcache, hibernate 영속성 컨택슽트 1차캐시 등에도 커넥션을 get
+// *               - multi-datasource 에서 트랜잭션 진입 이후 datasource 분기 불가
+// * packageName : kr.xit.core.spring.config.support
+// * fileName    : DatasourceConfig
+// * author      : julim
+// * date        : 2023-04-28
+// * ======================================================================
+// * 변경일         변경자        변경 내용
+// * ----------------------------------------------------------------------
+// * 2023-04-28    julim       최초 생성
+// *
+// * 
+// * @see PrimaryMybatisConfig +// */ +////@ConditionalOnProperty(value = "spring", havingValue = "datasource", matchIfMissing = false) +////@Configuration +//public class DatasourceConfig { +// @Bean(name = "primaryHikariConfig") +// @Primary +// @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") +// public HikariConfig primaryHikariConfig() { +// // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); +// // hikariConfig.setAutoCommit(false); +// // return hikariConfig; +// return new HikariConfig(); +// } +// +// @Bean(Constants.PRIMARY_DATA_SOURCE) +// @Primary +// public DataSource primaryDataSource() throws Exception{ +// //return new LazyConnectionDataSourceProxy(new HikariDataSource(primaryHikariConfig())); +// return new HikariDataSource(primaryHikariConfig()); +// } +// +// //@ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) +// @Bean(name = "secondaryHikariConfig") +// @ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") +// public HikariConfig secondaryHikariConfig() { +// // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); +// // hikariConfig.setAutoCommit(false); +// // return hikariConfig; +// return new HikariConfig(); +// } +// +// @Bean(name = Constants.SECONDARY_DATA_SOURCE) +// public DataSource secondaryDataSource() throws Exception{ +// //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); +// return new HikariDataSource(secondaryHikariConfig()); +// } +//} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDataSourceProperties.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDataSourceProperties.java new file mode 100644 index 0000000..4ff7bd4 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDataSourceProperties.java @@ -0,0 +1,30 @@ +//package kr.xit.core.spring.config.db; +// +//import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +//import org.springframework.boot.context.properties.ConfigurationProperties; +// +///** +// *
+// * description :
+// *
+// * packageName : kr.xit.core.spring.config.db
+// * fileName    : DataSourceProperties
+// * author      : limju
+// * date        : 2023-10-30
+// * ======================================================================
+// * 변경일         변경자        변경 내용
+// * ----------------------------------------------------------------------
+// * 2023-10-30    limju       최초 생성
+// *
+// * 
+// */ +//@ConfigurationProperties(prefix = "spring.datasource.hikari.primary") +////@EnableAutoConfiguration(exclude = SecondaryDataSourceProperties.class) +//public class PrimaryDataSourceProperties extends DataSourceProperties{ +// private String driverClassName; +// private String jdbcUrl; +// private String username; +// private String password; +// private String readonly; +// +//} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java similarity index 75% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java index 5ecae09..88937b3 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java @@ -1,15 +1,17 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import javax.sql.DataSource; import kr.xit.core.consts.Constants; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; //FIXME :: 재설정이 필요한 경우 해당 프로젝트에 동일한 파일로 재정의 하여 사용 + /** *
  * description : Datasource 설정 - FIXME:: spring.datasource 설정이 있는 경우만 loading
@@ -32,12 +34,13 @@ import org.springframework.context.annotation.Primary;
  * 
* @see PrimaryMybatisConfig */ -//@ConditionalOnProperty(value = "spring", havingValue = "datasource", matchIfMissing = false) @Configuration -public class DatasourceConfig { +@ConditionalOnProperty(value = "spring.datasource.hikari.primary.username") +//@ConditionalOnProperty(value = "spring.datasource.hikari.primary") +public class PrimaryDatasourceConfig { @Bean(name = "primaryHikariConfig") @Primary - @ConfigurationProperties(prefix = "spring.datasource.hikari.maria") + @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") public HikariConfig primaryHikariConfig() { // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); // hikariConfig.setAutoCommit(false); @@ -48,23 +51,10 @@ public class DatasourceConfig { @Bean(Constants.PRIMARY_DATA_SOURCE) @Primary public DataSource primaryDataSource() throws Exception{ - //return new LazyConnectionDataSourceProxy(new HikariDataSource(primaryHikariConfig())); - return new HikariDataSource(primaryHikariConfig()); - } - - //@ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) - @Bean(name = "secondaryHikariConfig") - @ConfigurationProperties(prefix = "spring.datasource.hikari.oracle") - public HikariConfig secondaryHikariConfig() { // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); // hikariConfig.setAutoCommit(false); // return hikariConfig; - return new HikariConfig(); - } - - @Bean(name = Constants.SECONDARY_DATA_SOURCE) - public DataSource secondaryDataSource() throws Exception{ - //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); - return new HikariDataSource(secondaryHikariConfig()); + //return new LazyConnectionDataSourceProxy(new HikariDataSource(primaryHikariConfig())); + return new HikariDataSource(primaryHikariConfig()); } } diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java similarity index 85% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java index d2b6133..21c2d72 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java @@ -1,21 +1,20 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.support.lob.DefaultLobHandler; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.sql.DataSource; //FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 /** @@ -34,8 +33,9 @@ import javax.sql.DataSource; *
* @see DatasourceConfig */ -@Slf4j @Configuration +//@ConditionalOnProperty("spring.datasource.hikari.primary.username") +@ConditionalOnClass(PrimaryDatasourceConfig.class) @MapperScan( basePackages = { "kr.xit.core.**.mapper", diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDataSourceProperties.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDataSourceProperties.java new file mode 100644 index 0000000..014bb28 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDataSourceProperties.java @@ -0,0 +1,27 @@ +package kr.xit.core.spring.config.db; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + *
+ * description :
+ *
+ * packageName : kr.xit.core.spring.config.db
+ * fileName    : DataSourceProperties
+ * author      : limju
+ * date        : 2023-10-30
+ * ======================================================================
+ * 변경일         변경자        변경 내용
+ * ----------------------------------------------------------------------
+ * 2023-10-30    limju       최초 생성
+ *
+ * 
+ */ +@ConfigurationProperties(prefix = "spring.datasource.hikari") +@Getter +@Setter +public class SecondaryDataSourceProperties { + private String secondary; +} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java new file mode 100644 index 0000000..cdf6f02 --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java @@ -0,0 +1,53 @@ +package kr.xit.core.spring.config.db; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import kr.xit.core.consts.Constants; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +//FIXME :: 재설정이 필요한 경우 해당 프로젝트에 동일한 파일로 재정의 하여 사용 + +/** + *
+ * description : Datasource 설정 - FIXME:: spring.datasource 설정이 있는 경우만 loading
+ *               - 조건 : spring.datasource
+ *               실제 필요한 경우만 커넥션을 점유하도록
+ *               LazyConnectionDataSourceProxy 사용
+ *               일반 Datasource 사용시
+ *               - Spring은 트랜잭션에 진입시 데이타 소스의 커넥션을 get
+ *               - ehcache, hibernate 영속성 컨택슽트 1차캐시 등에도 커넥션을 get
+ *               - multi-datasource 에서 트랜잭션 진입 이후 datasource 분기 불가
+ * packageName : kr.xit.core.spring.config.support
+ * fileName    : DatasourceConfig
+ * author      : julim
+ * date        : 2023-04-28
+ * ======================================================================
+ * 변경일         변경자        변경 내용
+ * ----------------------------------------------------------------------
+ * 2023-04-28    julim       최초 생성
+ *
+ * 
+ * @see PrimaryMybatisConfig + */ +@Configuration +@ConditionalOnProperty(value = "spring.datasource.hikari.secondary.username") +public class SecondaryDatasourceConfig { + @Bean(name = "secondaryHikariConfig") + @ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") + public HikariConfig secondaryHikariConfig() { + // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); + // hikariConfig.setAutoCommit(false); + // return hikariConfig; + return new HikariConfig(); + } + + @Bean(name = Constants.SECONDARY_DATA_SOURCE) + public DataSource secondaryDataSource() throws Exception{ + //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); + return new HikariDataSource(secondaryHikariConfig()); + } +} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java similarity index 66% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java index 18022fb..5dd4ea1 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java @@ -1,5 +1,7 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; import javax.sql.DataSource; import kr.xit.core.consts.Constants; import org.apache.ibatis.session.SqlSessionFactory; @@ -7,25 +9,36 @@ import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; //FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 -@Configuration +//@Configuration @MapperScan( basePackages = {"kr.xit.biz.sms.mapper"}, sqlSessionFactoryRef = Constants.SECONDARY_SQL_SESSION ) public class SecondaryMybatisConfig { static final String MYBATIS_CONFIG_FILE = "classpath:/egovframework/mapper/mapper-config.xml"; + @Bean(name = "secondaryHikariConfig") + public HikariConfig secondaryHikariConfig() { + // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); + // hikariConfig.setAutoCommit(false); + // return hikariConfig; + return new HikariConfig(); + } + + @Bean(name = Constants.SECONDARY_DATA_SOURCE) + public DataSource secondaryDataSource() throws Exception{ + //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); + return new HikariDataSource(secondaryHikariConfig()); + } - @ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) + //@ConditionalOnProperty(value = "spring.datasource.hikari.secondary") public SqlSessionFactory secondarySqlSession(@Qualifier(Constants.SECONDARY_DATA_SOURCE) DataSource dataSource) throws Exception { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); @@ -35,7 +48,7 @@ public class SecondaryMybatisConfig { return sessionFactory.getObject(); } - @ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) + //@ConditionalOnProperty(value = "spring.datasource.hikari.secondary") @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier(Constants.SECONDARY_SQL_SESSION) SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java similarity index 84% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java index 2cb8e99..c05c3c2 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java @@ -1,9 +1,9 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -30,7 +30,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; * * @see DatasourceConfig */ -@Slf4j @Configuration @EnableTransactionManagement public class TransactionConfig { @@ -41,6 +40,14 @@ public class TransactionConfig { // DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource); // dstm.setGlobalRollbackOnParticipationFailure(false); // return dstm; +// } +// @ConditionalOnMissingBean(SecondaryMybatisConfig.class) +// @Primary +// @Bean +// public PlatformTransactionManager primaryTransactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource dataSource) { +// DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource); +// dstm.setGlobalRollbackOnParticipationFailure(false); +// return dstm; // } //////////////////////////////////////////////////////////////////////////////////////////// @@ -54,6 +61,7 @@ public class TransactionConfig { * @throws Exception Exception */ //@ConditionalOnProperty(value = "spring.datasource.hikari", havingValue = "oracle", matchIfMissing = false) + @ConditionalOnClass(SecondaryMybatisConfig.class) @Primary @Bean public PlatformTransactionManager transactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource mariaDS, diff --git a/mens-batch/src/main/resources/META-INF/spring.factories b/mens-batch/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..0c0d27a --- /dev/null +++ b/mens-batch/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +# kr.xit.core.spring.auto.PrimaryDatabaseAutoConfiguration, kr.xit.core.spring.auto.SecondaryDatabaseAutoConfiguration] +# diff --git a/mens-batch/src/main/resources/config/application-local.yml b/mens-batch/src/main/resources/config/application-local.yml index 3a94df3..ef0cb5a 100644 --- a/mens-batch/src/main/resources/config/application-local.yml +++ b/mens-batch/src/main/resources/config/application-local.yml @@ -9,7 +9,7 @@ spring: # root / xit1807 hikari: # 9 server - maria: + primary: driver-class-name: org.mariadb.jdbc.Driver jdbc-url: jdbc:mariadb://211.119.124.9:4407/mens?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true username: root @@ -19,7 +19,7 @@ spring: # 115 server # jdbc:oracle:thin:@211.119.124.115:1521:XITSMS # xit_sms_lg / xit_sms_lg - oracle: + secondary: driver-class-name: oracle.jdbc.OracleDriver jdbc-url: jdbc:oracle:thin:@211.119.124.115:1521:XITSMS username: xit_sms_lg diff --git a/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml b/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml index 252c955..cc4e8cf 100644 --- a/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml +++ b/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml @@ -651,7 +651,7 @@ AND tesm.ffnlg_code = #{ffnlgCode} - /** ens-mysql-mapper|selectSmsSendTgts-SMS 발송 대상 목록 조회|julim */ SELECT tesm.unity_sndng_mastr_id , tesm.sndng_mastr_id @@ -679,6 +679,25 @@ AND tesm.ffnlg_code = #{ffnlgCode} + +