Service A wants some work from Service B. How does Service B respond, considering that Service B might be handling requests for a lot of other services.
The solution is to have the requestor mention the response topic(in this case a channel) to the responder.
type Request struct{
someArg string
replyTo chan<- Response
}
type Response struct{
reply string
}
funcresponder(c <-chan Request){
for request :=range c {
var resp Response
resp.reply ="reply-to-"+ request.someArg
request.replyTo <- resp
}
}
funcrequestor(c chan<- Request){
myChannel :=make(chan Response)
for i :=0; i <5; i++{
c <- Request{fmt.Sprintf("message%d", i), myChannel}
resp :=<-myChannel
fmt.Printf("request %d, response %s\n", i, resp.reply)
In a microservice architecture, a message may flow through multiple services, it is important that each message has a unique identifier to enable correlation and debugging in the service’s code.
We can use twitter sonyflake to generate id: 20f8707d6000108