Source code address: github.com/ThoughtsBet…

High concurrency multi – scheme SEC kill architecture

This source is the original small volume of supporting source code, designed to help small volume readers deconstruct the core of high concurrency design from the source code, is now open source. The source code contains two parts: core application and gateway application. Limited by copyright requirements, for a detailed interpretation of the source code, please see the booklet “High concurrency SEC design essentials and implementation”.

Source code core features

  • Complete distributed architecture application practice based on Spring Boot and Spring Cloud;
  • Design skills of local cache and distributed cache;
  • Synchronous ordering and high concurrent inventory deduction;
  • Asynchronous queue ordering and inventory deduction;
  • Decentralized sub-warehouse sub-table and sub-barrel inventory deduction;
  • The principle and application of current limiting;
  • Black and white attack and defense and security risk control strategy;
  • Domain-driven design methods and practices;
  • Current limiting, downgrading and fusing landing and practice;
  • Swarm cluster deployment and container technology;
  • Application and practice of 10+ middleware such as Redis+Nacos+RocketMQ+ELK;
  • Application of dynamic configuration methods and practices;
  • Measurement and monitoring of distributed architecture;
  • RESTful APIs design and experience.

The overall architecture from the link perspective

A quick start source code guide

In order to facilitate readers to quickly use the source code, we have made a video guide, please go to B station to view.

Second, the source code structure

Three, technical selection overview

In order to give consideration to readers of different levels and reduce the threshold and cost of technical understanding, we mainly adopt the current mainstream technical products and solutions on the market in specific technology selection:

How do I start and run the application

For the middleware used by FlashSale, we provide a complete solution based on Docker-compose, which can be installed in a Docker environment with one click. After downloading and opening the source code, you will see the enviroment directory in the project root directory. This directory has four related files and folders:

  • docker-compose.yml: [Full version] Middleware deployment scripts. Related middleware installation scripts that you can executedocker-compose -f docker-compose.yml upCommand to install all middleware dependencies.
  • docker-compose-light.yml: [Lightweight] Middleware deployment scripts (Local Development recommendation) to remove non-essential middleware services. Related middleware installation scripts that you can executedocker-compose -f docker-compose-light.yml upCommand to install all middleware dependencies.
  • Docker-cluster-middlewares. Yml: [clustering] middleware deployment scheme, suitable for cluster deployment under the Swarm network, refer to chapter 15 for specific deployment mode.
  • Docker-cluster-apps. yml: [clustering] application deployment solution, suitable for cluster deployment under the Swarm network. For details, see Section 15.
  • Config: location of configuration files for related middleware, including Prometheus and MYSQL configurations;
  • Grafana-storage: configuration data stored in grafana. The main reason why we provide this file is that the data source and report configuration of Grafana are relatively troublesome, and it may take a long time to configure ourselves, so we can directly load and use it after we provide it.
  • Data: indicates the data storage location of some middleware, such as MYSQL. The data in this directory is generated by the middleware system runtime, and it’s a lot of messy data. We didn’t put it in Git, so you won’t see it when you download the source code, but it will appear at runtime. So why put it in this location, and not somewhere else in the computer system? The main purpose of this is to make it easy to view and manage your data. You can always clean up all your data and start again, and of course you can put it anywhere.

Step 1: Start the middleware

  1. Download the source code to enterenvironmentDirectory, executedocker-compose -f docker-compose-light.yml up Start middleware;
  2. If you are not familiar with Docker command, it is recommended to install Docker Desktop to simplify container management, you can intuitively see the container startup status and log output;
  3. To stop all containers, run this commanddocker-compose -f docker-compose-light.yml down;
  4. To recreate all containers, perform this operationdocker-compose -f docker-compose-light.yml up --force-recreate.

About initialization of library tables in a database

For the business table used by FlashSale, we have put the initialization script in enviroment/config/mysql. Docker-compose will perform the initialization action of the data table after installing mysql, realizing the database out of the box.

.├ ── config │ ├── ├─ ├─ flash_sale_├ ─ ├─ flash_sale_init_sharding_0 # 0 database initialization statement │ ├ ─ ─ flash_sale_init_sharding_1. SQL / / # 1 database initialization statement │ └ ─ ─ nacos_init. SQL / / Nacos persistence statement ├ ─ ─ Docker - cluster - apps. Yml ├ ─ ─ docker - cluster - middlewares. Yml ├ ─ ─ docker - compose - light. Yml └ ─ ─ docker - compose. YmlCopy the code

Note that we provide two initialization scripts for MYSQL: Flash_sale_init. SQL and nacos_init.yml, the former is the business table initialization script, the latter is the Nacos initialization script, because FlashSale requires the dynamic configuration function provided by Nacos, but Nacos is memory storage by default, So we implemented a persistent storage scheme based on MYSQL for it.

Step 2: Start the application from the IDE

Before enabling the application, ensure that the first step is successfully performed and each middleware container is successfully started.

  1. Download the source code and execute./mvnw clean installThe system dependency packages have been installed.
  2. choosestartIn the modulecom.actionworks.flashsale.FlashSaleApplicationRun as a program entry.

Please specify properties as local in the IDE when starting locally.

This is recommended during debugging. When FlashSale starts, it will connect to the middleware you installed earlier.

Optional: Start and run using Docker

In addition to starting FlashSale in the IDE, starting it through Docker is also a very convenient solution.

  1. Build the FlashSale local image with the following command:
docker build -t flash-sale-app . 
Copy the code

After the build is complete, check with Docker Images to see if the image already exists.

  1. Add the following configuration to what was said earlierdocker-compose.ymlIn the middle of the run, the system will also boot at the same time. Of course, we can also run it from a separate file. It is important to note that FlashSale will live on the same network as the middleware when running through Docker, and we create a separate configuration file for this that needs to be specified at run timedockerConfiguration.
services:
  flash-sale-app:
    image: flash-sale-app
    container_name: flash-sale-app
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    ports:
      - 8090: 8090
    networks:
      - thoughts-beta-cluster-apps
    restart: on-failure
  flash-sale-gateway:
    image: flash-sale-gateway
    container_name: flash-sale-gateway
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    ports:
      - 8080: 8080
    networks:
      - thoughts-beta-cluster-apps
    restart: on-failure
Copy the code

How to test the interface

After installing and initializing the middleware and starting the application, we can then try to test the interface to determine if the middleware and application are ready and working properly.

Also, we don’t let readers create scripts and prepare test data themselves, because it’s not in line with our reader-first and out-of-the-box principles. To do this, in the root directory of the project, you will see the Postman directory that we provide. It is the Postman test script, which contains the interface definition and test data. You can simply select an interface and click test.

The Postman script location looks like this:

├ ─ ─ the environment │ ├ ─ ─ the config │ ├ ─ ─ data │ ├ ─ ─ docker - compose. Yml │ └ ─ ─ grafana - storage └ ─ ─ postman └ ─ ─ Flash-sale-postman. json # test scriptCopy the code