“This is my 32nd day of participating in the First Challenge 2022. For details: First Challenge 2022”

In the previous article, we gradually built a micro-service framework based on springCloudAlibaba, which integrates dependency management component Maven, interface document management tool Knife4J, persistence layer framework Mybatis – Plus. And the code generator Mybatis – plus-Gennerator, as well as different configuration files for different environments. This environment is not a waste of time, so we now use this service to do some work, and when we find problems, we will gradually optimize it through code optimization, or the introduction of other components. Keep adding new things to make it bigger.

Before this article, there are some basic component integration, environment building, I hope you can be patient to finish reading, and then you can refer to this article to achieve.

All contents of the article are included in this special topic: juejin.cn/column/7053… Beginners are advised to learn first

This article project code gitee address: gitee.com/wei_rong_xi…

A, planning,

1.1 Service Planning

I would like to make a set of microservices in a high concurrency scenario, based on the current code, simulate the scene of a large number of orders, and constantly find problems and optimize problems. Therefore, I now plan several modules for this service as follows:

Process from top to bottom:

  • User service selects goods
  • Order after selecting goods
  • Pay when the order is completed
  • The payment will be deducted from the user’s account

1.2 Service Planning

Therefore, service modules are divided as follows:

From top to bottom:

  • Account service
  • The gateway
  • Code generation service
  • Goods and services
  • Order service
  • The starter
  • Trading service
  • Customer service

1.3 Database Planning

The data plan is temporarily in the form of multiple tables in a library:

From top to bottom:

  • The code generates the rule table
  • Goods table
  • The order sheet
  • Order Details
  • Trade table
  • The users table
  • User account table

The above is the simple business, code and table structure planning, I will use the simplest and most direct way to complete the business line.

Second, call process analysis

I will simulate the following user shopping process as follows:

Among them, the test service imitates our app, and its whole shopping process has the following four stages:

  • 1- : Queries user information
  • 2- : Queries a product
  • 3- : order -> check commodity inventory
  • 4- : Payment -> Query order information -> Account deduction

Each of these phases also has a different invocation relationship between services, which I won’t say, but it’s all in the diagram. And there’s a lot of detail that even calls, and I’m not going to show it on the graph, but the more I draw it, the messier it gets.

Third, code writing

With the above process, we can write our business code.

3.1 Code Generation

Now it is time to verify our previous integration of mybatis- plugin-Gennerator and Velocity by calling the code generation interface and generating zip files.

In the previous article, feignClient was not introduced, so add:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.10. RELEASE</version>
</dependency>
Copy the code

After the code generation, the format looks like this. Take the order service as an example:

Verify that the service starts normally after code generation, at nacOS:

In the interface document, check whether the generated interface is normal, except for the gateway, six services, neatly:

Order service interface list:

3.2 Creating a Client folder for service invocation

During code generation, we generated the client interface of our own service for direct invocation by other services.

If other services need to call a service, they copy that client interface into their client folder. Copy the DTOS as well, because the interface arguments generated by the code are dTOS.

Add the order service client interface to the user service:

Following the previous flow chart, the clients of the services that need to call each other are configured. The process is not shown.

3.3 Business process code implementation

3.3.1 Build a test service

Build a test service that interacts with the gateway and invokes the business service.

The test service does several things:

  • Invoke the user service to get the user information
  • Use the obtained user information to query and bind commodity information
  • Place orders using user and product information
  • Check the order information and pay

The test service is invoked using Hutool’s HTTP tool.

3.3.2 Ordering process

Because it is the test code, it is slightly different from the real ordering process. We complete the calling of the ordering process according to the order in the previous section. The code in this paper only supports one order for the same commodity at present, but it can be multiple.

3.3.3 Payment process

When the user places an order, the user needs to pay for the order according to the order number.

Four, test,

After the previous design and research and development, the basic order and payment function has been realized, the following simple test.

My users and products, and the number of flat purchases are all random.

4.1 Data Initialization

  • SQL table structure

  • Create test data. In the controller of user service, user account service and commodity service, there is a test method to generate data. Students can execute this method to create data by themselves.

4.2 test

  • Create an order:

    Testing service call http://localhost:8010/test/order to create order, return to the order number.

    Order generation and order details are as follows:

    Product information, starting with 100W, reduced by one:

  • Pay for this order 9

    Testing service call interface: http://localhost:8010/test/trading? OrderId =9 returns the following result:

    The order information status has changed, as shown below:

    The trading order is as follows:

    User Account Information:

Five, the summary

This article is temporarily in front of the integration of things, in accordance with a certain business logic used, is a summary of the front.

But it is also the beginning of an expansion that will be updated and integrated with new content from time to time.

Suitable for beginners, want to learn micro service students to learn, not things can refer to the source code, or direct comments ask me.

All contents of the article are included in this special topic: juejin.cn/column/7053…

This article project code gitee address: gitee.com/wei_rong_xi…