The name of the project, goodsKill, on the one hand, means a flash sale of goods (it seems a bit like chinglish), and on the other hand, it can also be understood as good skill . This project hopes to build a complete project framework and integrate some good technologies and development skills ( Favoring back-end technology) for easy learning and review.
This project is a simulated flash sale project that provides a unified flash sale simulation request interface. Technically, it adopts SpringMVC + Mybatis persistence layer framework, uses Dubbo3.x [1] + Feign to complete inter-service interface calls, and uses Nacos for service registration discovery and configuration center. Support The database is divided into databases and tables, distributed transactions, and state machines are used to complete the conversion between data states (implemented based on Spring Statemachine).
Integrated with Spring AI service, simulated flash sales can be completed through AI robots.
The master
branch is built based on the latest Spring Cloud 2023.x + Spring Boot 3.x + JDK21 system. Currently, only the core simulation flash sale API interface is retained. If you need to use the Spring Boot 2.7.x + JDK11 version, you can switch to tag v2.7.4 (supported Login, registration and simple background management functions). The master branch is currently being upgraded and its functions are not stable enough. If you encounter code errors, it is recommended to use the older version.
The functions of this project are currently relatively rudimentary and have many imperfections. The real Flash Sale scenario is far more complex than the implementation method in this project. This project omits some technical implementation details in the real scenario. It is currently only used for learning reference. If If you think this project is helpful to you, please give it a star and support it? ~ ~.
Attachment: Code Cloud project link
https://gitee.com/techa/goodsKill
. If the clone speed is slow, use the Code Cloud warehouse to pull it. Synchronize to Code Cloud from time to time~
Tool or framework used | name | Official website |
---|---|---|
Spring Boot | Spring Boot Framework | https://spring.io/projects/spring-boot |
MyBatis-Plus | MyBatis enhancement tool | https://mp.baomidou.com/ |
ZooKeeper | Distributed coordination service | http://zookeeper.apache.org/ |
Redis | Distributed cache database | https://redis.io/ |
Kafka | message queue | http://kafka.apache.org/ |
RabbitMQ | message queue | https://www.rabbitmq.com/ |
MongoDB | MongoDB | https://www.mongodb.com/ |
MySQL | MySQL database | https://www.mysql.com/ |
Elasticsearch | Full text search engine | https://www.elastic.co |
Sharding-JDBC | Sub-database and sub-table components | https://shardingsphere.apache.org |
Spring Cloud Alibaba | Cloud Alibaba components | https://github.com/alibaba/spring-cloud-alibaba |
Apache Dubbo | RPC service remote calling framework | https://github.com/apache/dubbo |
Spring Cloud Gateway | Gateway component | https://spring.io/projects/spring-cloud-gateway |
Seata | Distributed transaction solutions | http://seata.io/zh-cn/index.html |
Spring SecurityOAuth2.0 | OAuth2.0 authorization component | https://spring.io/projects/spring-security-oauth |
GraphQL | A query language for APIs | https://docs.spring.io/spring-graphql/docs/current/reference/html |
Spring Statemachine | Spring state machine | https://spring.io/projects/spring-statemachine |
Sa-Token | Lightweight authority authentication framework | https://sa-token.cc/ |
Flyway | Database version control tool | https://flywaydb.org/ |
MinIO | Object storage service | https://min.io/ |
Spring AI | AI components | https://spring.io/projects/spring-ai |
goodsKill |--goodskill-admin ||SpringBoot Admin monitoring server, supports Spring Cloud microservice discovery|--goodskill-ai ||AI robot chat service|--goodskill-gateway ||Microservice API gateway, unified service authentication, Support dynamic route loading |--goodskill-order-provider ||Order service provider|--goodskill-seckill-provider ||Seckill service provider|--goodskill-spring-boot-starter ||Project configuration automatic assembly|--goodskill-common ||Public services (currently including minio upload and download functions) |--goodskill-web ||Provide flash kill simulation interface access|--goodskill-job ||elastic-job scheduled task|--goodskill-seata ||Example of distributed transaction solution integrating nacos+dubbo+shardingjdbc+seata| --goodskill-auth ||auth login and authorization module| |--auth-service ||User login authorization service based on Sa-Token framework |--oauth2-auth-server ||oauth2.0 login authorization server, customized login authorization service |--oauth2-resource-server ||oauth2.0 resource Server side, customized login authorization service
Currently, several flash killing solutions have been implemented, and the test interface is provided through SeckillMockController
Aggregation gateway Openapi document address: http://localhost/doc.html#/home
(need to enable gateway service)
Spring Boot Admin application monitoring address: http://www.goodskill.com:19031
, login username and password: user/123456
Scenario 1: Sychronized synchronization lock implementation
Scenario 2: Redisson distributed lock implementation
Scenario 3: ActiveMQ implementation (obsolete)
Scenario 4: Kafka message queue implementation
Scenario 5: Database atomic update
Scenario 6: Waiting for flash sale processing results in real time (obsolete)
Scenario 7: ZooKeeper distributed lock
Scenario 8: Use Redis to reduce inventory of flash sale products, send MQ asynchronously after the flash sale ends, and use MongoDB to complete data landing.
Scenario 9: Spring Cloud Stream implementation
Scenario 10: Sentinel current limiting + database atomic update (needs to be configured with the Sentinel console to configure the flow control rules of resource name limit
)
2021-04-14 21:58:59.857 INFO [goodskill-web,df43cc8f59291c48,df43cc8f59291c48] 15808 --- [ main] o.s.w.controller.SeckillMockController : 秒杀场景二(redis分布式锁实现)开始时间:Wed Apr 14 21:58:59 CST 2021,秒杀id:1000
2021-04-14 21:59:00.094 INFO [goodskill-web,144aa7910cca9520,2821cb8d62c5a908] 15808 --- [AClOSzbugzYng-1] o.s.w.s.c.SeckillMockResponseListener : 秒杀活动结束,秒杀场景二(redis分布式锁实现)时间:Wed Apr 14 21:59:00 CST 2021,秒杀id:1000
2021-04-14 21:59:00.101 INFO [goodskill-web,144aa7910cca9520,2821cb8d62c5a908] 15808 --- [AClOSzbugzYng-1] o.s.w.s.c.SeckillMockResponseListener : 最终成功交易笔数统计中。。。
2021-04-14 21:59:01.616 INFO [goodskill-web,144aa7910cca9520,2821cb8d62c5a908] 15808 --- [AClOSzbugzYng-1] o.s.w.s.c.SeckillMockResponseListener : 最终成功交易笔数统计中。。。
2021-04-14 21:59:03.129 INFO [goodskill-web,144aa7910cca9520,2821cb8d62c5a908] 15808 --- [AClOSzbugzYng-1] o.s.w.s.c.SeckillMockResponseListener : 最终成功交易笔数:10
2021-04-14 21:59:03.130 INFO [goodskill-web,144aa7910cca9520,2821cb8d62c5a908] 15808 --- [AClOSzbugzYng-1] o.s.w.s.c.SeckillMockResponseListener : 历史任务耗时统计:StopWatch '': running time = 36159894800 ns
---------------------------------------------
ns % Task name
---------------------------------------------
4492195700 012% 秒杀场景四(kafka消息队列实现)
3164155900 009% 秒杀场景八(秒杀商品存放redis减库存,异步发送秒杀成功MQ,mongoDb数据落地)
6219218300 017% 秒杀场景十(Sentinel限流+数据库原子性更新)
9189080600 025% 秒杀场景七(zookeeper分布式锁)
3135926500 009% 秒杀场景五(数据库原子性更新update set num = num -1)
3342791800 009% 秒杀场景九(基于springcloud stream rabbitmq)
3343433700 009% 秒杀场景一(sychronized同步锁实现)
3273092300 009% 秒杀场景二(redis分布式锁实现)
JDK: OpenJDK21
Sharding-JDBC: 5.5.0
SpringCloud: 2023.xx
SpringBoot: 3.3.x
SpringCloudAlibaba: 2023.xx
Apache Dubbo: 3.3.x
Docker image used
mirror | Version | port | Username and password |
---|---|---|---|
Nacos | 2.3.2-slim | 8848 | nacos:nacos (console) |
Redis | latest | 6379 | Password:123456 |
Kafka | 3.1.1 | 9092 | none |
KafkaManager | latest | 9001:9000 | none |
Mongo | 6.0.7 | 27017 | none |
MySQL | 8.0.29 | 3306 | root:Password123 |
Zookeeper | 3.6.2 | 2181 | none |
Elasticsearch | 7.17.3 | 9200 9300 | none |
Kibana | 7.17.3 | 5601 | none |
RabbitMQ | latest | 5672 15672 | none |
MinIO | latest | 9000 | root:password |
Seata | 2.0.0 | 7091 8091 | seata:seata (console) |
Execute in the project root directory goodsKill
mvn clean install Or #skip unit tests mvn clean install -DskipTests
The default port starts nacos, redis, mysql, rabbitmq, kafka, zookeeper, elasticsearch, seataServer, or use the docker-compose [2] command:
docker-compose -f goodskill-simple.yml up -d
Enter the goodskill-web/src/main/sql
directory, find the seckill.sql
file, establish the seckill
warehouse in the local mysql database and perform data initialization operations.
When docker-compose starts the MySQL image, it will automatically execute the initialization script. If you have already performed the previous step, you can skip this step. |
Configure host
127.0.0.1 kafka
127.0.0.1 nacos
127.0.0.1 redis
127.0.0.1 mysql
127.0.0.1 zookeeper
127.0.0.1 mongo
127.0.0.1 elasticsearch
127.0.0.1 rabbitmq
127.0.0.1 logstash
##如果网关服务部署在远程机器,此处改为相应的远程机器ip
127.0.0.1 www.goodskill.com
In the Nacos configuration center, add public configurations with DataId of goodskill-common-connection.yml
(middleware public configuration) and goodskill-common.yml
(service public configuration). Group is DEFAULT_GROUP
. For details, please refer to goodskill-common-connection.yml
in the project root directory. goodskill-common-connection.yml
, goodskill-common.yml
files
The main method runs the OrderApplication
class (order service)
The main method runs the SeckillApplication
class (seckill management service provider)
The main method runs the SampleWebApplication
class (simulating the flash sale web service)
Send a flash sale simulation request: Flash sale activity ID 1000, product quantity 10, perform 20 purchase operations, use sychronized synchronization lock to execute, for example:
You can directly use the following command to send a simulated flash sale request. Each flash sale activity seckillId corresponds to a unique product ID. Each time the interface is executed, there will be an inventory initialization action. After the interface is executed, it can be called repeatedly.
curl -X POST " http://www.goodskill.com:8080/sychronized "
-H " accept: */* " -H " Content-Type: application/json " -d
" { " requestCount " : 20, " seckillCount " : 10, " seckillId " : 1000} "
curl -X POST " http://www.goodskill.com:8080/redisson "
-H " accept: */* " -H " Content-Type: application/json " -d
" { " requestCount " : 20, " seckillCount " : 10, " seckillId " : 1000} "
curl --location --request POST ' http://www.goodskill.com:8080/limit '
--header ' User-Agent: apifox/1.0.0 (https://www.apifox.cn) '
--header ' Content-Type: application/json '
--data-raw ' {
"maxPoolSize": 10,
"seckillId": 1000,
"corePoolSize": 2,
"seckillCount": 100,
"requestCount": 120
} '
The request is executed asynchronously by default. You can view the execution log on the console. If the number of final successful transactions is equal to the number of items 10, it means there is no oversold or undersold problem.
A simple flash sale robot service built based on Spring AI Alibaba. Service calls can be completed through the robot service. Currently, it only supports opening flash sale activities and outputting the time consumption of the activity.
Enter the goodskill-ai
module and replace the configuration spring.ai.dashscope.api-key
, which can be obtained through the Alibaba Cloud Bailian platform
The main method runs the AiBotApplication
class and accesses the chat page http://127.0.0.1:18077
Here is an example conversation
我: 帮我执行秒杀
AI: 当然可以帮助您执行秒杀活动啦!为了顺利进行,请告诉我以下几个信息:1.秒杀活动的ID是多少?2. 这次秒杀的商品数量是多少?3.您打算这次请求的次数是多少?有了这些信息,我就能帮您开始了!?
我: 秒杀活动id 1001,请求次数20,商品数量10个
AI: 秒杀活动已成功开启!我们现在等待10秒钟,然后我会为您获取任务的耗时统计信息。秒杀活动已成功启动,并在10秒后获得了任务耗时统计信息:总耗时:6.673773375秒具体耗时分布如下:- 秒杀活动id:1001,秒杀场景五(数据库原子性更新update setnum = num -1)耗时 3.528280125 秒,占比 53%-秒杀活动id:1001,秒杀场景五(数据库原子性更新updateset num = num-1)耗时3.14549325 秒,占比 47%希望这些信息对您有帮助!如果还有其他问题或需要进一步的帮助,请随时告诉我哦!?
Add the following steps to the quick start section to start a complete project
Enter the goodskill-gateway
module, start the service gateway through the main method of GatewayBootApplication
class, and uniformly access various services through the gateway.
http://www.goodskill.com/api/order/**
corresponds to accessing the goodskill-order
service
http://www.goodskill.com/api/seata/**
corresponds to accessing the goodskill-seata
service
http://www.goodskill.com/api/seckill/**
corresponds to accessing the goodskill-seckill
service
http://www.goodskill.com/api/auth/**
corresponds to accessing the goodskill-auth
service
http://www.goodskill.com/api/web/**
corresponds to accessing the goodskill-web
service
The Sentinel
current limiting component has been integrated and supports pushing current limiting rules through nacos
configuration center. To use it, you need to start the Sentinel
console and start it with port 18088
The docker environment is not supported yet.
For the Seata distributed transaction test method, see the Seata distributed transaction test example running instructions.
The main method runs the GoodskillAdminApplication
class (microservice health status indicator monitoring)
docker es image startup failed
This problem usually occurs in Linux environment. Run the following command sysctl -w vm.max_map_count=262144
, or modify the /etc/sysctl.conf file and add the following configuration:
grep vm.max_map_count /etc/sysctl.conf
vm.max_map_count=262144
How to use the OAuth2.0 authorization server customized in this project for login authorization?
To be improved. .
What is the current compatibility between the various frameworks integrated into the project?
The versions of each mainstream framework that this project currently relies on are relatively new and have not been fully tested [3] .
How to solve the error " no available service found in cluster 'default', please make sure registry config correct and keep your seata server running
reported in the service startup console?
Just start the seata-server
service (provided in the docker-compose.yml file). You can refer to the Seata official website to add nacos related configurations. If distributed transactions are not used, you can ignore this error and it will not affect the normal operation of the service.
docker-compose cannot pull the image
hub.docker is blocked. Alibaba Cloud Image Accelerator can be used in China. For specific operations, see Alibaba Cloud Image Accelerator.
Failed to start project using JDK17 or above
Add the following jvm parameters when starting, for example:
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.util.concurrent=ALL-UNNAMED
--add-opens java.base/java.math=ALL-UNNAMED
--add-opens java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED
Currently, the control of flash sale activity status is based on the Spring Statemachine state machine. The advantages of using the state machine are:
Unified control of activity status facilitates centralized status maintenance;
Prevent business status from being changed at will and ensure controllable updates of status;
surface | database | Whether to divide the library | Sub-library field | Whether to divide the table | Sub-table fields |
---|---|---|---|---|---|
success_killed | MySQL | Yes (in the same server, it is divided into two libraries: seckill and seckill_01) | seckill_id | Yes (divided into two tables: success_killed_0 and success_kill_1) | user_phone |
? | None of the other tables are divided into databases or tables. By default, seckill is used as the main database. |
Solve the problem of Docker container connection failure to Kafka: https://www.cnblogs.com/hellxz/p/why_cnnect_to_kafka_always_failure.html