We have a series that covers the full practice of microservices from requirements to live, from code to K8S deployment, from logging to monitoring, and more.

The whole project uses the micro-services developed by GO-Zero, and basically includes some middleware developed by Go-Zero and related GO-Zero authors. The technology stack used is basically self-developed components of go-Zero project team, which is basically the whole Family of Go-Zero.

Actual combat project address: github.com/Mikaelemmmm…

1. Business architecture diagram of order service

2. Dependency

Order-API

  • Order-RPC
  • Payment – RPC

Payment – RPC

  • Mqueue-rpc (Message queue)

Order-RPC

  • Mqueue-rpc (Message queue)
  • Travel-rpc (Home Stay RPC)

3. Examples of orders

3.1 order

1. The user will place an order after browsing the date of homestay in travel service and call the ordering API interface

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

// Order module v1 interface
@server(
   prefix: order/v1
   group: homestayOrder
)
service order {
   @doc "Create a Home stay order"
   @handler createHomestayOrder
   post /homestayOrder/createHomestayOrder (CreateHomestayOrderReq) returns (CreateHomestayOrderResp)
   
   .....
}
Copy the code

Order-rpc is called in order-API

3. After the order is created by the check condition in RPC, mqueue-rpc is called to create a message queue that delays closing the order

4. Mqueue-rpc delay queue

Asynq is a high performance queue based on Redis. It also supports message queue, timing queue and fixed period queue. However, in order to demonstrate go-Zero’s official message queue go-Queue (Go-queue is based on Kafka), So go-queue for message queue, asynq for delay queue and timed task. Note here that this is just adding a deferred task to the deferred queue, the execution is not here, so let’s go to the code that executes 20 minutes later, in app/ Order/CMD/MQ

5. Queue of tasks with a delay of 20 minutes

In app/ Order/CMD/MQ, I would like to explain that gozero official goctl supports API, RPC, currently does not support console, MQ, etc., but Go-Zero provides ServiceGroup. It is convenient for us to manage any of our own services, so I used ServiceGroup to manage the services in MQ, which is also the official recommended way to use, the code is as follows:

1) the app/order/CMD/mq/order. Go first we see the main. Go

func main(a) {
	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()
}
Copy the code

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

List.mqs (c); list.mqs (c); list.mqs (c); list.mqs (c); list.mqs (c); list.mqs (c); list.mqs (c

Not only do we want to listen on asynq delay queue and timing queue, but we also want to listen on the Kafka queue of the Go-queue. In code, we don’t want to put the Kafka queue of the Go-queue together with asynq delay queue and timing queue, so we make a classification here

3) Asyny’s delayed message queue

Define asynq

Define the routing

Concrete implementation logic (close order logic)

So when we start this order – mq, asynq will be loaded, definitions, routing, and when we add delay queue to 20 minutes, before order will be automatically closed logic, if the order is not paid, here will shut down the order, payment is ignored, so that you can need not use timing task training in rotation close order, ha ha

3.2 Order List

There is no logic, is to find out to show it, it is good to have a look

// Order module v1 interface
@server(
   prefix: order/v1
   group: homestayOrder
)
service order {

   @doc "User Order List"
   @handler userHomestayOrderList
   post /homestayOrder/userHomestayOrderList (UserHomestayOrderListReq) returns (UserHomestayOrderListResp)
   
}
Copy the code

3.3 Order Details

There is no logic, is to find out to show it, it is good to have a look

// Order module v1 interface
@server(
	prefix: order/v1
	group: homestayOrder
)
service order {

	@doc "User Order Details"
	@handler userHomestayOrderDetail
	post /homestayOrder/userHomestayOrderDetail (UserHomestayOrderDetailReq) returns (UserHomestayOrderDetailResp)
}
Copy the code

4, and end

After placing an order, of course we have to pay for it. Let’s see the next article on payment services

The project address

Github.com/zeromicro/g…

Welcome to Go-Zero and star support us!

Wechat communication group

Pay attention to the public account of “micro-service Practice” and click on the exchange group to obtain the QR code of the community group.