一、前言
下载ppt版本完整demo演示内容
功能介绍 @b@ 多租户@b@ 读写分离@b@ 负载均衡 @b@ 只是用主数据源@b@ 轮询策略@b@ 权重策略@b@ 全局序列号 @b@ 全局表@b@ 分库@b@ 分表@b@ 分布式事务 @b@ 数据库容 @b@ SQL限流 @b@ SQL监控
二、示例脚本
-- ----------------------------@b@-- Table structure for t_item_0@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_0";@b@CREATE TABLE "public"."t_item_0" (@b@ "item_id" numeric NOT NULL,@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_0" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_0@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_0" VALUES ('7', '27', '0', 'status-970843768');@b@INSERT INTO "public"."t_item_0" VALUES ('10', '30', '0', 'status-591580969');@b@INSERT INTO "public"."t_item_0" VALUES ('13', '33', '0', 'status-1192476597');@b@INSERT INTO "public"."t_item_0" VALUES ('16', '36', '0', 'status-130997777');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_item_1@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_1";@b@CREATE TABLE "public"."t_item_1" (@b@ "item_id" numeric NOT NULL,@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_1" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_1@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_1" VALUES ('8', '28', '0', 'status1281627962');@b@INSERT INTO "public"."t_item_1" VALUES ('11', '31', '0', 'status-1874881577');@b@INSERT INTO "public"."t_item_1" VALUES ('14', '34', '0', 'status1467289651');@b@INSERT INTO "public"."t_item_1" VALUES ('17', '37', '0', 'status1664561888');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_item_2@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_2";@b@CREATE TABLE "public"."t_item_2" (@b@ "item_id" numeric NOT NULL,@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_2" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_2@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_2" VALUES ('2', '23', '1', 'status-2058927716');@b@INSERT INTO "public"."t_item_2" VALUES ('9', '29', '0', 'status1484409515');@b@INSERT INTO "public"."t_item_2" VALUES ('12', '32', '0', 'status427320392');@b@INSERT INTO "public"."t_item_2" VALUES ('15', '35', '0', 'status-2033859517');@b@INSERT INTO "public"."t_item_2" VALUES ('18', '38', '0', 'status-1876869668');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_item_ext_0@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_ext_0";@b@CREATE TABLE "public"."t_item_ext_0" (@b@ "ext_id" numeric NOT NULL,@b@ "item_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_ext_0" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_ext_0@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_ext_0" VALUES ('4', '7', 'status-2043491080');@b@INSERT INTO "public"."t_item_ext_0" VALUES ('7', '10', 'status529543919');@b@INSERT INTO "public"."t_item_ext_0" VALUES ('10', '13', 'status-983731981');@b@INSERT INTO "public"."t_item_ext_0" VALUES ('13', '16', 'status-245452489');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_item_ext_1@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_ext_1";@b@CREATE TABLE "public"."t_item_ext_1" (@b@ "ext_id" numeric NOT NULL,@b@ "item_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_ext_1" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_ext_1@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_ext_1" VALUES ('5', '8', 'status350778158');@b@INSERT INTO "public"."t_item_ext_1" VALUES ('8', '11', 'status-284886720');@b@INSERT INTO "public"."t_item_ext_1" VALUES ('11', '14', 'status-1201482568');@b@INSERT INTO "public"."t_item_ext_1" VALUES ('14', '17', 'status1894720321');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_item_ext_2@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_item_ext_2";@b@CREATE TABLE "public"."t_item_ext_2" (@b@ "ext_id" numeric NOT NULL,@b@ "item_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_item_ext_2" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_item_ext_2@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_item_ext_2" VALUES ('3', '2', 'status104420805');@b@INSERT INTO "public"."t_item_ext_2" VALUES ('6', '9', 'status1136320205');@b@INSERT INTO "public"."t_item_ext_2" VALUES ('9', '12', 'status-1718334354');@b@INSERT INTO "public"."t_item_ext_2" VALUES ('12', '15', 'status-733558140');@b@INSERT INTO "public"."t_item_ext_2" VALUES ('15', '18', 'status733951758');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_order_0@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_order_0";@b@CREATE TABLE "public"."t_order_0" (@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_order_0" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_order_0@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_order_0" VALUES ('12', '1', 'new');@b@INSERT INTO "public"."t_order_0" VALUES ('18', '1', 'new');@b@INSERT INTO "public"."t_order_0" VALUES ('21', '1', 'new');@b@INSERT INTO "public"."t_order_0" VALUES ('27', '0', 'status1955136834');@b@INSERT INTO "public"."t_order_0" VALUES ('30', '0', 'status-1869897438');@b@INSERT INTO "public"."t_order_0" VALUES ('33', '0', 'status-1648215998');@b@INSERT INTO "public"."t_order_0" VALUES ('36', '0', 'status-2116995573');@b@INSERT INTO "public"."t_order_0" VALUES ('39', '0', 'status-196322594');@b@INSERT INTO "public"."t_order_0" VALUES ('42', '0', 'status-901995625');@b@INSERT INTO "public"."t_order_0" VALUES ('45', '0', 'status752861562');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_order_1@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_order_1";@b@CREATE TABLE "public"."t_order_1" (@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_order_1" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_order_1@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_order_1" VALUES ('13', '1', 'new');@b@INSERT INTO "public"."t_order_1" VALUES ('16', '1', 'new');@b@INSERT INTO "public"."t_order_1" VALUES ('28', '0', 'status538739525');@b@INSERT INTO "public"."t_order_1" VALUES ('31', '0', 'status-721556298');@b@INSERT INTO "public"."t_order_1" VALUES ('34', '0', 'status-1332951579');@b@INSERT INTO "public"."t_order_1" VALUES ('37', '0', 'status-1541211926');@b@INSERT INTO "public"."t_order_1" VALUES ('40', '0', 'status-252273631');@b@INSERT INTO "public"."t_order_1" VALUES ('43', '0', 'status815886354');@b@INSERT INTO "public"."t_order_1" VALUES ('46', '0', 'status549822887');@b@INSERT INTO "public"."t_order_1" VALUES ('19', '3', 'new');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Table structure for t_order_2@b@-- ----------------------------@b@DROP TABLE IF EXISTS "public"."t_order_2";@b@CREATE TABLE "public"."t_order_2" (@b@ "order_id" numeric NOT NULL,@b@ "user_id" numeric NOT NULL,@b@ "status" varchar(50) COLLATE "default"@b@)@b@WITH (OIDS=FALSE);@b@ALTER TABLE "public"."t_order_2" OWNER TO "postgreuser";@b@@b@-- ----------------------------@b@-- Records of t_order_2@b@-- ----------------------------@b@BEGIN;@b@INSERT INTO "public"."t_order_2" VALUES ('14', '1', 'new');@b@INSERT INTO "public"."t_order_2" VALUES ('17', '1', 'new');@b@INSERT INTO "public"."t_order_2" VALUES ('20', '1', 'new');@b@INSERT INTO "public"."t_order_2" VALUES ('23', '1', 'status-1064495912');@b@INSERT INTO "public"."t_order_2" VALUES ('29', '0', 'status1954627296');@b@INSERT INTO "public"."t_order_2" VALUES ('32', '0', 'status670257300');@b@INSERT INTO "public"."t_order_2" VALUES ('35', '0', 'status150627316');@b@INSERT INTO "public"."t_order_2" VALUES ('38', '0', 'status-111931557');@b@INSERT INTO "public"."t_order_2" VALUES ('41', '0', 'status-1026098682');@b@INSERT INTO "public"."t_order_2" VALUES ('44', '0', 'status1565284214');@b@INSERT INTO "public"."t_order_2" VALUES ('47', '0', 'status-916851753');@b@COMMIT;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_0@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_0" ADD PRIMARY KEY ("item_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_1@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_1" ADD PRIMARY KEY ("item_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_2@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_2" ADD PRIMARY KEY ("item_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_ext_0@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_ext_0" ADD PRIMARY KEY ("ext_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_ext_1@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_ext_1" ADD PRIMARY KEY ("ext_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_item_ext_2@b@-- ----------------------------@b@ALTER TABLE "public"."t_item_ext_2" ADD PRIMARY KEY ("ext_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_order_0@b@-- ----------------------------@b@ALTER TABLE "public"."t_order_0" ADD PRIMARY KEY ("order_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_order_1@b@-- ----------------------------@b@ALTER TABLE "public"."t_order_1" ADD PRIMARY KEY ("order_id") NOT DEFERRABLE INITIALLY IMMEDIATE;@b@@b@-- ----------------------------@b@-- Primary key structure for table t_order_2@b@-- ----------------------------@b@ALTER TABLE "public"."t_order_2" ADD PRIMARY KEY ("order_id") NOT DEFERRABLE INITIALLY IMMEDIATE;
三、示例代码配置
路由数据和表配置 :定义分片数据源pddl:data-source-partition、定义全局表pddl:global-table、定义逻辑表pddl:logic-table、定义逻辑子表pddl:logic-child-table
<pddl:data-source id="shardingDataSource" database-type="PostgreSQL" >@b@ <pddl:data-source-partitions>@b@ <pddl:data-source-partition name="p0" read-strategy="weight">@b@ <pddl:master-data-source ref="ds0"/>@b@ <pddl:slave-data-source ref="ds01" weight="200"/>@b@ <pddl:slave-data-source ref="ds02" weight="400"/>@b@ </pddl:data-source-partition>@b@ <pddl:data-source-partition name="p1" read-strategy="cycle">@b@ <pddl:master-data-source ref="ds1" />@b@ <pddl:slave-data-source ref="ds11"/>@b@ <pddl:slave-data-source ref="ds12"/>@b@ </pddl:data-source-partition>@b@ <pddl:data-source-partition name="p2" read-strategy="weight-x">@b@ <pddl:master-data-source ref="ds2" weight ="200"/>@b@ <pddl:slave-data-source ref="ds21" weight="200" />@b@ <pddl:slave-data-source ref="ds22" weight="400" />@b@ </pddl:data-source-partition>@b@ </pddl:data-source-partitions>@b@ <pddl:tables>@b@ <pddl:global-table name="stock"/>@b@ <pddl:logic-table name="t_order" @b@ primary-key="order_id" @b@ table-postfixes="_0,_1,_2" @b@ database-strategy="orderDatabaseStrategy" @b@ table-strategy="orderTableStrategy">@b@ <pddl:logic-child-table name="t_item" @b@ primary-key="item_id" @b@ foreign-key="order_id"/>@b@ </pddl:logic-table>@b@ </pddl:tables>@b@</pddl:data-source>
路由规则配置 :定义分库策略pddl:strategy、定义分表策略pddl:strategy
<pddl:strategy id="orderDatabaseStrategy" @b@ sharding-columns="user_id" @b@ expression="DB${user_id.intValue() % 3+1}"/>@b@@b@<pddl:strategy id="orderTableStrategy" @b@ sharding-columns="order_id" @b@ expression="_${order_id.intValue() % 3}"/>@b@?全局序列号配置@b@<bean id="sequence" class="io.pddl.sequence.support.PGPartitionSequenceGenerator">@b@ <property name="incrStep" value="1000"/>@b@ <property name="dataSource" ref="ds0"/>@b@</bean>
全局序列号使用
@Service@b@public class DemoService{@b@@b@ @AutoWire@b@ private SequenceGenerator sequence;@b@@b@ public void test(){@b@ long id= sequence.nextval("t_order");@b@ ....@b@ }@b@}
多租户使用 - 在业务代码中设置HintContext上下文指定使用的分片数据源名称即可
HintContextHolder.setHintContext(new HintContext(){@b@ @Override@b@ public String getPartitionDBName() { return ”p2”; }@b@});
基本测试用例
@Service@b@public class DemoJdbcTest{@b@@b@ @Resource(name="shardingDataSource")@b@ private DataSource shardingDataSource;@b@@b@ @AutoWire@b@ private SequenceGenerator sequence;@b@@b@ public void testSql(){@b@ String sql= "select user_id,order_id,status from t_order where order_id in (1,2,3) and user_id=0";@b@ Connection conn= shardingDataSource.getConnection();@b@ Statement st= conn.createStatement();@b@ ResultSet rs= st.executeQuery(sql);@b@ while(rs.next()){@b@ System.out.println("user_id="+rs.getLong("user_id")+",order_id="+rs.getLong("order_id")+",status="+rs.getString(3));@b@ }@b@ rs.close();@b@ st.close();@b@ conn.close();@b@ }@b@@b@ public void testInsertSql(){@b@ String sql= "insert into t_order(user_id,order_id,status) values(?,?,?)";@b@ Connection conn= shardingDataSource.getConnection();@b@ Statement st= conn.preparedStatment(sql);@b@ st.setLong(1,0L);@b@ st.setLong(2,sequence.nextval("t_order"));@b@ st.setString(3,"hello");@b@ int result= st.executeUpdate();@b@ st.close();@b@ conn.close();@b@ }@b@@b@ public void testERSql(){@b@ String sql= "select o.user_id, 0.order_id,i.item_id from t_order o,t_item i where o.user_id=0 and i.order_id= o.order_id and i.item_id=23";@b@ Connection conn= shardingDataSource.getConnection();@b@ Statement st= conn.createStatement();@b@ ResultSet rs= st.executeQuery(sql);@b@ while(rs.next()){@b@ System.out.println("user_id="+rs.getLong("user_id")+",order_id="+rs.getLong("order_id")+",item_id="+rs.getLong("item_id"));@b@ }@b@ rs.close();@b@ st.close();@b@ conn.close();@b@ }@b@}