RPC Call server method in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Original link, reference golang Chinese document: http://docscn.studygolang.com/pkg/net/rpc/#NewServer

The RPC package provides a way to enter an external method of an object over a network or other I/O connection. A server registers an object, marking it as a service for the name of the visible object type. Once registered, the object's external methods can be called remotely. A server can register multiple objects of different types, but it is not possible to register multiple objects of the same type.

Only methods that meet these criteria are considered visible by remote invocation, and other methods are ignored:

-the method is externally visible. -the method has two parameters, and the type of the parameter is externally visible. -the second parameter of the method is a pointer. -Method has return type error

In fact, the method must look something like this

Func (t *t) MethodName (Argtype T1, Replytype *t2) error

T,t1 and T2 can be serialized by Encoding/gob. No matter what codec is used, these requirements are met. (In the future, these requirements may be relaxed on custom codecs)

The first parameter of the method represents the argument supplied by the caller, and the second parameter represents the parameter returned to the caller. The return value of the method, if non-null, is returned as a string, and the client error is errors. The new call returns the same. If the error returns, the returned parameters will be sent back to the client.

Service break you can use Serveconn to process requests on a single connection. In a more general way, the server can create a network listener, then call Accept, or listen to an HTTP, handling Handlehttp and Http.serve.

The client wants to use the service to establish the connection, and then call Newclient on the connection to establish the connection. A more convenient approach is to call Dial (Dialhttp) to establish a new network connection (an HTTP connection). The client obtains an object with two methods, call and go, with the specified parameters: the service and the method pointer to the parameter, and a pointer to the returned result.

The call method waits for the remote invocation to complete, but the Go method calls the call method asynchronously, using the Invoke channel to flag the invocation.

Unless a codec is explicitly developed, the ENCODING/GOB is used by default to transfer data.

This is a simple example where the server wants to serve out Arith objects:

Package ServerType args struct {A, B int}type quotient struct {Quo, Rem int}type arith intfunc (t *arith) Multiply (args *a RGS, reply *int) error {*reply = args. A * args. Breturn Nil}func (t *arith) Divide (args *args, quo *quotient) error {if args. B = = 0 {return errors. New ("Divide by Zero")}quo. Quo = args. A/args. Bquo.rem = args. A% args. Breturn Nil}

Server-side invocation (using HTTP Service):

Arith: = new (Arith) RPC. Register (Arith) RPC. Handlehttp () L, E: = Net. Listen ("TCP", ": 1234") if E! = nil {log. Fatal ("Listen error:", e)}go http. Serve (l, Nil)

At this time, the client can see the service "Arith", and there are "arith.multiply" Methods and "Arith.divide" methods. Call one of the clients to connect to the service first:

Client, err: = RPC. Dialhttp ("TCP", ServerAddress + ": 1234") if err! = Nil {log. Fatal ("Dialing:", err)}

When it wants to invoke the remote service:

Synchronous Callargs: = &server. Args{7,8}var Reply Interr = client. Call ("Arith.multiply", args, &reply) if err! = Nil {log. Fatal ("Arith error:", err)}fmt. Printf ("Arith:%d*%d=%d", args. A, args. B, Reply)

Or

Asynchronous Callquotient: = new (quotient) Divcall: = client. Go ("Arith.divide", args, quotient, nil) Replycall: = <-divcall.done//would be equal to divcall//check errors, print, ET C.

Service-side implementations need to provide a simple, type-safe service to the client.

That is, when the server registers an object, it can invoke all the methods that are hanging on the object.



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.