Golang Nats request reply mode

Source: Internet
Author: User

Request Response Mode

Neither the Nats nor the queue mode guarantees that the message must be sent to the Subscriber unless the Subscriber sends a response to the publisher.
So the subscriber sends a receipt to the publisher, which is the request response mode.

What is the use of this model?

NATS requires subscribers to complete the subscription first, post the message, Subscribers can receive the message, similar to the offline message mode Nats is not supported. Even if the subscription is completed before the message is sent, the sender of the message does not know if any of the subscribers received the message, and the request response pattern responds to this situation.

Basic process

A sends a message, B receives a message, and sends a receipt to a. This is the basic flow of request reply.

Basic implementation Principles

  • A enable request mode to send a message (the message contains the receipt information, the Replya topic), and a synchronous wait receipt (with a time-out).
  • b receives the message, takes out the receipt information in the Message =replay topic, to the replay topic, 主动 sends the ordinary message (the message content can be customized, such as Service1 on Server a receives MSGID=XXXX message. )。
  • A message is received within the timeout, confirming the end.
  • A message was not received within the timeout period and the timeout ended.

Attention

  • Because the message sent by a is wrapped in the receipt measurement related information, subscriber B receives the message, but also to actively send receipts, so the request response mode, has an impact on both sides.
  • A after sending a message, wait for the receipt of B, you need to set a time-out period, after the timeout, not waiting for receipts, the direct end, the effect and the message does not need receipts to send the same, do not care whether there are subscribers to receive messages.

Two modes

There are two modes of request reply:

  • One to one default mode

1 messages, n subscribers, message senders, receive only one 回执记录 (because the message sender receives the receipt message, the subscription to the receipt message is automatically disconnected.) ), even if all n subscriptions have received a message. Note: The difference between the pub/sub and the queue mode

  • One to many non-default mode, you need to implement it yourself

1 messages, n subscribers, message senders, can set a quantity limit of n, accept the N receipts message, and then disconnect the subscription to the receipt message.

Server

