From b9c05e6742d60c6b17ee8bcda698aab8e3e4826f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 12 May 2019 20:44:52 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=90=8E=E7=AB=AF=EF=BC=9A=E5=BC=95?= =?UTF-8?q?=E5=85=A5=20druid=20spring=20boot=20-=20=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=EF=BC=9A=E8=A7=A3=E5=86=B3=E5=A4=9A=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E7=9A=84=E5=BE=AA=E7=8E=AF=E4=BE=9D=E8=B5=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ promotion/promotion-service-impl/pom.xml | 6 +- .../biz/config/DatabaseConfiguration.java | 65 ++++++++++--------- .../main/resources/config/application.yaml | 6 ++ 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index 0b9528a1c..7cfaf135c 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ 2.7.1 5.1.46 + 1.1.16 1.3.0.Final 2.13.0 @@ -84,6 +85,11 @@ mysql-connector-java ${mysql-connector-java.version} + + com.alibaba + druid-spring-boot-starter + ${druid.version} + org.mapstruct diff --git a/promotion/promotion-service-impl/pom.xml b/promotion/promotion-service-impl/pom.xml index ed8ae8e00..a9f4293b5 100644 --- a/promotion/promotion-service-impl/pom.xml +++ b/promotion/promotion-service-impl/pom.xml @@ -41,6 +41,10 @@ mysql mysql-connector-java + + com.alibaba + druid-spring-boot-starter + @@ -109,12 +113,10 @@ org.springframework spring-tx - 5.1.3.RELEASE org.springframework spring-jdbc - 5.1.3.RELEASE diff --git a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java index a98fefa88..8c682c6d0 100644 --- a/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java +++ b/promotion/promotion-service-impl/src/main/java/cn/iocoder/mall/promotion/biz/config/DatabaseConfiguration.java @@ -1,16 +1,15 @@ package cn.iocoder.mall.promotion.biz.config; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import io.seata.rm.datasource.DataSourceProxy; import io.seata.spring.annotation.GlobalTransactionScanner; import org.mybatis.spring.annotation.MapperScan; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Primary; import org.springframework.transaction.annotation.EnableTransactionManagement; @@ -19,7 +18,7 @@ import javax.sql.DataSource; @Configuration @MapperScan("cn.iocoder.mall.promotion.biz.dao") // 扫描对应的 Mapper 接口 @EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600 -@EnableConfigurationProperties(DataSourceProperties.class) +//@EnableConfigurationProperties(DataSourceProperties.class) public class DatabaseConfiguration { // 数据源,使用 HikariCP @@ -27,41 +26,47 @@ public class DatabaseConfiguration { @Value("${spring.application.name}") private String applicationId; - @Autowired - private DataSourceProperties dataSourceProperties; - -// @Bean // TODO 芋艿,加了就一直报错,后面在找原因。 +// @Autowired +// private DataSourceProperties dataSourceProperties; +// +//// @Bean // TODO 芋艿,加了就一直报错,后面在找原因。 // @Primary +// public DruidDataSource druidDataSource(){ +// DruidDataSource druidDataSource = new DruidDataSource(); +// druidDataSource.setUrl(dataSourceProperties.getUrl()); +// druidDataSource.setUsername(dataSourceProperties.getUsername()); +// druidDataSource.setPassword(dataSourceProperties.getPassword()); +// druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); +// druidDataSource.setInitialSize(0); +// druidDataSource.setMaxActive(180); +// druidDataSource.setMaxWait(60000); +// druidDataSource.setMinIdle(0); +// druidDataSource.setValidationQuery("Select 1 from DUAL"); +// druidDataSource.setTestOnBorrow(false); +// druidDataSource.setTestOnReturn(false); +// druidDataSource.setTestWhileIdle(true); +// druidDataSource.setTimeBetweenEvictionRunsMillis(60000); +// druidDataSource.setMinEvictableIdleTimeMillis(25200000); +// druidDataSource.setRemoveAbandoned(true); +// druidDataSource.setRemoveAbandonedTimeout(1800); +// druidDataSource.setLogAbandoned(true); +// return druidDataSource; +// } + + @Bean("druidDataSource") + @ConfigurationProperties("spring.datasource.druid") public DruidDataSource druidDataSource(){ - DruidDataSource druidDataSource = new DruidDataSource(); - druidDataSource.setUrl(dataSourceProperties.getUrl()); - druidDataSource.setUsername(dataSourceProperties.getUsername()); - druidDataSource.setPassword(dataSourceProperties.getPassword()); - druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); - druidDataSource.setInitialSize(0); - druidDataSource.setMaxActive(180); - druidDataSource.setMaxWait(60000); - druidDataSource.setMinIdle(0); - druidDataSource.setValidationQuery("Select 1 from DUAL"); - druidDataSource.setTestOnBorrow(false); - druidDataSource.setTestOnReturn(false); - druidDataSource.setTestWhileIdle(true); - druidDataSource.setTimeBetweenEvictionRunsMillis(60000); - druidDataSource.setMinEvictableIdleTimeMillis(25200000); - druidDataSource.setRemoveAbandoned(true); - druidDataSource.setRemoveAbandonedTimeout(1800); - druidDataSource.setLogAbandoned(true); - return druidDataSource; + return DruidDataSourceBuilder.create().build(); } @ConfigurationProperties(prefix = "spring.datasource") @Primary @Bean("dataSource") // @Bean + @DependsOn("druidDataSource") // 解决多数据源,循环依赖的问题。主要发生点在 DataSourceInitializerInvoker public DataSource dataSource() { - DruidDataSource dataSource = druidDataSource(); - - return new DataSourceProxy(dataSource); + DruidDataSource druidDataSource = druidDataSource(); + return new DataSourceProxy(druidDataSource); } /** diff --git a/promotion/promotion-service-impl/src/main/resources/config/application.yaml b/promotion/promotion-service-impl/src/main/resources/config/application.yaml index 73ba7adc2..0d7dfd6e7 100644 --- a/promotion/promotion-service-impl/src/main/resources/config/application.yaml +++ b/promotion/promotion-service-impl/src/main/resources/config/application.yaml @@ -1,10 +1,16 @@ spring: # datasource datasource: + type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://180.167.213.26:13306/mall_promotion?useSSL=false&useUnicode=true&characterEncoding=UTF-8 driver-class-name: com.mysql.jdbc.Driver username: root password: ${MALL_MYSQL_PASSWORD} + # TODO 芋艿, 后续优化下 druid 参数 + druid: + initial-size: 5 + max-active: 5 + max-wait: 10000 # mybatis #mybatis: