基于springboot通过注解AOP动态切换druid多数据源--mybatis
控制于接口之上:
开始:demo地址 在lsr-core-base中
自定义注解:
/**
* @Description: 数据源切换注解
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DS {
DataSourceType value() default DataSourceType.db1;
}
数据源标识:
/**
* @Description: 数据源标识
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public enum DataSourceType {
db1,db2
}
数据源配置setting:
/**
* @Description: 数据源配置
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidSettings {
private String driverClassName;
private String url;
private String username;
private String password;
private String driverClassName2;
private String url2;
private String username2;
private String password2;
private int initialSize;
private int minIdle;
private int maxActive;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private String filters;
private int maxPoolPreparedStatementPerConnectionSize;
private boolean useGlobalDataSourceStat;
private String connectionProperties;
//get set
}
Druid配置类:
package cn.lsr.core.config.druid; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map; /**
* = = Druid配置类
*
* @Version: 1.0
* @Author: Hacker_lsr@126.com
*/
@Configuration
public class DruidConfig {
@Autowired
DruidSettings druidSettings;
/**
* 数据源db1
* @return
*/
//@ConfigurationProperties(prefix = "spring.datasource.db1")
@Bean(name = "dataSource01")
public DataSource dataSource01(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(druidSettings.getDriverClassName());
druidDataSource.setUrl(druidSettings.getUrl());
druidDataSource.setUsername(druidSettings.getUsername());
druidDataSource.setPassword(druidSettings.getPassword());
druidDataSource.setInitialSize(druidSettings.getInitialSize());
druidDataSource.setMinIdle(druidSettings.getMinIdle());
druidDataSource.setMaxActive(druidSettings.getMaxActive());
druidDataSource.setMaxWait(druidSettings.getMaxWait());
druidDataSource.setTimeBetweenEvictionRunsMillis(druidSettings.getTimeBetweenEvictionRunsMillis());
druidDataSource.setMinEvictableIdleTimeMillis(druidSettings.getMinEvictableIdleTimeMillis());
druidDataSource.setValidationQuery(druidSettings.getValidationQuery());
druidDataSource.setTestWhileIdle(druidSettings.isTestWhileIdle());
druidDataSource.setTestOnBorrow(druidSettings.isTestOnBorrow());
druidDataSource.setTestOnReturn(druidSettings.isTestOnReturn());
druidDataSource.setPoolPreparedStatements(druidSettings.isPoolPreparedStatements());
try {
druidDataSource.setFilters(druidSettings.getFilters());
} catch (Exception e) {
e.printStackTrace();
}
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidSettings.getMaxPoolPreparedStatementPerConnectionSize());
druidDataSource.setUseGlobalDataSourceStat(druidSettings.isUseGlobalDataSourceStat());
druidDataSource.setConnectionProperties(druidSettings.getConnectionProperties());
return druidDataSource;
} /**
* 数据源db2
* @return
*/
//@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean(name = "dataSource02")
public DataSource dataSource02(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(druidSettings.getDriverClassName2());
druidDataSource.setUrl(druidSettings.getUrl2());
druidDataSource.setUsername(druidSettings.getUsername2());
druidDataSource.setPassword(druidSettings.getPassword2());
druidDataSource.setInitialSize(druidSettings.getInitialSize());
druidDataSource.setMinIdle(druidSettings.getMinIdle());
druidDataSource.setMaxActive(druidSettings.getMaxActive());
druidDataSource.setMaxWait(druidSettings.getMaxWait());
druidDataSource.setTimeBetweenEvictionRunsMillis(druidSettings.getTimeBetweenEvictionRunsMillis());
druidDataSource.setMinEvictableIdleTimeMillis(druidSettings.getMinEvictableIdleTimeMillis());
druidDataSource.setValidationQuery(druidSettings.getValidationQuery());
druidDataSource.setTestWhileIdle(druidSettings.isTestWhileIdle());
druidDataSource.setTestOnBorrow(druidSettings.isTestOnBorrow());
druidDataSource.setTestOnReturn(druidSettings.isTestOnReturn());
druidDataSource.setPoolPreparedStatements(druidSettings.isPoolPreparedStatements());
try {
druidDataSource.setFilters(druidSettings.getFilters());
} catch (Exception e) {
e.printStackTrace();
}
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidSettings.getMaxPoolPreparedStatementPerConnectionSize());
druidDataSource.setUseGlobalDataSourceStat(druidSettings.isUseGlobalDataSourceStat());
druidDataSource.setConnectionProperties(druidSettings.getConnectionProperties()); return druidDataSource;
} /**
* 动态数据源管理
* @return
*/
@Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource(){
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//设置默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSource01());
Map<Object,Object> dbMap = new HashMap<>();
dbMap.put(DataSourceType.db1,dataSource01());
dbMap.put(DataSourceType.db2,dataSource02());
// targetDataSources 这里保存我们数据源配置的多个数据源)的数据源保存到resolvedDataSources
dynamicDataSource.setTargetDataSources(dbMap);
return dynamicDataSource;
} /**
* 将动态数据源放入事务管理器
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(dynamicDataSource());
}
/**
* 配置监控服务器
**/
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(
new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
// druid后台管理员用户
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
// 是否能够重置数据
initParams.put("resetEnable", "false"); bean.setInitParameters(initParams);
return bean;
} /**
* 配置web监控的过滤器
**/
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean(
new WebStatFilter());
// 添加过滤规则
bean.addUrlPatterns("/*");
Map<String,String> initParams = new HashMap<>();
// 忽略过滤格式
initParams.put("exclusions","*.js,*.css,*.icon,*.png,*.jpg,/druid/*");
bean.setInitParameters(initParams);
return bean;
} }
动态数据源切换:
/**
* @Description: 动态数据源切换
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final Logger log = LoggerFactory.getLogger(DynamicDataSource.class);
// 当前线程数据源
private static final ThreadLocal<DataSourceType> dataSourceContext = new ThreadLocal<>();
@Override
protected Object determineCurrentLookupKey() {
log.info("数据源:{}",getDataSource());
return getDataSource();
} /**
* 获取当前数据源
* @return
*/
public DataSourceType getDataSource(){
return dataSourceContext.get();
} /**
* 设置数据源
* @param dataSourceType
*/
public static void setDataSource(DataSourceType dataSourceType){
dataSourceContext.set(dataSourceType);
} /**
* 删除数据源
*/
public static void clearDataSource(){
dataSourceContext.remove();
}
}
注解动态数据源切换实现:
/**
* @Description: 动态数据源AOP
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
@Aspect
@Order(1)
@Component
public class DynamicDataSourceAspect {
@Before("@annotation(ds)")
public void beforeSwitchDS(JoinPoint joinPoint,DS ds){
//获取当前访问的class
Class<?> className = joinPoint.getTarget().getClass();
//获取方法名
String methodName = joinPoint.getSignature().getName();
//获取方法的参数类型
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Class[] arrClass = methodSignature.getParameterTypes();
//默认数据源
DataSourceType dataSourceType = DataSourceType.db1;
try {
//得到方法对象
Method method = className.getMethod(methodName, arrClass);
//判断是否有注解存在
if (method.isAnnotationPresent(DS.class)){
//拿到注解方法
DS dsValue = method.getAnnotation(DS.class);
//拿到注解value
dataSourceType = dsValue.value();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//切换数据源
DynamicDataSource.setDataSource(dataSourceType);
}
@After("@annotation(ds)")
public void afterSwitchDS(JoinPoint joinPoint,DS ds){
DynamicDataSource.clearDataSource();
}
}
application.properties##################################### 配置数据源 #####################################
##################################### 配置数据源 #####################################
datasource.db1url=192.168.0.104:3306/lsr-microservice
datasource.db2url=192.168.0.104:3306/lsr-microservice
##################################### 数据源1 #######################################
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://${datasource.db1url}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=lishirui
##################################### 数据源2 #######################################
spring.datasource.driver-class-name2=com.mysql.jdbc.Driver
spring.datasource.url2=jdbc:mysql://${datasource.db1url}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username2=root
spring.datasource.password2=lishirui
##################################### druid连接池 ###################################
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
#配置监控统计拦截的filters.去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,slf4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.useGlobalDataSourceStat=true
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
使用:@DS(DataSourceType.db1)
public interface UserMapper extends MyMapper<User> { User selectByName(String username); User selectUserByName(String username);
@DS(DataSourceType.db1)
User selecTest(String username); }
基于springboot通过注解AOP动态切换druid多数据源--mybatis的更多相关文章
- AOP获取方法注解实现动态切换数据源
AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...
- springboot 双数据源+aop动态切换
# springboot-double-dataspringboot-double-data 应用场景 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 多数据源 ...
- Spring AOP动态切换数据源
现在稍微复杂一点的项目,一个数据库也可能搞不定,可能还涉及分布式事务什么的,不过由于现在我只是做一个接口集成的项目,所以分布式就先不用了,用Spring AOP来达到切换数据源,查询不同的数据库就可以 ...
- 基于 Spring 和 iBATIS 的动态可更新多数据源持久层
前言 我们时常会遇到一些 web 项目,需要从不同的数据源中抓取数据来进行分析,而这些数据源是有可能变化的,需要用户来进行动态的维护和添加.可是,大多数的 web 程序使用了应用服务器或者容器中间件来 ...
- springboot集成mongodb实现动态切换数据源
主要实现原理,利用spring的aop 在切入点执行db操作之前 将数据库切换: 本例子采用aop在controller进行拦截 拦截到MongoTemplate.class 切换数据源后重新放回去 ...
- springAOP实现基于注解的数据源动态切换
需求 代码实现读写数据库分离 武器 spring3.0以上版本 实现思路 1.继承org.springframework.jdbc.datasource.lookup.AbstractRoutingD ...
- SPRINGAOP实现基于注解的数据源动态切换(转)
需求 代码实现读写数据库分离 武器 spring3.0以上版本 实现思路 1.继承org.springframework.jdbc.datasource.lookup.AbstractRoutingD ...
- Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源
本文链接:https://blog.csdn.net/wjy511295494/article/details/78825890 Springboot+Mybatis+Pagehelper+Aop ...
- 在使用 Spring Boot 和 MyBatis 动态切换数据源时遇到的问题以及解决方法
相关项目地址:https://github.com/helloworlde/SpringBoot-DynamicDataSource 1. org.apache.ibatis.binding.Bind ...
随机推荐
- Django的安装、使用详解、自动化测试应用以及程序打包
1.Django的安装 pip install Django 验证 Django 是否能被 Python 识别 >>> import django >>> prin ...
- 浅谈Power Signoff
Power Analysis是芯片设计实现中极重要的一环,因为它直接关系到芯片的性能和可靠性.Power Analysis 需要Timing Analysis 产生包含频率.transition 等时 ...
- MySQL-THINKPHP 商城系统二 商品模块的展示
上回已经说到,商品被分为spu商品和sku商品 , ------------------------------------------------------------------------- ...
- thinkphp的where 之 or的使用
需要生成 SELECT * FROM `goods` WHERE ( `goodstype_id` = 2 or `goodstype_id` = 3 ) $where['goodstype_id'] ...
- Codeforces Round #624 (Div. 3) F
题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度 ...
- Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...
- dbGet net trace instant pin
proc rn { net_name } {puts " "puts "Net name : $net_name : "set name_rule [dbget ...
- redis环境搭建学习笔记
学习环境为windows.java环境 一.学习教程: 1.菜鸟教程:http://www.runoob.com/redis/redis-tutorial.html 2.redis中文网:http:/ ...
- Python学习之列表篇
浮点数类型:round(x,d)可对浮点数进行四舍五入,科学计数法:aeb表示a*10^bpython大小写敏感整数类型:无范围限制,pow(x,y)表示x^y,想算多大算多大,四种表示形式:十进制, ...
- python中 使用join()方法
使用join()方法 对各种数据类型中元的素进行合并拼接 "连接符".join(列表或元组或字符串或字典) 返回的是一个使用连接符进行拼接的字符串 如果对象是列表,元组,就是以一个 ...