论坛首页 Java企业应用论坛

summercool-ddl框架简介 (二)

浏览 9383 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-01-13  

简介:轻量封装Ibatis3

         因为本人在国内最大的电子商务公司工作期间,深感一个好的分表分库框架可以大大提高系统的承载能力及系统的灵活性,而一个不好的分表分库方案,则让系统在大数据量处理的时候非常郁闷。所以, 在根据笔者在从事电子商务开发的这几年中,对各个应用场景而开发的一个轻量封装Ibatis3的一个分表分库框架。

         笔者工作的这几年之中,总结并开发了如下几个框架: summercool(Web 框架,已经应用于某国内大型网络公司的等重要应用)、summercool-hsf(基于Netty实现的RPC框架,已经应用国内某移动互联网公司)、 summercool-ddl(基于Mybaits的分表分库框架,已经应用国内某移动互联网公司);相继缓存方案、和消息系统解决方案也会慢慢开源。 Summercool框架做为笔者的第一个开源框架

 

1.依赖

<dependency>
    <groupId>org.summercool</groupId>
    <artifactId>summercool-ddl</artifactId>
    <version>1.0</version>
</dependency>

 源码svn地址:http://summercool-ddl.googlecode.com/svn/trunk

2.准备Sql映射文件

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ">
<mapper namespace="NS-User">
    <insert id="insertUser" parameterType="org.summercool.mybatis.demo.dao.entity.UserEntity">
        insert into $[user]$(
            id,
            name
        ) values (
            #{id},
            #{name}
        )
    </insert>
</mapper>

 注意此处,对于需要分表或分库的表,使用“$[table name]$”这种方式表示。

3.Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-autowire="byName">
    <bean id="dataSourceMaster" class="com.jolbox.bonecp.BoneCPDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl">
            <value>${demo.datasource.master.jdbcUrl}</value>
        </property>
        <property name="username" value="${demo.datasource.master.username}" />
        <property name="password" value="${demo.datasource.master.password}" />
        <property name="idleConnectionTestPeriodInMinutes" value="5" />
        <property name="idleMaxAgeInMinutes" value="30" />
        <property name="maxConnectionsPerPartition" value="30" />
        <property name="minConnectionsPerPartition" value="5" />
        <property name="partitionCount" value="2" />
        <property name="acquireIncrement" value="5" />
        <property name="statementsCacheSize" value="150" />
        <property name="releaseHelperThreads" value="8" />
        <property name="connectionTestStatement" value="select 1 from dual" />
    </bean>
     
    <bean id="dataSourceSlave" class="com.jolbox.bonecp.BoneCPDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl">
            <value>${demo.datasource.slave.jdbcUrl}</value>
        </property>
        <property name="username" value="${demo.datasource.slave.username}" />
        <property name="password" value="${demo.datasource.slave.password}" />
        <property name="idleConnectionTestPeriodInMinutes" value="5" />
        <property name="idleMaxAgeInMinutes" value="30" />
        <property name="maxConnectionsPerPartition" value="30" />
        <property name="minConnectionsPerPartition" value="5" />
        <property name="partitionCount" value="2" />
        <property name="acquireIncrement" value="5" />
        <property name="statementsCacheSize" value="150" />
        <property name="releaseHelperThreads" value="8" />
        <property name="connectionTestStatement" value="select 1 from dual" />
    </bean>
    <bean id="demoSqlSessionFactory"
        class="org.summercool.mybatis.spring.support.SqlSessionFactoryBean">
        <property name="mainDataSource" ref="dataSourceMaster" />
        <property name="shardDataSourceList">
            <util:list>
                <ref bean="dataSourceSlave" />
            </util:list>
        </property>
        <property name="mapperLocations">
            <array>
                <value>classpath:mybatis/user-mapper.xml</value>
            </array>
        </property>
        <property name="shardStrategy">
            <map>
                <entry key="Shard-User">
                    <value>org.summercool.mybatis.demo.shard.UserShardStrategy</value>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="userDao" class="org.summercool.mybatis.demo.dao.impl.UserDaoImpl">
        <property name="sqlSessionFactory" ref="demoSqlSessionFactory"/>
    </bean>
</beans>
从上面的配置文件中,可以看到,注入了两个DataSource,分别代表两个物理库。

然后再添加类型为org.summercool.mybatis.spring.support.SqlSessionFactoryBean的bean描述,并将两个DataSource关联到该bean的主库和备库(备库可以有多个)字段。

同时,需要设置分表策略字段shardStrategy,该字段的类型是Map,添加分表策略时,Key即为分表策略的名称,Value则为具体的分表策略实现。如上,注册了一个名称为"fr"的分表策略实现类com.gexin.contact.utils.shard.ContactShardStrategy。

4.分表策略的实现

package org.summercool.mybatis.demo.shard;
import java.util.Map;
import javax.sql.DataSource;
import org.summercool.mybatis.ShardParam;
import org.summercool.mybatis.strategy.ShardStrategy;
/**
 * 
 * @author Kolor
 */
public class UserShardStrategy extends ShardStrategy {
    @Override
    public DataSource getTargetDataSource() {
        ShardParam shardParam = getShardParam();
        //
        Long param = (Long) shardParam.getShardValue();
        Map<String, DataSource> map = this.getShardDataSources();
        if (param > 100) {
            return map.get("dataSourceSlave");
        }
        return getMainDataSource();
    }
    @Override
    public String getTargetSql() {
        String targetSql = getSql();
        ShardParam shardParam = getShardParam();
        //
        Long param = (Long) shardParam.getShardValue();
        String tableName = "user_" + (param % 2);
        targetSql = targetSql.replaceAll("\\$\\[user\\]\\$", tableName);
        return targetSql;
    }
}
可以看到这个分表策略继承自org.summercool.mybatis.strategy.ShardStrategy,并实现其两个抽象方法getTargetDataSource和getTargetSql,分别代表获取目标DataSource和获取目标Sql。

目标DataSource通过解析分表参数,按设计好的策略获取。

目标Sql同样通过解析分表参数,生成真正的表名,然后用字符串替换的方式获取真正的Sql。

5.Dao层实现

package org.summercool.mybatis.demo.dao.impl;
import org.summercool.mybatis.ShardParam;
import org.summercool.mybatis.demo.dao.UserDao;
import org.summercool.mybatis.demo.dao.entity.UserEntity;
import org.summercool.mybatis.spring.support.SqlSessionDaoSupport;
/**
 *  
 * @author
 */
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    public boolean insertUser(UserEntity user) {
        ShardParam shardParam = new ShardParam("Shard-User", user.getId(), user);
         
        return getSqlSession().insert("NS-User.insertUser", shardParam) > 0;
    }
}

Dao层实现没有特别的地方,仅有的一点就是,对于需要分表分库的操作,需要传入分表参数(ShardParam)

ShardParam shardParam = new ShardParam("Shard-User", user.getId(), user);

我们来看看ShardParam的定义:

package org.summercool.mybatis;
public class ShardParam {
    public static final ShardParam NO_SHARD = new ShardParam();
    private String name;
    private Object shardValue;
    private Object params;
    public ShardParam() {
    }
    public ShardParam(String name, Object shardValue, Object params) {
        super();
        this.name = name;
        this.shardValue = shardValue;
        this.params = params;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Object getShardValue() {
        return shardValue;
    }
    public void setShardValue(Object shardValue) {
        this.shardValue = shardValue;
    }
    public Object getParams() {
        return params;
    }
    public void setParams(Object params) {
        this.params = params;
    }
}

 构造函数,第一个参数是分表分库策略的名字(即在第二小节中提到的"fr"),第二个参数是分表参数,第三个参数是真正提交到数据库的参数。

6. Spring事务配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-autowire="byName">
     
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/config.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8" />
    </bean>
     
    <bean id="userService" class="org.summercool.mybatis.demo.service.impl.UserServiceImpl"/>
     
    <!-- 事务配置 -->
    <bean name="userTransactionManager"
        class="org.summercool.mybatis.spring.support.MultiDataSourceTransactionManager">
    </bean>
    <tx:advice id="userTxAdvice" transaction-manager="userTransactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    </tx:advice>
     
    <aop:config>
        <!-- UserService事务管理 -->
        <aop:pointcut id="userServiceOperation"
            expression="execution(* org.summercool.mybatis.demo.service..*Service.*(..))" />
        <aop:advisor advice-ref="userTxAdvice" pointcut-ref="userServiceOperation" />
    </aop:config>
</beans>
 目前只支持XA分布式事务。
   发表时间:2013-01-17  
分布式事务用的是jotm还是atomikos?
0 请登录后投票
   发表时间:2013-01-17  
kelloKitty 写道
分布式事务用的是jotm还是atomikos?

就是mysql的xa,我这个事务处理上还是有点和大家想的不是太一样。有时间画个图出来~
0 请登录后投票
   发表时间:2013-01-19  
以前只要有人在论坛提出MyBatis的文章,总有一群只懂crud的Hibernate死忠人士来搅和,为什么最近Hibernate的死忠们集体哑声了。

以前我和别人讨论MyBatis的好处,我说MyBatis可以实现很多灵活的数据库操作,比如动态建表、查询表结构、动态增删字段、复杂多表查询等等。

当我提出这些Mybatis的优点时,那些Hibernate的死忠们总是说Hibernate也能实现这些,但是殊不知Hibernate中实现这些,其实是直接用得jdbc,跟Hibernate本身没有半毛钱的关系。
0 请登录后投票
   发表时间:2013-01-19  
youarestupid 写道
以前只要有人在论坛提出MyBatis的文章,总有一群只懂crud的Hibernate死忠人士来搅和,为什么最近Hibernate的死忠们集体哑声了。

以前我和别人讨论MyBatis的好处,我说MyBatis可以实现很多灵活的数据库操作,比如动态建表、查询表结构、动态增删字段、复杂多表查询等等。

当我提出这些Mybatis的优点时,那些Hibernate的死忠们总是说Hibernate也能实现这些,但是殊不知Hibernate中实现这些,其实是直接用得jdbc,跟Hibernate本身没有半毛钱的关系。


这个也是看个人习惯吧,在Hibernate3.2以后也有类似于ibatis这样的配置SQL和SQLQuery这样的,还有配置的
我个人刚开始就用Hibernate,然后是因为Hibernate实现同一功能的方式太多,所以后来就不用了,控制不了开发人员使用哪种方式,而且有时候写不好效率也不高,写好了效率没有问题。所以,后来不敢用了
0 请登录后投票
   发表时间:2013-02-04  
看到summercool-hsf,  hsf的字眼相当的眼熟啊
0 请登录后投票
   发表时间:2013-02-05  
agapple 写道
看到summercool-hsf,  hsf的字眼相当的眼熟啊

我也熟,呵呵~
0 请登录后投票
   发表时间:2013-03-26  
支持什么类型的sql
0 请登录后投票
   发表时间:2013-03-26  
很想知道,  分库分表之后, 如何做统计操作
0 请登录后投票
   发表时间:2013-03-26  
loveyunwt 写道
支持什么类型的sql

其实就是对SQL的一个目标库和目标表的一个代理,所以不区别SQL类型,都可以
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics