提交 232a8c01 authored 作者: 陈世营's avatar 陈世营

分表分库 多线程性能测试

上级 e6b0d8e1
package com.loit.shardingsphere.seata.nacos.feign.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 1. 当一个任务被提交到线程池时,首先查看线程池的核心线程是否都在执行任务,否就选择一条线程执行任务,是就执行第二步。
* 2. 查看核心线程池是否已满,不满就创建一条线程执行任务,否则执行第三步。
* 3. 查看任务队列是否已满,不满就将任务存储在任务队列中(SynchronousQueue同步队直接执行第四步),否则执行第四步。
* 4. 查看线程池是否已满,不满就创建一条线程执行任务,否则就按照策略处理无法执行的任务。
*/
@Configuration
@EnableAsync
@Slf4j
public class ThreadPoolTaskConfig implements AsyncConfigurer {
//配置类实现AsyncConfigurer接口并重写AsyncConfigurer方法,并返回一个ThreadPoolTaskExecutor
//这样我们就得到了一个基于线程池的TaskExecutor
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程
taskExecutor.setCorePoolSize(5);
//连接池中保留的最大连接数。Default: 15 maxPoolSize
taskExecutor.setMaxPoolSize(200);
//queueCapacity 线程池所使用的缓冲队列
taskExecutor.setQueueCapacity(500);
//除核心线程外的线程存活时间
taskExecutor.setKeepAliveSeconds(10);
//线程名称前缀
taskExecutor.setThreadNamePrefix("thread-sharding-execute");
//设置拒绝策略
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SplcCxtjAsyncExceptionHandler();
}
class SplcCxtjAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
log.error("-------------------------->> thread-sharding-execute-");
log.error("-------------------------->>>> Exception message - " + throwable.getMessage());
log.error("-------------------------->>>> Method name - " + method.getName());
for (Object param : objects) {
log.error("-------------------------->>>> Parameter value - " + String.valueOf(param));
}
}
}
}
......@@ -28,20 +28,32 @@ public class DemoController {
@GetMapping("purchase")
public void purchase() {
for (long i = 1; i < 20; i++) {
long startTime = System.currentTimeMillis(); //获取开始时间
for (long i = 3920570; i <= 10000000; i++) {
try {
businessService.purchase(i);
} catch (Exception e) {
log.info(e.getMessage(), e);
}
}
long endTime = System.currentTimeMillis(); //获取结束时间
log.error("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
log.info("success");
}
@GetMapping("findPage")
public void findPage() {
long startTime = System.currentTimeMillis(); //获取开始时间
List<OrderEntity> pageList = orderService.findPage(0l, 5l);
log.info(pageList.toString());
long endTime = System.currentTimeMillis(); //获取结束时间
log.error("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
log.info("success");
}
......
......@@ -3,11 +3,8 @@ package com.loit.shardingsphere.seata.nacos.feign.modules.service.impl;
import com.loit.shardingsphere.seata.nacos.feign.modules.entity.OrderEntity;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IBusinessService;
import com.loit.shardingsphere.seata.nacos.feign.modules.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -22,7 +19,7 @@ public class BusinessServiceImpl implements IBusinessService {
*
* @param userId
*/
@Override
/* @Override
@ShardingTransactionType(TransactionType.BASE)
@GlobalTransactional
public void purchase(Long userId) {
......@@ -36,26 +33,26 @@ public class BusinessServiceImpl implements IBusinessService {
orderEntity.setUserId(userId.intValue());
orderService.insertOrder(orderEntity);
//throw new RuntimeException("回滚测试");
}
}*/
/**
* 分表 使用本地事务
*
* @param userId
*/
// @Override
// @ShardingTransactionType(TransactionType.LOCAL)
// @Transactional
// public void purchase(Long userId) {
// if (userId == null) {
// return;
// }
// TransactionTypeHolder.set(TransactionType.LOCAL);
// OrderEntity orderEntity = new OrderEntity();
// orderEntity.setOrderId(userId);
// orderEntity.setStatus("seata");
// orderEntity.setUserId(userId.intValue());
// orderService.insertOrder(orderEntity);
// throw new RuntimeException("回滚测试");
// }
@Override
@Transactional
@Async
public void purchase(Long userId) {
if (userId == null) {
return;
}
OrderEntity orderEntity = new OrderEntity();
orderEntity.setOrderId(userId);
orderEntity.setStatus("seata");
orderEntity.setUserId(userId.intValue());
orderService.insertOrder(orderEntity);
//throw new RuntimeException("回滚测试");
}
}
......@@ -18,37 +18,15 @@ spring:
proxy-target-class: true
shardingsphere:
datasource:
names: ds0,ds1
ds0:
names: ds
ds:
type: com.alibaba.druid.pool.DruidDataSource
# type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: loit2019ABC
jdbc-url: jdbc:mysql://192.168.66.40:3321/demo_ds_0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
url: jdbc:mysql://192.168.66.40:3321/demo_ds_0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
ds1:
type: com.alibaba.druid.pool.DruidDataSource
# type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: loit2019ABC
jdbc-url: jdbc:mysql://192.168.66.40:3321/demo_ds_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
url: jdbc:mysql://192.168.66.40:3321/demo_ds_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc-url: jdbc:mysql://192.168.66.40:3321/demo_ds?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
url: jdbc:mysql://192.168.66.40:3321/demo_ds?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
......@@ -64,28 +42,24 @@ spring:
maxOpenPreparedStatements: 20
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
sharding:
default-database-strategy:
inline:
sharding-column: user_id
algorithm-expression: ds$->{user_id % 2}
tables:
t_order:
actual-data-nodes: ds$->{0..1}.t_order_$->{0..1}
actual-data-nodes: ds.t_order_$->{0..9}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_$->{order_id % 2}
algorithm-expression: t_order_$->{order_id % 10}
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker.id: 123
t_order_item:
actual-data-nodes: ds$->{0..1}.t_order_item_$->{0..1}
actual-data-nodes: ds.t_order_item_$->{0..9}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_item_$->{order_id % 2}
algorithm-expression: t_order_item_$->{order_id % 10}
key-generator:
column: order_item_id
type: SNOWFLAKE
......@@ -96,7 +70,7 @@ mybatis-plus:
# datasource: dataSource
mapper-locations: classpath:/mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.loit.shardingsphere.seata.nacos.feign.modules
type-aliases-package: com.loit.shardingsphere.seata
#typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID", 4:"该类型为未设置主键类型", 5:"字符串全局唯一ID";
......
......@@ -74,7 +74,7 @@ mybatis-plus:
# datasource: dataSource
mapper-locations: classpath:/mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.loit.shardingsphere.seata.modules.entity
type-aliases-package: com.loit.shardingsphere.seata
#typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID", 4:"该类型为未设置主键类型", 5:"字符串全局唯一ID";
......
......@@ -44,22 +44,22 @@ spring:
sharding:
tables:
t_order:
actual-data-nodes: ds.t_order_$->{0..1}
actual-data-nodes: ds.t_order_$->{0..9}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_$->{order_id % 2}
algorithm-expression: t_order_$->{order_id % 10}
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker.id: 123
t_order_item:
actual-data-nodes: ds.t_order_item_$->{0..1}
actual-data-nodes: ds.t_order_item_$->{0..9}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_item_$->{order_id % 2}
algorithm-expression: t_order_item_$->{order_id % 10}
key-generator:
column: order_item_id
type: SNOWFLAKE
......@@ -70,7 +70,7 @@ mybatis-plus:
# datasource: dataSource
mapper-locations: classpath:/mapper/**/*.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.loit.shardingsphere.seata.modules.entity
type-aliases-package: com.loit.shardingsphere.seata
#typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID", 4:"该类型为未设置主键类型", 5:"字符串全局唯一ID";
......
......@@ -94,9 +94,9 @@ store {
## mysql/oracle/h2/oceanbase etc.
db-type = "mysql"
driver-class-name = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://39.98.202.173:3306/seata_server"
url = "jdbc:mysql://192.168.66.40:3321/seata_server"
user = "root"
password = "abcd1234A!"
password = "loit2019ABC"
min-conn = 1
max-conn = 10
global.table = "global_table"
......
......@@ -26,7 +26,7 @@
</encoder>
</appender>
<root level="DEBUG">
<root level="ERROR">
<appender-ref ref="seata-default"/>
<appender-ref ref="stdout"/>
</root>
......
/*
Navicat MySQL Data Transfer
Source Server : 39.98.202.173seata
Source Server Version : 50725
Source Host : 39.98.202.173:3306
Source Server : 192.168.66.40.7
Source Server Version : 50722
Source Host : 192.168.66.40:3321
Source Database : demo_ds
Target Server Type : MYSQL
Target Server Version : 50725
Target Server Version : 50722
File Encoding : 65001
Date: 2020-02-13 22:37:45
Date: 2020-02-17 16:51:00
*/
SET FOREIGN_KEY_CHECKS=0;
......@@ -19,90 +19,198 @@ SET FOREIGN_KEY_CHECKS=0;
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE "t_order" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_0
-- ----------------------------
DROP TABLE IF EXISTS `t_order_0`;
CREATE TABLE "t_order_0" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order_0` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_1
-- ----------------------------
DROP TABLE IF EXISTS `t_order_1`;
CREATE TABLE "t_order_1" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order_1` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_10
-- ----------------------------
DROP TABLE IF EXISTS `t_order_10`;
CREATE TABLE `t_order_10` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_2
-- ----------------------------
DROP TABLE IF EXISTS `t_order_2`;
CREATE TABLE `t_order_2` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_3
-- ----------------------------
DROP TABLE IF EXISTS `t_order_3`;
CREATE TABLE `t_order_3` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_4
-- ----------------------------
DROP TABLE IF EXISTS `t_order_4`;
CREATE TABLE `t_order_4` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_5
-- ----------------------------
DROP TABLE IF EXISTS `t_order_5`;
CREATE TABLE `t_order_5` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_6
-- ----------------------------
DROP TABLE IF EXISTS `t_order_6`;
CREATE TABLE `t_order_6` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_7
-- ----------------------------
DROP TABLE IF EXISTS `t_order_7`;
CREATE TABLE `t_order_7` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_8
-- ----------------------------
DROP TABLE IF EXISTS `t_order_8`;
CREATE TABLE `t_order_8` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_9
-- ----------------------------
DROP TABLE IF EXISTS `t_order_9`;
CREATE TABLE `t_order_9` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_item
-- ----------------------------
DROP TABLE IF EXISTS `t_order_item`;
CREATE TABLE "t_order_item" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_item_id" bigint(20) NOT NULL,
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order_item` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_item_id` bigint(20) NOT NULL,
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_item_0
-- ----------------------------
DROP TABLE IF EXISTS `t_order_item_0`;
CREATE TABLE "t_order_item_0" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_item_id" bigint(20) NOT NULL,
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order_item_0` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_item_id` bigint(20) NOT NULL,
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order_item_1
-- ----------------------------
DROP TABLE IF EXISTS `t_order_item_1`;
CREATE TABLE "t_order_item_1" (
"id" varchar(64) NOT NULL COMMENT '主键',
"order_item_id" bigint(20) NOT NULL,
"order_id" bigint(20) NOT NULL,
"user_id" int(11) NOT NULL,
"status" varchar(50) DEFAULT NULL,
PRIMARY KEY ("id")
CREATE TABLE `t_order_item_1` (
`id` varchar(64) NOT NULL COMMENT '主键',
`order_item_id` bigint(20) NOT NULL,
`order_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE "undo_log" (
"id" bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
"branch_id" bigint(20) NOT NULL COMMENT 'branch transaction id',
"xid" varchar(100) NOT NULL COMMENT 'global transaction id',
"context" varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
"rollback_info" longblob NOT NULL COMMENT 'rollback info',
"log_status" int(11) NOT NULL COMMENT '0:normal status,1:defense status',
"log_created" datetime NOT NULL COMMENT 'create datetime',
"log_modified" datetime NOT NULL COMMENT 'modify datetime',
PRIMARY KEY ("id"),
UNIQUE KEY "ux_undo_log" ("xid","branch_id") USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=306 DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` bigint(20) NOT NULL COMMENT 'branch transaction id',
`xid` varchar(100) NOT NULL COMMENT 'global transaction id',
`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` longblob NOT NULL COMMENT 'rollback info',
`log_status` int(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` datetime NOT NULL COMMENT 'create datetime',
`log_modified` datetime NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论