springboot 配置quart多数据源
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();
}
}
springboot 配置quart多数据源的更多相关文章
- 037.[转] springboot 配置多个数据源
1.在application.properties文件 配置两个数据源 #默认使用 tomcat-jdbc spring.datasource.type=org.apache.tomcat.jdbc. ...
- 使用springboot配置和注入数据源属性的方法和步骤
/** 1.书写一个名为resources/application.properties的属性文件---->书写一个配置属性类,类名为: **/ 文件:application.propertie ...
- Springboot配置连接两个数据库
背景: 项目中需要从两个不同的数据库查询数据,之前实现方法是:springboot配置连接一个数据源,另一个使用jdbc代码连接. 为了改进,现在使用SpringBoot配置连接两个数据源 实现效果: ...
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot入门之基于Druid配置Mybatis多数据源
上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,Sp ...
- SpringBoot配置多数据源时遇到的问题
SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...
- springboot配置多数据源(JdbcTemplate方式)
在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer…) 如果使用springboot开发,可做如下配置: Co ...
- 接管SpringBoot对Activiti的数据源自动配置
SpringBoot的自动配置真的让人又爱又恨,但还是爱更多一点. SpringBoot想要帮我们自动配置好一切,但是有时候配置的却并不是我们需要的,甚至有时候会默默的坑我们. 我的项目是一个多数据源 ...
- SpringBoot整合Mybatis,多数据源,事务,支持java -jar 启动.
用了一段时间SpringBoot,之前配置MYBATIS ,在打包WAR 放到tomcat下正常,但是WAR已经过时了,现在流行直接打包JAR 丢到DOCKER 里,无奈JAR 启动的时候MAPPER ...
随机推荐
- python 并发编程 多线程 目录
线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...
- neo4j - 查询效率的几种优化思路
最近在公司实习做的就是优化neo4j图形数据库查询效率的事,公司提供的是一个在Linux上搭建且拥有几亿个节点的数据库.开始一段时间主要是熟悉该数据库的一些基本操作,直到上周才正式开始步入了优化数据库 ...
- HTTP协议的概念作用和特点
Web交互的基本流程 请求:客户端根据服务器地址把数据发送给服务器的过程叫请求. 相应:服务器将请求的处理结果发送给浏览器的过程叫响应. 什么是HTTP? 概念:超文本传输协议. HTTP有什么作用? ...
- java对象 Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...
- PostgreSQL-优化之分表
分表概述 数据库分表,就是把一张表分成多张表,物理上虽然分开了,逻辑上彼此仍有联系. 分表有两种方式:水平分表,即按列分开:垂直分表,即按行分开 优势 1. 查询速度大幅提升 2. 删除数据速度更快 ...
- datetime的timedelta对象
datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象. 如果有人问你昨天是几号,这个很容易就回答出来了.但是如果 ...
- qt table中字体倾斜
在itemdelegate,的paint事件中添加 QStyleOptionViewItem newOption(option); QTransform transfor ...
- java复习(3)继承
一.继承为题的提出 ---------------------------------------------------- 我们知道面向对象的三大特性是:封装.继承和多态,可以知道继承在java应用 ...
- Chrome之谷歌插件开发
最近碰到一个需求,需要在某个平台上批量的添加好友,如果是人工点击,可以操作,但是效率并不高,人工成本较高.就打算使用浏览器插件的方式来完成这件重复性的工作. 介绍: Chrome插件的本质就是一个由 ...
- error: (-215) !empty() in function detectMultiScale
tips: pip install opencv-python or https://www.lfd.uci.edu/~gohlke/pythonlibs/ 原因确实是找不到 opencv 的 xml ...