Spring Boot HikariCP 一 ——集成多数据源
其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的。
参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource-demo?spm=5176.100239.blogcont188540.13.iARYDh。
这块内容前前后后总共写了三篇
1. Spring Boot HikariCP 一 ——集成多数据源
2. Spring Boot 动态切换数据源二——负载均衡
3. Spring Boot 动态切换数据源三——动态获取配置文件中的配置信息
4. 插件GitHubrhettpang/dynamic-datasource
读写分离的功能我已经使用replication集成好了,因为我们需要单独设置每个数据源的链接属性,而且使用的还是Hikari数据源,所以又在网上找了两天,最终昨天晚上发现了这种方式。
我这里说说自己集成的时候的一些注意点。
配置文件:
hikari:
master:
jdbc-url: jdbc:mysql://masterhost:3306/testdb?useUnicode=true&characterEncoding=utf8&useSSL=true&allowMultiQueries=true&verifyServerCertificate=false
username: root
password: root
maximum-pool-size: 20
pool-name: master
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1765000
slave:
jdbc-url: jdbc:mysql://slavehost:3306/testdb?useUnicode=true&characterEncoding=utf8&useSSL=true&allowMultiQueries=true&verifyServerCertificate=false
username: root
password: root
maximum-pool-size: 80
pool-name: slave
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1765000
read-only: true
我这里主要用到的是maximum-pool-size这个值,一般情况下读数据库(slave)总会比写(master)要多一些,而且往往是一个master多个slave。所以,maximum-pool-size这个值在master的设置小于slave比较高效。
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Created by pangkunkun on 2017/12/18.
*/
@Component
@ConfigurationProperties(prefix = "hikari")
public class DBProperties {
private HikariDataSource master;
private HikariDataSource slave;
public HikariDataSource getMaster() {
return master;
}
public void setMaster(HikariDataSource master) {
this.master = master;
}
public HikariDataSource getSlave() {
return slave;
}
public void setSlave(HikariDataSource slave) {
this.slave = slave;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* @author Created by pangkunkun on 2017/12/18.
*/
@Configuration
@EnableScheduling
public class DataSourceConfig {
@Autowired
private DBProperties properties;
@Bean(name = "dataSource")
public DataSource dataSource() {
//按照目标数据源名称和目标数据源对象的映射存放在Map中
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("master", properties.getMaster());
targetDataSources.put("slave", properties.getSlave());
//采用是想AbstractRoutingDataSource的对象包装多数据源
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
//设置默认的数据源,当拿不到数据源时,使用此配置
dataSource.setDefaultTargetDataSource(properties.getMaster());
return dataSource;
}
@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource());
}
}
import com.easyar.cloud.cms.common.util.TargetDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author Created by pangkunkun on 2017/12/18.
*/
@Component
@Aspect
public class DataSourceAspect {
private final static Logger log= LoggerFactory.getLogger(DataSourceAspect.class);
//切换放在mapper接口的方法上,所以这里要配置AOP切面的切入点
@Pointcut("execution( * com.easyar.cloud.cms.dao.mapper.*.*(..))||execution( * com.easyar.cloud.cms.shiro.mapper.*.*(..))")
public void dataSourcePointCut() {
}
@Before("dataSourcePointCut()")
public void before(JoinPoint joinPoint) {
Object target = joinPoint.getTarget();
String method = joinPoint.getSignature().getName();
Class<?>[] clazz = target.getClass().getInterfaces();
Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
try {
Method m = clazz[0].getMethod(method, parameterTypes);
//如果方法上存在切换数据源的注解,则根据注解内容进行数据源切换
if (m != null && m.isAnnotationPresent(TargetDataSource.class)) {
TargetDataSource data = m.getAnnotation(TargetDataSource.class);
String dataSourceName = data.value();
DynamicDataSourceHolder.putDataSource(dataSourceName);
log.debug("current thread " + Thread.currentThread().getName() + " add " + dataSourceName + " to ThreadLocal");
} else {
log.debug("switch datasource fail,use default");
}
} catch (Exception e) {
log.error("current thread " + Thread.currentThread().getName() + " add data to ThreadLocal error", e);
}
}
//执行完切面后,将线程共享中的数据源名称清空
@After("dataSourcePointCut()")
public void after(JoinPoint joinPoint){
DynamicDataSourceHolder.removeDataSource();
}
}
原文:https://blog.csdn.net/qq_35981283/article/details/78846892
Spring Boot HikariCP 一 ——集成多数据源的更多相关文章
- 14、Spring Boot 2.x 集成 Druid 数据源
14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos
- Spring Boot 2.0 集成 Druid 数据源
一.Maven项目依赖 <!-- 开发者工具(热部署 修改classpath下的文件springboot自动重启) --> <dependency> <groupId&g ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
- Spring Boot系列——如何集成Log4j2
上篇<Spring Boot系列--日志配置>介绍了Spring Boot如何进行日志配置,日志系统用的是Spring Boot默认的LogBack. 事实上,除了使用默认的LogBack ...
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- 15、Spring Boot 2.x 集成 Swagger UI
1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...
随机推荐
- Centos 7环境下安装配置Hadoop 3.0 Beta1简记
前言 由于以前已经写过一篇Centos 7环境下安装配置2.8的随笔,因此这篇写得精简些,只挑选一些重要环节记录一下. 安装环境为:两台主机均为Centos 7.*操作系统,两台机器配置分别为: 主机 ...
- Access denied when I try to install profiler
I had the same issue and used the diagtool to find more information. The traces showed this error me ...
- 使用Fiddler 4 调用WebService
Fiddler让我们这些.neter感到非常欣慰, 是用C#写出来的,它包含一个简单却功能强大的基于JScript .NET 事件脚本子系统,它的灵活性非常棒,可以支持众多的http调试任务,并且能够 ...
- 图解 (a + b) * (a + b) == a**2 + 2*a*b + b**2
示意图
- windows server 2003产生的 Minidmp蓝屏文件分析求助
在机房运行的四台服务器中均出现了蓝屏dmp文件,经过整理发现CDMS主备服务器最近(2018年1月开始)蓝屏的dmp很多.经过自己的学习分析发现不足以找到先关的原因和处理方法,希望得到大牛们的帮助.以 ...
- java中二维数组的排序
首先定义一个5X8的二维数组,然后使用随机数填充满.借助Arrays的方法对二维数组进行排序.参考思路:先把二维数组使用System.arraycopy进行数组复制到一个一维数组然后使用sort进行排 ...
- python slenium 中CSS定位
以百度为例 一.通过id.class定位 1.#id:python:driver.find_element_by_css_selector('input#kw') 2..class:python:dr ...
- java面试总结(资料来源网络)
core java: 一.集合 1.hashMap 结构如图: HashMap在Map.Entry静态内部类实现中存储key-value对. HashMap使用哈希算法.在put和get方法中.它使用 ...
- 页面跳转、URL直接访问限制
问题 URL控制是为了避免以下错误 当前页需要读取上一页缓存,但是当前页直接通过URL访问无法获得相应的缓存 当前页需要通过入口,清楚历史中保留的缓存,但是当前页直接通过URL访问没有清除 本质上是为 ...
- pyqt-QGrapicsView 坐标系详解
PTQT——GraphicsView框架 转载 原网址 http://blog.51cto.com/9291927/1879128 一.GraphicsView框架简介 QT4.2开始引入了Graph ...