zoukankan      html  css  js  c++  java
  • 161220、使用Spring AOP实现MySQL数据库读写分离案例分析

    一、前言

    分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量。

    在进行数据库读写分离的时候,我们首先要进行数据库的主从配置,最简单的是一台Master和一台Slave(大型网站系统的话,当然会很复杂,这里只是分析了最简单的情况)。通过主从配置主从数据库保持了相同的数据,我们在进行读操作的时候访问从数据库Slave,在进行写操作的时候访问主数据库Master。这样的话就减轻了一台服务器的压力。

    在进行读写分离案例分析的时候。首先,配置数据库的主从复制,下边是两种方法(任选其一即可):

    1、MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解

    2、使用mysqlreplicate命令快速搭建 Mysql 主从复制

    当然,只是简单的为了看一下如何用代码的方式实现数据库的读写分离,完全不必要去配置主从数据库,只需要两台安装了 相同数据库的机器就可以了。

    二、实现读写分离的两种方法

    具体到开发中,实现读写分离常用的有两种方式:

    1、第一种方式是我们最常用的方式,就是定义2个数据库连接,一个是MasterDataSource,另一个是SlaveDataSource。更新数据时我们读取MasterDataSource,查询数据时我们读取SlaveDataSource。这种方式很简单,我就不赘述了。

    2、第二种方式动态数据源切换,就是在程序运行时,把数据源动态织入到程序中,从而选择读取主库还是从库。主要使用的技术是:Annotation,Spring AOP ,反射。

    下面会详细的介绍实现方式。

    三、Aop实现主从数据库的读写分离案例

    1、项目代码地址

    目前该Demo的项目地址在开源中国 码云 上边:http://git.oschina.net/xuliugen/aop-choose-db-demo

    或者CSDN免费下载:

    http://download.csdn.net/detail/u010870518/9724872

    2、项目结构

    上图中,除了标记的代码,其他的主要是配置代码和业务代码。

    3、具体分析

    该项目是SSM框架的一个demo,Spring、Spring MVC和MyBatis,具体的配置文件不在过多介绍。

    (1)UserContoller模拟读写数据

    模拟读写数据,调用IUserService 。

    (2)spring-db.xml读写数据源配置

    <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"      xsi:schemaLocation="http://www.springframework.org/schema/beans                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">    <beanid="statFilter" class="com.alibaba.druid.filter.stat.StatFilter" lazy-init="true">        <property name="logSlowSql" value="true"/>        <property name="mergeSql"value="true"/>    </bean>    <!-- 数据库连接 -->    <bean id="readDataSource"class="com.alibaba.druid.pool.DruidDataSource"          destroy-method="close" init-method="init" lazy-init="true">        <property name="driverClassName"value="${driver}"/>        <property name="url" value="${url1}"/>        <propertyname="username" value="root"/>        <property name="password"value="${password}"/>         <!-- 省略部分内容 -->    </bean>    <beanid="writeDataSource" class="com.alibaba.druid.pool.DruidDataSource"          destroy-method="close" init-method="init" lazy-init="true">        <propertyname="driverClassName" value="${driver}"/>        <property name="url"value="${url}"/>        <property name="username" value="root"/>        <propertyname="password" value="${password}"/>        <!-- 省略部分内容 -->    </bean>    <!-- 配置动态分配的读写 数据源 -->    <bean id="dataSource"class="com.xuliugen.choosedb.demo.aspect.ChooseDataSource" lazy-init="true">        <property name="targetDataSources">            <map key-type="java.lang.String"value-type="javax.sql.DataSource">                <!-- write -->                <entrykey="write" value-ref="writeDataSource"/>                <!-- read -->                <entrykey="read" value-ref="readDataSource"/>            </map>        </property>        <property name="defaultTargetDataSource" ref="writeDataSource"/>        <property name="methodType">            <map key-type="java.lang.String">                <!-- read -->                <entry key="read" value=",get,select,count,list,query"/>                <!-- write -->                <entry key="write"value=",add,create,update,delete,remove,"/>            </map>        </property>    </bean></beans>

     

     

    上述配置中,配置了readDataSource和writeDataSource两个数据源,但是交给SqlSessionFactoryBean进行管理的只有dataSource,其中使用到了:com.xuliugen.choosedb.demo.aspect.ChooseDataSource 这个是进行数据库选择的。


    (3)ChooseDataSource


    (4)DataSourceAspect进行具体方法的AOP拦截


    (5)DataSourceHandler,数据源的Handler类



  • 相关阅读:
    背水一战 Windows 10 (90)
    背水一战 Windows 10 (89)
    背水一战 Windows 10 (88)
    背水一战 Windows 10 (87)
    背水一战 Windows 10 (86)
    背水一战 Windows 10 (85)
    背水一战 Windows 10 (84)
    背水一战 Windows 10 (83)
    背水一战 Windows 10 (82)
    背水一战 Windows 10 (81)
  • 原文地址:https://www.cnblogs.com/zrbfree/p/6235740.html
Copyright © 2011-2022 走看看