- 后端:引入 druid spring boot

- 后端:解决多数据源的循环依赖问题
pull/1/head
YunaiV 2019-05-12 20:44:52 +08:00
parent 4888dfaf2a
commit b9c05e6742
4 changed files with 51 additions and 32 deletions

View File

@ -31,6 +31,7 @@
<!-- <com.alibab.dubbo.version>2.6.5</com.alibab.dubbo.version>-->
<dubbo.version>2.7.1</dubbo.version>
<mysql-connector-java.version>5.1.46</mysql-connector-java.version>
<druid.version>1.1.16</druid.version>
<!-- <dubbo-spring-boot-starter.version>0.2.1.RELEASE</dubbo-spring-boot-starter.version>-->
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<curator.version>2.13.0</curator.version>
@ -84,6 +85,11 @@
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>

View File

@ -41,6 +41,10 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
@ -109,12 +113,10 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework</groupId>-->

View File

@ -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);
}
/**

View File

@ -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: