使用shardingsphere进行分库分表操作

shardingsphere官网:https://shardingsphere.apache.org 详情可以去官网进行查看

Sharding-JDBC介绍:

作为ShardingSphere的第一个产品和前身,Sharding-JDBC定义自己为轻量级Java框架,可在Java JDBC层提供额外的服务。客户端直接连接到数据库时,它以jar形式提供服务,并且不需要额外的部署和依赖性。它可以被认为是增强的JDBC驱动程序,它与JDBC和各种ORM框架完全兼容。

(1) 适用于任何基于JDBC的ORM框架,例如JPA,Hibernate,Mybatis,Spring JDBC模板或直接使用JDBC。
(2) 支持任何第三方数据库连接池,例如DBCP,C3P0,BoneCP,Druid,HikariCP。
(3) 支持任何种类的JDBC标准数据库:MySQL,Oracle,SQLServer,PostgreSQL和任何遵循SQL92的数据库。

开始使用shardingsphere Sharding-JDBC进行分库分表操作

1.创建数据库与表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `t_order_0`;
CREATE TABLE `t_order_0`  (
  `order_id` bigint(20) NOT NULL,
  `user_id` int(11) NOT NULL,
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`order_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `t_order_1`;
CREATE TABLE `t_order_1`  (
  `order_id` bigint(20) NOT NULL,
  `user_id` int(11) NOT NULL,
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`order_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;


SET FOREIGN_KEY_CHECKS = 1;

2.搭建spring Maven项目,引入jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dxy.shardingsphere</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <!-- ShardingJDBC核心包 -->
    <dependency>
    	<groupId>org.apache.shardingsphere</groupId>
    	<artifactId>sharding-jdbc-core</artifactId>
    	<version>4.0.0-RC1</version>
    </dependency>
    <!--SharingJDBC与Spring整合组件  -->
    <dependency>
    	<groupId>org.apache.shardingsphere</groupId>
    	<artifactId>sharding-jdbc-spring-namespace</artifactId>
    	<version>4.0.0-RC1</version>
    </dependency>
    <!-- spring 核心包 -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>5.1.6.RELEASE</version>
    </dependency>
    <!-- 数据库 JDBC驱动-->
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.47</version>
    </dependency>
    <!-- 连接池使用最快的HikariCP  -->
    <dependency>
    	<groupId>com.zaxxer</groupId>
    	<artifactId>HikariCP</artifactId>
    	<version>3.2.0</version>
    </dependency>
    <dependency>  
         <groupId>junit</groupId>  
         <artifactId>junit</artifactId>  
         <version>4.9</version>  
         <scope>test</scope>  
    </dependency>  
    <dependency>  
         <groupId>org.springframework</groupId>  
         <artifactId>spring-test</artifactId>  
         <version> 3.2.4.RELEASE</version>  
         <scope>provided</scope>  
     </dependency> 
	  <!-- @Resouce 依赖jar包-->
	<dependency>
	    <groupId>javax.annotation</groupId>
	    <artifactId>javax.annotation-api</artifactId>
	    <version>1.3.2</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version> 3.2.4.RELEASE  </version>  
        <scope>provided</scope>  
	</dependency>
  </dependencies>
</project>

3.Spring命名空间配置

新建 applicationContext.xml 并进行配置

内联表达式标识符可以使用 . . . 或 {...}或 ...->{…},但是前者与Spring属性文件中的占位符冲突,因此建议$->{…}在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:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd">

     <bean id="dxy_test_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dxy_test_0?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
     </bean>
    
     <bean id="dxy_test_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dxy_test_1?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    
    <!--数据库分库策略 -->
    <sharding:inline-strategy  id="databaseStrategy" sharding-column="user_id" algorithm-expression="dxy_test_$->{user_id % 2}"/>
    <!-- 数据表分表策略 -->
    <sharding:inline-strategy  id="orderTableStrategy"  sharding-column="order_id" algorithm-expression="t_order_$->{order_id % 2}"/>
    <!-- 主键生成策略 -->
    <sharding:key-generator id="orderKeyGenerator" type="SNOWFLAKE" column="order_id"/>
       
    <!-- sharding JDBC数据源 -->
    <sharding:data-source id="shardingDataSource">
    	<sharding:sharding-rule data-source-names="dxy_test_0,dxy_test_1">
	    	 <sharding:table-rules>
	    		<!--logic-table:表规则   database-strategy-ref:数据库分库规则-->
	    		 <sharding:table-rule logic-table="t_order" actual-data-nodes="dxy_test_$->{0..1}.t_order_$->{0..1}" 
	    		 	database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" 
	    		 	key-generator-ref="orderKeyGenerator"/>
    		</sharding:table-rules>
    	</sharding:sharding-rule>
    </sharding:data-source>
   
</beans>

4.进行测试

package com.dxy.shardingsphere.test;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SharingTest {
	
	//数据源
	@Resource(name="shardingDataSource")
	private DataSource dataSource;
	
	
	@Test
	public void testInsert(){
		//得到数据库连接
		try {
			Connection connection= dataSource.getConnection();
			PreparedStatement preparedStatement=connection.prepareStatement("insert into t_order(`order_id`,`user_id`,`status`) values(5,3,'N')");
			preparedStatement.executeUpdate();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void testQuery(){
		//得到数据库连接
		try {
			Connection connection= dataSource.getConnection();
			PreparedStatement preparedStatement=connection.prepareStatement("select * from t_order");
			ResultSet resultSet= preparedStatement.executeQuery();
			while(resultSet.next()){
				System.out.println("order_id:"+resultSet.getLong("order_id")+"user_id:"+resultSet.getInt("user_id"));
			}
			resultSet.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 分库分表后,不要使用自动生成的编号,要选择分布式主键生成策略
	 * @throws SQLException 
	 */
	@Test
	public void tesyBatchInsert() throws SQLException{
		Connection connection=dataSource.getConnection();
		for (int i = 0; i <=20; i++) {
			PreparedStatement preparedStatement=connection.prepareStatement("insert into t_order(`user_id`,`status`) values("+i+",'N')");
			preparedStatement.executeUpdate();
		}
		connection.close();
	}

}

代码可以正常新增和插入数据,并进行了分库分表操作,Sharding-JDBC分库分表 测试成功!

你可能感兴趣的:(数据库,shardingsphere)