1

We use a series to explain the complete practice of microservices from requirements to online, from code to k8s deployment, from logging to monitoring, etc.

The whole project uses microservices developed by go-zero, which basically includes go-zero and some middleware developed by related go-zero authors. The technology stack used is basically the self-developed components of the go-zero project team, basically go -zero the whole family bucket.

Actual project address: https://github.com/Mikaelemmmm/go-zero-looklook

1. Order service business architecture diagram

2. Dependencies

order-api (order api)

  • order-rpc (order-rpc)
  • payment-rpc

payment-rpc

  • mqueue-rpc (message queue)

order-rpc (order-rpc)

  • mqueue-rpc (message queue)
  • travel-rpc (homestay rpc)

3. Order example

3.1 Place an order

1. The user wants to place an order after browsing the homestay in the travel service and chooses a date, and calls the order api interface

app/order/cmd/api/desc/order.api

 // 订单模块v1版本的接口
@server(
   prefix: order/v1
   group: homestayOrder
)
service order {
   @doc "创建民宿订单"
   @handler createHomestayOrder
   post /homestayOrder/createHomestayOrder (CreateHomestayOrderReq) returns (CreateHomestayOrderResp)
   
   .....
}

2. Call order-rpc in order-api

3. After the verification condition in rpc creates an order, mqueue-rpc will be called to create a message queue that delays closing the order

4. mqueue-rpc delay queue

The delay queue uses asynq, which is a high-performance queue based on redis, and supports message queues, timing queues, and fixed-period queues at the same time, but our project is to demonstrate go-zero's official message queue go-queue (go-queue is based on kafka), so go-queue is used for message queues, and asynq is used for delay queues and timed tasks. Note here that this is just adding a delay task to the delay queue, the specific execution is not here, then let's look at the specific code executed after 20 minutes, in app/order/cmd/mq

5. Task queue with a delay of 20 minutes

In app/order/cmd/mq, I want to explain here that the services generated by go-zero's official goctl support are currently api and rpc. Currently, there is no support for console, mq, etc., but go-zero provides ServiceGroup, which is convenient for us. To manage any of our own services, so in mq, I use ServiceGroup to manage services, which is also the official recommended way of use. The code is as follows:

1) app/order/cmd/mq/order.go First we look at main.go

 func main() {
    flag.Parse()
    var c config.Config

    conf.MustLoad(*configFile, &c)
    prometheus.StartAgent(c.Prometheus)

    serviceGroup := service.NewServiceGroup()
    defer serviceGroup.Stop()

    for _, mq := range listen.Mqs(c) {
        serviceGroup.Add(mq)
    }
    serviceGroup.Start()
}

ServiceGroup You can add any service, but how to become a service? Then you have to implement two methods, a Starter and a Stoper

2) We can see the loop listen.Mqs(c) in main, then let's see what listen.Mqs(c) has

We not only want to monitor the delay queue and timing queue of asynq, but we also want to monitor the kafka message queue of go-queue. In terms of code, we consider that we don’t want to put the kafka message queue of go-queue together with the delay queue and timing queue of asynq go, so here is a classification

3) asyny's delayed message queue

define asynq

define routes

Specific implementation logic (close order logic)

So when we start this order-mq, asynq will be loaded and the route will be defined. When the delay queue we added before reaches 20 minutes, the order closing logic will be automatically executed. If the order is not paid, the order will be closed here. , ignore it after payment, so that you can close the order without using timed task rotation training, haha

3.2 Order List

There is no logic, just check it out and display it, just look at it yourself

 // 订单模块v1版本的接口
@server(
   prefix: order/v1
   group: homestayOrder
)
service order {

   @doc "用户订单列表"
   @handler userHomestayOrderList
   post /homestayOrder/userHomestayOrderList (UserHomestayOrderListReq) returns (UserHomestayOrderListResp)
   
}

3.3 Order Details

There is no logic, just check it out and display it, just look at it yourself

 // 订单模块v1版本的接口
@server(
    prefix: order/v1
    group: homestayOrder
)
service order {

    @doc "用户订单明细"
    @handler userHomestayOrderDetail
    post /homestayOrder/userHomestayOrderDetail (UserHomestayOrderDetailReq) returns (UserHomestayOrderDetailResp)
}

4. End

After placing an order, of course we have to pay, then look at the next payment service

project address

https://github.com/zeromicro/go-zero

Welcome go-zero and star support us!

WeChat exchange group

Follow the official account of " Microservice Practice " and click on the exchange group to get the QR code of the community group.


kevinwan
931 声望3.5k 粉丝

go-zero作者