Use of the Go language net package RPC Remote Call

Source: Internet
Author: User

One, HTTP-based RPC

Service side:

Package Main;import ("Net/rpc" "Net/http" "Log")//go support for RPC, supports three levels: TCP, HTTP, jsonrpc//go RPC only supports the interaction between the server and client of Go development, Because the GOB encoding//Note field must be exported type Params struct {Width, Height int;} The type Rect struct{}//function must be exported//must have two export type parameters//The first parameter is the receive parameter//the second parameter is returned to the client parameter, must be a pointer type//function and have a return value Errorfunc (r *rect) area (p Params, ret *int) error {*ret = P.width * P.height;return nil;} Func (R *rect) Perimeter (P Params, ret *int) error {*ret = (p.width + p.height) * 2;return Nil;} Func Main () {rect: = new (rect);//registers a RECT service RPC. Register (rect);//BIND service processing to RPC on HTTP protocol. Handlehttp (); Err: = http. Listenandserve (": 8080", nil); if err! = Nil {log. Fatal (err);}}

Client:

Package Main;import ("Net/rpc" "Log" "FMT") type Params struct {Width, Height int;} Func Main () {///Connect the remote RPC service RPC, err: = RPC. Dialhttp ("TCP", "127.0.0.1:8080"); if err! = Nil {log. Fatal (err);} RET: = 0;//Call remote method//Note The third parameter is pointer type err2: = RPC. Call ("Rect.area", params{50, +, &ret); if err2! = Nil {log. Fatal (ERR2);} Fmt. PRINTLN (ret); ERR3: = RPC. Call ("Rect.perimeter", params{50, +, &ret); if err3! = Nil {log. Fatal (ERR3);} Fmt. PRINTLN (ret);}

Second, TCP-based RPC

Service side:

Package Main;import ("NET" "Log" "NET/RPC")//note field must be exported type Params struct {Width, Height int;} Type Rect Struct{}func (r *rect) area (P Params, ret *int) error {*ret = P.width * P.height;return nil;} Func (R *rect) Perimeter (P Params, ret *int) error {*ret = (p.width + p.height) * 2;return Nil;} Func chkerror (err error) {if err! = Nil {log. Fatal (err);}} Func Main () {rect: = new (rect);//register RPC service RPC. Register (rect);//Get tcpaddrtcpaddr, err: = Net. RESOLVETCPADDR ("TCP4", "127.0.0.1:8080"); Chkerror (err);//Listener Port Tcplisten, ERR2: = Net. LISTENTCP ("TCP", tcpaddr); Chkerror (ERR2);//Dead Loop processing connection request for {conn, err3: = Tcplisten. Accept (); if err3! = nil {continue;} Use Goroutine to handle RPC connection requests alone to go RPC. SERVECONN (conn);}}

Client:

Package Main;import ("Net/rpc" "FMT" "Log") type Params struct {Width, Height int;} Func Main () {///connect to the remote RPC Service///use dialhttp here, the other code is the same as RPC, err: = RPC. Dial,http. Dial ("TCP", "127.0.0.1:8080"); if err! = Nil {log. Fatal (err);} RET: = 0;//Call remote method//Note The third parameter is pointer type err2: = RPC. Call ("Rect.area", params{50, +, &ret); if err2! = Nil {log. Fatal (ERR2);} Fmt. PRINTLN (ret); ERR3: = RPC. Call ("Rect.perimeter", params{50, +, &ret); if err3! = Nil {log. Fatal (ERR3);} Fmt. PRINTLN (ret);}

Third, the JSON RPC way

The Jsonrpc method is that the data is encoded using JSON rather than GOB encoding.

Service side:

Package Main;import ("NET" "Log" "Net/rpc" "NET/RPC/JSONRPC")//note field must be exported type Params struct {Width, Height int;} Type Rect Struct{}func (r *rect) area (P Params, ret *int) error {*ret = P.width * P.height;return nil;} Func (R *rect) Perimeter (P Params, ret *int) error {*ret = (p.width + p.height) * 2;return Nil;} Func chkerror (err error) {if err! = Nil {log. Fatal (err);}} Func Main () {rect: = new (rect);//register RPC service RPC. Register (rect);//Get tcpaddrtcpaddr, err: = Net. RESOLVETCPADDR ("TCP4", "127.0.0.1:8080"); Chkerror (err);//Listener Port Tcplisten, ERR2: = Net. LISTENTCP ("TCP", tcpaddr); Chkerror (ERR2); for {conn, err3: = Tcplisten. Accept (); if err3! = nil {continue;} Use Goroutine to process RPC connection requests separately//use JSONRPC to process go jsonrpc here. SERVECONN (conn);}}

Client:

Package Main;import ("FMT" "Log" "Net/rpc/jsonrpc") type Params struct {Width, Height int;} Func Main () {///connect to the remote RPC service//use JSONRPC here. DIALRPC, err: = Jsonrpc. Dial ("TCP", "127.0.0.1:8080"); if err! = Nil {log. Fatal (err);} RET: = 0;//Call remote method//Note The third parameter is pointer type err2: = RPC. Call ("Rect.area", params{50, +, &ret); if err2! = Nil {log. Fatal (ERR2);} Fmt. PRINTLN (ret); ERR3: = RPC. Call ("Rect.perimeter", params{50, +, &ret); if err3! = Nil {log. Fatal (ERR3);} Fmt. PRINTLN (ret);}

Use of the Go language net package RPC Remote Call

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.