Springboot版本为2.1.6 多数据源配置使用druid进行配置,数据库使用的为Oracle11g,如果使用的是MySQL,直接将数据库的地址和驱动改一下即可
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
二.Quartz配置介绍
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
druid:
business: # 配置业务数据源
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: business
password: business
quartz: #配置Quartz数据源
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: quartz
password: quartz
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 15
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j2
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
remove-abandoned: true
remove-abandoned-timeout: 180
log-abandoned: true
quartz:
jdbc:
initialize-schema: never #配置是否每次重启项目都自动生成Quartz表结构,在此使用always生成一次后就可以改为never配置
job-store-type: jdbc
properties:
org:
quartz:
scheduler:
instanceName: etlCleanScheduler
instanceId: AUTO
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_ #Quartz表前缀
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
#线程数 一个任务使用一个线程
threadCount: 100
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
三.配置多数据源
package com.rubikstack.etlclean.config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
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.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
import java.util.Properties;
@Slf4j
@Configuration
@MapperScan(basePackages = DataSourceConfig.MAPPER_PACKAGE, sqlSessionFactoryRef = DataSourceConfig.SESSION_FACTORY)
public class DataSourceConfig {
@Value("${mybatis.queryLimit}")
private String queryLimit;
static final String SESSION_FACTORY = "dbSqlSessionFactory";
private static final String DATASOURCE_NAME = "dbDataSource";
/**
* mapper类的包路径
*/
static final String MAPPER_PACKAGE = "com.example.mapper";
static final String MODEL_PACKAGE = "com.example.model";
/**
* 自定义mapper的xml文件路径
*/
private static final String MAPPER_XML_PATH = "classpath*:com.example.mapper/*Mapper.xml";
/**
* 数据源配置的前缀,必须与application.properties中配置的对应数据源的前缀一致
*/
private static final String BUSINESS_DATASOURCE_PREFIX = "spring.datasource.druid.business";
private static final String QUARTZ_DATASOURCE_PREFIX = "spring.datasource.druid.quartz";
@Primary
@Bean(name = DATASOURCE_NAME)
@ConfigurationProperties(prefix = BUSINESS_DATASOURCE_PREFIX)
public DruidDataSource druidDataSource() {
return new DruidDataSource();
}
/**
* 配置Mybatis环境
*/
@Primary
@Bean(name = SESSION_FACTORY)
public SqlSessionFactory sqlSessionFactory() {
log.info("配置SqlSessionFactory开始");
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(druidDataSource());
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 自定义mapper的xml文件地址,当通用mapper提供的默认功能无法满足我们的需求时,可以自己添加实现,与mybatis写mapper一样
sessionFactoryBean.setMapperLocations(resolver.getResources(MAPPER_XML_PATH));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
Properties properties = new Properties();
properties.put("queryLimit",queryLimit);
configuration.setVariables(properties);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
sessionFactoryBean.setConfiguration(configuration);
sessionFactoryBean.setTypeAliasesPackage(MODEL_PACKAGE);
return sessionFactoryBean.getObject();
} catch (Exception e) {
log.error("配置SqlSessionFactory失败,error:{}", e.getMessage());
throw new RuntimeException(e.getMessage());
}
}
/**
* @QuartzDataSource 注解则是配置Quartz独立数据源的配置
*/
@Bean
@QuartzDataSource
@ConfigurationProperties(prefix = QUARTZ_DATASOURCE_PREFIX)
public DataSource quartzDataSource(){
return new DruidDataSource();
}
}