Package Mainimport ("Github.com/nats-io/go-nats" "Log" "flag") const (//url = "nats://192.168.3.125:4222" url = nats. Defaulturl) Var (nc *nats. Conn Encodeconn *nats. Encodedconn err Error) func init () {if NC, err = Nats. Connect (URL); Checkerr (ERR) {//if encodeconn, err = Nats. Newencodedconn (NC, Nats.            Json_encoder); Checkerr (Err) {}}}func Main () {var (servername = flag). String ("servername", "Y", "Name for server") Queuegroup = flag. String ("group", "", "group name for Subscribe") SUBJ = flag. String ("Subj", "Yasenagat", "Subject name")) flag. Parse () Mode: = "queue" if *queuegroup = = "" {mode = "pub/sub"} log. Printf ("server[%v] Subscribe subject[%v] in [%v]mode", *servername, *SUBJ, Mode) StartService (*SUBJ, *servername+ "work Er1 ", *queuegroup) StartService (*SUBJ, *servername+" Worker2 ", *queuegroup) StartService (*SUBJ, *servername+" worker 3 ", *queueGroup) NC. Flush () Select {}}//receive messagefunc startservice (subj, name, queue string) {Go Async (NC, SUBJ, name, queue)}fun C Async (NC *nats. Conn, SUBJ, name, queue string) {replymsg: = name + "Received a msg" if queue = = "" {NC. Subscribe (SUBJ, func (msg *nats). MSG) {NC. Publish (Msg. Reply, []byte (replymsg)) log. PRINTLN (Name, "Received a message from Async:", String (Msg. Data)})} else {NC. Queuesubscribe (subj, Queue, func (msg *nats). MSG) {NC. Publish (Msg. Reply, []byte (replymsg)) log. PRINTLN (Name, "Received a message from Async:", String (Msg. Data)})}}func Checkerr (err error) bool {if err! = Nil {log. PRINTLN (ERR) return false} return true}

Client

Package Mainimport ("Github.com/nats-io/go-nats" "Log" "Github.com/pborman/uuid" "Flag" "Time") const ( url = "nats://192.168.3.125:4222" url = nats. Defaulturl) Var (nc *nats. Conn Encodeconn *nats. Encodedconn err Error) func init () {if NC, err = Nats. Connect (URL); Checkerr (Err, func () {}) {//if encodeconn, err = Nats. Newencodedconn (NC, Nats.            Json_encoder); Checkerr (Err, func () {}) {}}}func Main () {var (subj = flag). String ("Subj", "Yasenagat", "Subject name")) flag. Parse () log. Println (*SUBJ) startclient (*SUBJ) time. Sleep (time. Second)}//send message to Serverfunc startclient (Subj string) {for I: = 0; i < 3; i++ {id: = UUID. New () log. PRINTLN (ID) if MSG, err: = NC. Request (SUBJ, []byte (id+ "Hello"), time. Second); Checkerr (Err, func () {//Handle err}) {log. Println (String (Msg. Data)}}}funcCheckerr (Err Error, errfun func ()) bool {if err! = Nil {log. PRINTLN (Err) Errfun () return false} return true}

Pub/sub mode start

$./MAIN2018/08/18 18:54:10 Server[y] Subscribe Subject[yasenagat] in [pub/sub]mode2018/08/18 18:54:26 Y worker2 Received A message from async:b035d7c2-e7e9-4337-bb8a-a23ec85fc31a hello2018/08/18 18:54:26 Y worker1 Received a message from a sync:b035d7c2-e7e9-4337-bb8a-a23ec85fc31a hello2018/08/18 18:54:26 Y worker3 Received a message from async:b035d7c2- e7e9-4337-bb8a-a23ec85fc31a hello2018/08/18 18:54:26 Y worker2 Received a message from async:2d8dfe75-8fee-4b4c-8599-18 24638dfa8c hello2018/08/18 18:54:26 Y worker1 Received a message from async:2d8dfe75-8fee-4b4c-8599-1824638dfa8c Hello2 018/08/18 18:54:26 Y worker3 Received a message from async:2d8dfe75-8fee-4b4c-8599-1824638dfa8c HELLO2018/08/18 18:54:2 6 y worker2 Received a message from async:fe9f773a-129b-4919-9bc4-c8a4571fef6e hello2018/08/18 18:54:26 Y worker1 recei VED a message from async:fe9f773a-129b-4919-9bc4-c8a4571fef6e hello2018/08/18 18:54:26 Y worker3 Received a message Fro M async:fe9f773a-129b-4919-9bc4-c8a4571fef6e Hello 

Send Message

$ ./main2018/08/18 18:54:26 yasenagat2018/08/18 18:54:26 b035d7c2-e7e9-4337-bb8a-a23ec85fc31a2018/08/18 18:54:26 Y worker3 Received a msg2018/08/18 18:54:26 2d8dfe75-8fee-4b4c-8599-1824638dfa8c2018/08/18 18:54:26 Y worker2 Received a msg2018/08/18 18:54:26 fe9f773a-129b-4919-9bc4-c8a4571fef6e2018/08/18 18:54:26 Y worker2 Received a msg

Queue mode Startup

$ ./main -group=test2018/08/18 19:14:31 Server[Y] Subscribe Subject[yasenagat] in [queue]Mode2018/08/18 19:14:33 Y worker2 Received a message From Async :  4ecf2728-b3a7-4181-893a-aefde3bc8d2e hello Y worker2 Received a msg2018/08/18 19:14:33 Y worker3 Received a message From Async :  4e7f1363-9a47-4705-b87a-4aaeb80164f0 hello Y worker3 Received a msg2018/08/18 19:14:33 Y worker2 Received a message From Async :  38b1f74b-8a3b-46ba-a10e-62e50efbc127 hello Y worker2 Received a msg

Send Message

$ ./main2018/08/18 19:14:33 yasenagat2018/08/18 19:14:33 4ecf2728-b3a7-4181-893a-aefde3bc8d2e2018/08/18 19:14:33 Y worker2 Received a msg2018/08/18 19:14:33 4e7f1363-9a47-4705-b87a-4aaeb80164f02018/08/18 19:14:33 Y worker3 Received a msg2018/08/18 19:14:33 38b1f74b-8a3b-46ba-a10e-62e50efbc1272018/08/18 19:14:33 Y worker2 Received a msg

In queue mode, 3 messages are sent, 3 subscribers have the same queue, and each message is received by only one subscriber.

In Pub/sub mode, 3 messages are sent and 3 subscribers receive 3 messages, altogether 9.

Summarize:

Receipts are primarily addressed by the question of whether the Subscriber received the message, and how many subscribers were receiving the message. (Not a specific business is performing a completed receipt!) )
Event-based schema patterns can be built on message mechanisms and rely on message mechanisms. One of the implementations of an asynchronous invocation is based on the event pattern. Asynchronous invocation is also a common task in distributed systems.

Business model

    • Business A sends Eventa to event hubs,等待回执
    • Event hubs informs a that a message has been received and the broadcast is sent out
    • Subscriber B subscribes to the Eventa theme
    • Event hubs sends broadcasts to the Eventa topic,等待回执
    • b receive the message, inform the event hub, receive Eventa, and begin to perform the task Taska
    • B asynchronously executes Taska, notifies event hubs Taskacomplete,等待回执
    • Event hubs sends receipts to B, outbound broadcasts, Taskacomplete
    • ........

If the time-out, failed to receive receipts, the acknowledgement of receipt information is required to actively invoke the relevant interface, query task execution status, according to the task status to do follow-up processing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.