Skip to content

Commit 5d7ca45

Browse files
authored
Merge pull request 2227324689#153 from 2227324689/stu_dev_0809
Stu dev 0809
2 parents ff47105 + 7c1270e commit 5d7ca45

30 files changed

Lines changed: 761 additions & 31 deletions

File tree

db_scrpit/gpmall.sql

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ INSERT INTO `tb_order` VALUES ('19081913521928018', '1998.00', null, null, '0',
333333
DROP TABLE IF EXISTS `tb_order_item`;
334334
CREATE TABLE `tb_order_item` (
335335
`id` varchar(50) COLLATE utf8_bin NOT NULL,
336-
`item_id` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '商品id',
336+
`item_id` bigint(20) COLLATE utf8_bin NOT NULL COMMENT '商品id',
337337
`order_id` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '订单id',
338338
`num` int(10) DEFAULT NULL COMMENT '商品购买数量',
339339
`title` varchar(200) COLLATE utf8_bin DEFAULT NULL COMMENT '商品标题',
@@ -344,8 +344,12 @@ CREATE TABLE `tb_order_item` (
344344
KEY `item_id` (`item_id`),
345345
KEY `order_id` (`order_id`)
346346
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
347-
347+
alter table tb_order_item add column `status` int(4) DEFAULT NULL COMMENT '1库存已锁定 2库存已释放 3-库存减扣成功';
348+
alter table tb_order_item add column `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
349+
alter table tb_order_item add column `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';
350+
alter table tb_order_item add UNIQUE KEY `oder_item_id` (`order_id`,`item_id`) USING BTREE COMMENT '订单商品唯一索引';
348351
-- ----------------------------
352+
349353
-- Records of tb_order_item
350354
-- ----------------------------
351355
INSERT INTO `tb_order_item` VALUES ('19081913521949774', '100053202', '19081913521928018', '2', '地平线 8 号商务旅行箱', '999.00', '1998.00', 'https://resource.smartisan.com/resource/d1dcca9144e8d13ffb33026148599d0a.png');
@@ -590,3 +594,12 @@ CREATE TABLE `tb_user_verify` (
590594
alter table
591595
tb_member add COLUMN isverified varchar(26) DEFAULT 'N';
592596

597+
DROP TABLE IF EXISTS `tb_stock`;
598+
CREATE TABLE `tb_stock` (
599+
`item_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '商品id',
600+
`stock_count` bigint(20) NOT NULL DEFAULT '0' COMMENT '库存数量',
601+
`lock_count` int(11) NOT NULL DEFAULT '0' COMMENT '冻结库存数量',
602+
`restrict_count` int(3) DEFAULT '5' COMMENT '限购数量',
603+
`sell_id` int(6) DEFAULT NULL COMMENT '售卖id',
604+
PRIMARY KEY (`item_id`)
605+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='库存表';

gpmall-commons/commons-lock/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<groupId>com.gpmall.commons</groupId>
13-
<artifactId>common-lock</artifactId>
13+
<artifactId>commons-lock</artifactId>
1414
<version>1.0-SNAPSHOT</version>
1515

1616
<dependencies>

gpmall-commons/commons-mq/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<parent>
7+
<groupId>com.gpmall.parent</groupId>
8+
<artifactId>gpmall-parent</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<groupId>com.gpmall.commons</groupId>
14+
<artifactId>commons-mq</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<name>commons-mq</name>
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
<optional>true</optional>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-amqp</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>4.11</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<configuration>
51+
<source>1.8</source>
52+
<target>1.8</target>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.gpmall.commons.config;
2+
3+
4+
import org.springframework.amqp.core.Binding;
5+
import org.springframework.amqp.core.BindingBuilder;
6+
import org.springframework.amqp.core.FanoutExchange;
7+
import org.springframework.amqp.core.Queue;
8+
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
9+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
10+
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* @Description: 延迟队列设置
19+
* @Author: wz
20+
* @Date: 2019-09-17 23:17
21+
**/
22+
@Configuration
23+
public class RabbitMqConfig {
24+
25+
public static final String DELAY_EXCHANGE="delay_exchange";
26+
27+
public static final String DELAY_QUEUE="delay_queue";
28+
29+
//延时队列
30+
@Bean
31+
public Queue delayQueue(){
32+
return new Queue(DELAY_QUEUE,true);
33+
}
34+
//延时交换机
35+
@Bean
36+
public FanoutExchange delayExchange(){
37+
Map<String,Object> args=new HashMap<>();
38+
args.put("x-delayed-type","direct");
39+
FanoutExchange topic=new FanoutExchange(DELAY_EXCHANGE,true,false,args);
40+
topic.setDelayed(true);
41+
return topic;
42+
}
43+
44+
//绑定延时队列与交换机
45+
@Bean
46+
public Binding delayBind(){
47+
return BindingBuilder.bind(delayQueue()).to(delayExchange());
48+
}
49+
50+
//消息转换器
51+
Jackson2JsonMessageConverter jsonMessageConverter(){
52+
return new Jackson2JsonMessageConverter();
53+
}
54+
55+
//消息发送器
56+
@Bean
57+
RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory){
58+
final RabbitTemplate rabbitTemplate=new RabbitTemplate(connectionFactory);
59+
rabbitTemplate.setMessageConverter(jsonMessageConverter());
60+
return rabbitTemplate;
61+
}
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.gpmall.commons.producer;
2+
3+
import com.gpmall.commons.config.RabbitMqConfig;
4+
import org.springframework.amqp.core.MessageDeliveryMode;
5+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* @Description:
11+
* @Author: wz
12+
* @Date: 2019-09-18 00:52
13+
**/
14+
@Component
15+
public class RabbitMessageProducer {
16+
@Autowired
17+
RabbitTemplate rabbitTemplate;
18+
19+
public void send(String context) {
20+
//将订单发送到rabbitmq
21+
rabbitTemplate.convertAndSend(RabbitMqConfig.DELAY_EXCHANGE, context, message -> {
22+
message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
23+
message.getMessageProperties().setDelay(15 * 60 * 1000);//毫秒为单位
24+
return message;
25+
});
26+
}
27+
}

gpmall-commons/commons-tool/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@
5757
<groupId>org.apache.curator</groupId>
5858
<artifactId>curator-recipes</artifactId>
5959
</dependency>
60+
<dependency>
61+
<groupId>org.apache.rocketmq</groupId>
62+
<artifactId>rocketmq-common</artifactId>
63+
<version>4.3.0</version>
64+
<exclusions>
65+
<exclusion>
66+
<groupId>io.netty</groupId>
67+
<artifactId>netty-tcnative</artifactId>
68+
</exclusion>
69+
</exclusions>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.apache.rocketmq</groupId>
74+
<artifactId>rocketmq-client</artifactId>
75+
<version>4.3.0</version>
76+
</dependency>
6077
<dependency>
6178
<groupId>javax.mail</groupId>
6279
<artifactId>javax.mail-api</artifactId>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
package com.gpmall.commons.tool.rocketmq;
3+
4+
import lombok.Value;
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.apache.rocketmq.client.exception.MQClientException;
7+
import org.apache.rocketmq.client.producer.DefaultMQProducer;
8+
import org.apache.rocketmq.client.producer.MQProducer;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
*/
15+
/**
16+
*
17+
*//*
18+
19+
@Configuration
20+
public class ProducerConfiguration {
21+
22+
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerConfiguration.class);
23+
24+
private final String PRODUCER_GROUP = "quartz-job-producer";
25+
private final String ROCKETMQ_NAMESERVER_ADDRESS = "127.0.0.1:9876";
26+
27+
*/
28+
/**
29+
* 发送同一类消息的设置为同一个group,保证唯一,默认不需要设置,rocketmq会使用ip@pid(pid代表jvm名字)作为唯一标示
30+
*//*
31+
32+
@Value("${rocketmq.producer.groupName}")
33+
private String groupName;
34+
35+
@Value("${rocketmq.producer.namesrvAddr}")
36+
private String namesrvAddr;
37+
*/
38+
/**
39+
* 消息最大大小,默认4M
40+
*//*
41+
42+
@Value("${rocketmq.producer.maxMessageSize}")
43+
private Integer maxMessageSize ;
44+
*/
45+
/**
46+
* 消息发送超时时间,默认3秒
47+
*//*
48+
49+
@Value("${rocketmq.producer.sendMsgTimeout}")
50+
private Integer sendMsgTimeout;
51+
*/
52+
/**
53+
* 消息发送失败重试次数,默认2次
54+
*//*
55+
56+
@Value("${rocketmq.producer.retryTimesWhenSendFailed}")
57+
private Integer retryTimesWhenSendFailed;
58+
59+
@Bean
60+
public MQProducer mqProducer(){
61+
if (LOGGER.isInfoEnabled()) {
62+
LOGGER.info("mqProducer init");
63+
}
64+
65+
String producerGroup;
66+
String rocketMqNameServerAddress;
67+
if (StringUtils.isNotEmpty(groupName)) {
68+
producerGroup = this.groupName;
69+
} else {
70+
producerGroup = PRODUCER_GROUP;
71+
if (LOGGER.isInfoEnabled()) {
72+
LOGGER.error("当前MQProducer生产者,没有配置producer.group属性,系统默认使用初始值。");
73+
}
74+
}
75+
76+
if (StringUtils.isNotEmpty(namesrvAddr)) {
77+
rocketMqNameServerAddress = this.namesrvAddr;
78+
} else {
79+
rocketMqNameServerAddress = ROCKETMQ_NAMESERVER_ADDRESS;
80+
if (LOGGER.isInfoEnabled()) {
81+
LOGGER.error("当前MQProducer生产者,没有配置rocketmq.nameserver.address属性,系统默认使用初始值。");
82+
}
83+
}
84+
85+
DefaultMQProducer producer;
86+
producer = new DefaultMQProducer(producerGroup);
87+
88+
producer.setNamesrvAddr(rocketMqNameServerAddress);
89+
producer.setCreateTopicKey("AUTO_CREATE_TOPIC_KEY");
90+
91+
if(this.maxMessageSize != null){
92+
producer.setMaxMessageSize(this.maxMessageSize);
93+
}
94+
if(this.sendMsgTimeout != null){
95+
//设置超时时间 默认是3000
96+
producer.setSendMsgTimeout(this.sendMsgTimeout);
97+
}
98+
//如果发送消息失败,设置重试次数,默认为2次
99+
if(this.retryTimesWhenSendFailed != null){
100+
producer.setRetryTimesWhenSendFailed(this.retryTimesWhenSendFailed);
101+
}
102+
103+
try {
104+
producer.start();
105+
LOGGER.info(String.format("producer is start ! groupName:[%s],namesrvAddr:[%s]", this.groupName, this.namesrvAddr));
106+
} catch (MQClientException e) {
107+
LOGGER.error(String.format("producer is error {}", e.getMessage(),e));
108+
if (LOGGER.isErrorEnabled()) {
109+
LOGGER.error("RocketMQ failed to start :", e);
110+
}
111+
}
112+
return producer;
113+
}
114+
}
115+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.gpmall.commons.tool.rocketmq;
2+
3+
import org.apache.rocketmq.client.producer.LocalTransactionState;
4+
import org.apache.rocketmq.client.producer.TransactionListener;
5+
import org.apache.rocketmq.common.message.Message;
6+
import org.apache.rocketmq.common.message.MessageExt;
7+
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
11+
/**
12+
* @Description: 事务监听器
13+
* @Author: wz
14+
* @Date: 2019-08-22 17:50
15+
**/
16+
17+
public class TransactionListenerImpl implements TransactionListener {
18+
private AtomicInteger transactionIndex = new AtomicInteger(0);
19+
20+
private ConcurrentHashMap<String, Integer> localTrans = new ConcurrentHashMap<>();
21+
22+
@Override
23+
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
24+
int value = transactionIndex.getAndIncrement();
25+
int status = value % 3;
26+
localTrans.put(msg.getTransactionId(), status);
27+
return LocalTransactionState.UNKNOW;
28+
}
29+
30+
@Override
31+
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
32+
Integer status = localTrans.get(msg.getTransactionId());
33+
if (null != status) {
34+
switch (status) {
35+
case 0:
36+
return LocalTransactionState.UNKNOW;
37+
case 1:
38+
return LocalTransactionState.COMMIT_MESSAGE;
39+
case 2:
40+
return LocalTransactionState.ROLLBACK_MESSAGE;
41+
}
42+
}
43+
return LocalTransactionState.COMMIT_MESSAGE;
44+
}
45+
}

gpmall-commons/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<module>commons-tool</module>
1313
<module>commons-core</module>
1414
<module>commons-lock</module>
15+
<module>commons-mq</module>
1516
</modules>
1617

1718
<distributionManagement>

0 commit comments

Comments
 (0)