spring 配置多数据源(mysql读写分离)
前段时间刚换了家新公司,然后看项目代码里用了数据库读写分离的架构,然后好奇扒了代码简单看了下,总体来说就是运用spring aop切面方式来实现的。看明白后就在自己的个人小项目里运用了下,测试OK,所以下面总结下流程:
1、首先定义一个数据源注解,它有两个值,一个对应写库(主库),一个对应读库(从库)
package com.jdd.ds;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String DATA_SOURCE_READ = "dataSourceRead";
String DATA_SOURCE_WRITE = "dataSourceWrite";
String name() default "dataSourceWrite";
}
2、在需要拦截的方法上加上该注解
package com.jdd.service.impl;
import com.jdd.dao.UserDao;
import com.jdd.ds.DataSource;
import com.jdd.pojo.User;
import com.jdd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@DataSource(name=DataSource.DATA_SOURCE_READ)
@Override
public User getUserByNameAndPassword(String name, String password) {
return userDao.getUserByNameAndPassword(name, password);
}
@DataSource(name=DataSource.DATA_SOURCE_WRITE)
@Override
public int insertUser(User user) {
return userDao.insertUser(user);
}
}
3、然后在配置文件里加上aop切面配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 扫描包加载Service实现类 -->
<context:component-scan base-package="com.jdd.service"></context:component-scan>
<bean id="dataSourceExchange" class="com.jdd.ds.DataSourceExchange"></bean>
<aop:config>
<aop:aspect ref="dataSourceExchange">
<aop:around method="execute" pointcut="within(com.jdd.service.impl.*)"></aop:around>
</aop:aspect>
</aop:config>
</beans>
4、然后我们要在切面类里,获取到方法上的dataSource注解里设置的值,从而决定使用哪个数据源
package com.jdd.ds;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
public class DataSourceExchange {
private static Logger logger = LoggerFactory.getLogger(DataSourceExchange.class);
public Object execute(ProceedingJoinPoint pjp){
logger.info("DataSourceExchange==>");
Object obj = null;
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Method targetMethod = methodSignature.getMethod();
Method realMethod = null;
try {
realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
if(realMethod.isAnnotationPresent(DataSource.class)){
DataSource datasource = (DataSource) realMethod.getAnnotation(DataSource.class);
DataSourceContext.setDbType(datasource.name());
logger.info(realMethod.getName() +" set dbtype==>"+datasource.name());
}else{
DataSourceContext.setDbType("dataSourceWrite");
}
obj = pjp.proceed();
} catch (Throwable t) {
t.printStackTrace();
}
logger.info(realMethod.getName()+" clear datatype==>");
DataSourceContext.clearDbType();
return obj;
}
}
在方法执行前,设置具体数据源,然后方法执行完后,再清除掉该值。
注:注意上面的通过反射获取业务方法的代码, 开始的时候用
MethodSignature signature = (MethodSignature)pjp.getSignature();
Method method = signature.getMethod();
发现通过这种方法获取的method,它是没有注解信息的。后来在网上搜了下,大概说是这种方法获取的是代理方法,不是目标方法。代理方法是不带注解信息的,所以这块注意下。
5、下面看下 DataSourceContext 类,作用自然就是设置数据源上下文。
package com.jdd.ds;
public class DataSourceContext {
public static final String DATA_SOURCE_READ = "dataSourceRead";
public static final String DATA_SOURCE_WRITE = "dataSourceWrite";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public DataSourceContext(){
}
public static void setDbType(String dbType){
contextHolder.set(dbType);
}
public static String getDbType(){
return (String)contextHolder.get();
}
public static void clearDbType(){
contextHolder.remove();
}
}
6、下面定义 MultipleDataSource类, 继承与spring的 AbstractRoutingDataSource类, 并重写它的 determineCurrentLookupKey 方法, 作用就是从上下文获取当前线程使用的数据源标识:
package com.jdd.ds;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
Object key = DataSourceContext.getDbType();
if(key != null){
this.logger.info("当前线程使用的数据源标识为 [ " + key.toString() + " ].");
}
return key;
}
}
7、最后在 配置文件 applicationContext-mysql.xml 里 配上数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties" />
<!-- 数据库连接池 -->
<bean id="dataSourceWrite" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${url.write}" />
<property name="username" value="${username.write}" />
<property name="password" value="${password.write}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<bean id="dataSourceRead" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${url.read}" />
<property name="username" value="${username.read}" />
<property name="password" value="${password.read}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<bean id="multipleDataSource" class="com.jdd.ds.MultipleDataSource">
<property name="defaultTargetDataSource" ref="dataSourceWrite" />
<property name="targetDataSources">
<map>
<entry value-ref="dataSourceWrite" key="dataSourceWrite"></entry>
<entry value-ref="dataSourceRead" key="dataSourceRead"></entry>
</map>
</property>
</bean>
<!-- sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
<property name="dataSource" ref="multipleDataSource"></property>
<property name="mapperLocations" value="classpath:com/jdd/mapper/*.xml"></property>
</bean>
</beans>
8、到这里和spring相关的配置基本就配完了, 其实后面还要再配置一下 mysql的主从复制,就是对写库的操作都同步到从库,这样写库从库的数据才一致。具体配置操作我就不写了,可以自行到网上搜索。
spring 配置多数据源(mysql读写分离)的更多相关文章
- 使用Spring配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考.关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!-- ...
- 阿里P7教你如何使用 Spring 配置动态数据源实现读写分离
最近搭建的一个项目需要实现数据源的读写分离,在这里将代码进行分享,以供参考. 关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!- ...
- 使用 Spring 配置动态数据源实现读写分离
关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!--读数据源配置--><bean id="readData ...
- spring 配置双数据源并读写分离
摘自 开源项目Ibase4j 关键思想在于AbstractRoutingSource 类 还有方法名称和切入点去控制使用哪个数据源 1.首先在配置文件配置多个数据源 并且交给继承自spri ...
- spring集成mybatis实现mysql读写分离
前言 在网站的用户达到一定规模后,数据库因为负载压力过高而成为网站的瓶颈.幸运的是目前大部分的主流数据库都提供主从热备功能,通过配置两台数据库主从关系,可以将一台数据库的数据更新同步到另一台服务器上. ...
- mybatis用spring的动态数据源实现读写分离
一.环境: 三个mysql数据库.一个master,两个slaver.master写数据,slaver读数据. 二.原理: 借助Spring的 AbstractRoutingDataSource 这个 ...
- Spring Boot2(四):使用Spring Boot多数据源实现读写分离
前言 实际业务场景中,不可能只有一个库,所以就有了分库分表,多数据源的出现.实现了读写分离,主库负责增改删,从库负责查询.这篇文章将实现Spring Boot如何实现多数据源,动态数据源切换,读写分离 ...
- ThinkPHP配置简单的mysql读写分离
ThinkPHP内置了分布式数据库的支持,包括主从式数据库的读写分离,但是分布式数据库必须是相同的数据库类型. 配置DB_DEPLOY_TYPE 为1 可以采用分布式数据库支持.如果采用分布式数据库, ...
- 原理解密 → Spring AOP 实现动态数据源(读写分离),底层原理是什么
开心一刻 女孩睡醒玩手机,收到男孩发来一条信息:我要去跟我喜欢的人表白了! 女孩的心猛的一痛,回了条信息:去吧,祝你好运! 男孩回了句:但是我没有勇气说不来,怕被打! 女孩:没事的,我相信你!此时女孩 ...
- Spring配置动态数据源-读写分离和多数据源
在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多 ...
随机推荐
- boost::coroutine 无法显示调用栈
boost::coroutine 无法显示调用栈(金庆的专栏)一例因 boost::format() 格式化参数个数错误造成的 coredump,因为使用了 boost::coroutine, 无法显 ...
- awk:快速入门(简单实用19例+鸟哥书内容)
awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 ...
- msm8974 camera driver添加新摄像头kernel hal修改
添加一款新摄像头流程 1添加sensor kernel driver, 主要实现上电.rst.pwd.mclk等power setting,sensor prob & sensor i2c ...
- CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤
准备文件如下: 1.CentOS-6.4-x86_64-bin-DVD1.iso 2.jdk-7u67-linux-x64.rpm 3.apache-tomcat-7.0.55.tar.gz 安装步骤 ...
- 《java入门第一季》之根据小案例体会泛型
泛型在哪些地方使用呢? * 看API,如果类,接口,抽象类后面跟的有<E>就说要使用泛型.一般来说就是在集合中使用. 下面根据案例,加深体会泛型的作用. 案例一: import java. ...
- Torch的安装和学习
Long long ago, 就已经安装好Torch,这里再记录一下.Torch是Facebook开发的用于AI的科学计算框架,可广泛运用于机器学习的很多算法.相比Caffe,其接口运用更加方便,使用 ...
- 【一天一道LeetCode】#94. Binary Tree Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Java Web 高性能开发,第 3 部分: 网站优化实战
这个系列的前两篇,介绍了前端的优化技术,这些技术秉承了前人至高无上的智慧,我只是负责吸收和传播.然而,这些技术一般也都是某某大型网站的技术经验,我们大部分人或许只能接触到相对小规模的网站,小规模的网站 ...
- web多语言url的设计
因为项目要支持国际化,最近跟一个同事在讨论多语言版本下面url如何设计,假如我们需要支持en和cn的版本. 他倾向于支持如下的url格式,后续以格式1指代: /en/group/abc.html /c ...
- Spring揭秘 读书笔记 五 容器的启动
Spring的IoC容器所起的作用,就是生产bean,并维持bean间的依赖关系.它会以某种方式加载Configuration Metadata(通常也就是XML格式的配置信息),然后根据这些信息绑定 ...