- 后端:引入 druid spring boot
- 后端:解决多数据源的循环依赖问题
This commit is contained in:
parent
4888dfaf2a
commit
b9c05e6742
6
pom.xml
6
pom.xml
@ -31,6 +31,7 @@
|
|||||||
<!-- <com.alibab.dubbo.version>2.6.5</com.alibab.dubbo.version>-->
|
<!-- <com.alibab.dubbo.version>2.6.5</com.alibab.dubbo.version>-->
|
||||||
<dubbo.version>2.7.1</dubbo.version>
|
<dubbo.version>2.7.1</dubbo.version>
|
||||||
<mysql-connector-java.version>5.1.46</mysql-connector-java.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>-->
|
<!-- <dubbo-spring-boot-starter.version>0.2.1.RELEASE</dubbo-spring-boot-starter.version>-->
|
||||||
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
|
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
|
||||||
<curator.version>2.13.0</curator.version>
|
<curator.version>2.13.0</curator.version>
|
||||||
@ -84,6 +85,11 @@
|
|||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<version>${mysql-connector-java.version}</version>
|
<version>${mysql-connector-java.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>${druid.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mapstruct</groupId>
|
<groupId>org.mapstruct</groupId>
|
||||||
|
@ -41,6 +41,10 @@
|
|||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||||
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
|
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
|
||||||
@ -109,12 +113,10 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-tx</artifactId>
|
<artifactId>spring-tx</artifactId>
|
||||||
<version>5.1.3.RELEASE</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-jdbc</artifactId>
|
<artifactId>spring-jdbc</artifactId>
|
||||||
<version>5.1.3.RELEASE</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.springframework</groupId>-->
|
<!-- <groupId>org.springframework</groupId>-->
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
package cn.iocoder.mall.promotion.biz.config;
|
package cn.iocoder.mall.promotion.biz.config;
|
||||||
|
|
||||||
import com.alibaba.druid.pool.DruidDataSource;
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||||
import io.seata.rm.datasource.DataSourceProxy;
|
import io.seata.rm.datasource.DataSourceProxy;
|
||||||
import io.seata.spring.annotation.GlobalTransactionScanner;
|
import io.seata.spring.annotation.GlobalTransactionScanner;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.ConfigurationProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.DependsOn;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@ -19,7 +18,7 @@ import javax.sql.DataSource;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@MapperScan("cn.iocoder.mall.promotion.biz.dao") // 扫描对应的 Mapper 接口
|
@MapperScan("cn.iocoder.mall.promotion.biz.dao") // 扫描对应的 Mapper 接口
|
||||||
@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600
|
@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。为什么使用 proxyTargetClass 参数,参见 https://blog.csdn.net/huang_550/article/details/76492600
|
||||||
@EnableConfigurationProperties(DataSourceProperties.class)
|
//@EnableConfigurationProperties(DataSourceProperties.class)
|
||||||
public class DatabaseConfiguration {
|
public class DatabaseConfiguration {
|
||||||
|
|
||||||
// 数据源,使用 HikariCP
|
// 数据源,使用 HikariCP
|
||||||
@ -27,41 +26,47 @@ public class DatabaseConfiguration {
|
|||||||
@Value("${spring.application.name}")
|
@Value("${spring.application.name}")
|
||||||
private String applicationId;
|
private String applicationId;
|
||||||
|
|
||||||
@Autowired
|
// @Autowired
|
||||||
private DataSourceProperties dataSourceProperties;
|
// private DataSourceProperties dataSourceProperties;
|
||||||
|
//
|
||||||
// @Bean // TODO 芋艿,加了就一直报错,后面在找原因。
|
//// @Bean // TODO 芋艿,加了就一直报错,后面在找原因。
|
||||||
// @Primary
|
// @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(){
|
public DruidDataSource druidDataSource(){
|
||||||
DruidDataSource druidDataSource = new DruidDataSource();
|
return DruidDataSourceBuilder.create().build();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConfigurationProperties(prefix = "spring.datasource")
|
@ConfigurationProperties(prefix = "spring.datasource")
|
||||||
@Primary
|
@Primary
|
||||||
@Bean("dataSource")
|
@Bean("dataSource")
|
||||||
// @Bean
|
// @Bean
|
||||||
|
@DependsOn("druidDataSource") // 解决多数据源,循环依赖的问题。主要发生点在 DataSourceInitializerInvoker
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
DruidDataSource dataSource = druidDataSource();
|
DruidDataSource druidDataSource = druidDataSource();
|
||||||
|
return new DataSourceProxy(druidDataSource);
|
||||||
return new DataSourceProxy(dataSource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
spring:
|
spring:
|
||||||
# datasource
|
# datasource
|
||||||
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
|
url: jdbc:mysql://180.167.213.26:13306/mall_promotion?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||||
driver-class-name: com.mysql.jdbc.Driver
|
driver-class-name: com.mysql.jdbc.Driver
|
||||||
username: root
|
username: root
|
||||||
password: ${MALL_MYSQL_PASSWORD}
|
password: ${MALL_MYSQL_PASSWORD}
|
||||||
|
# TODO 芋艿, 后续优化下 druid 参数
|
||||||
|
druid:
|
||||||
|
initial-size: 5
|
||||||
|
max-active: 5
|
||||||
|
max-wait: 10000
|
||||||
|
|
||||||
# mybatis
|
# mybatis
|
||||||
#mybatis:
|
#mybatis:
|
||||||
|
Loading…
Reference in New Issue
Block a user