当前位置:网站首页>rpc-remote procedure call demo
rpc-remote procedure call demo
2022-08-05 03:12:00 【ALEX_CYL】
RPC:Remote Procedure Call
rpcrequirements for functions:
1.字母大写,i.e. the requirement ispublic
2.Contains two parameters of exportable type,The parameter returned to the client must be of type pointer
3.函数必须有一个返回值 error
server demo:
package main
import (
"errors"
"fmt"
"net/http"
"net/rpc"
)
type Args struct {
A, B int
}
type Query struct {
X, Y int
}
type Last int
func (t *Last) Multiply(args Args, reply *int) error {
*reply = args.A * args.B
fmt.Println(*reply, "Multiply exec")
if *reply == 0 {
return fmt.Errorf("%vThere are zero values in ", args)
}
return nil
}
func (t *Last) Divide(args Args, query *Query) error {
if args.B == 0 {
return errors.New("分子args.B = 0")
}
query.X = args.A / args.B
query.Y = args.A % args.B
fmt.Println(*query, "Divide exec")
return nil
}
func main() {
La := new(Last)
fmt.Println("la", La)
// 类型注册
// Register在DefaultServerPublish methods to receive types in .
rpc.Register(La)
// HandleHTTP在DefaultRPCPath上为RPC消息向DefaultServer注册了一个HTTP处理器,
// 在DefaultDebugPathA debug handler is registered on .
// 仍然需要调用http.Serve()
rpc.HandleHTTP() //设定http类型
err := http.ListenAndServe("127.0.0.1:8080", nil)
defer func() {
err := recover()
if err != nil {
fmt.Println("panic() happend,err:", err)
}
}()
if err != nil {
panic(err)
}
}
client demo:
package main
import (
"fmt"
"net/rpc"
)
type Args struct {
A, B int
}
type Query struct {
X, Y int
}
func main() {
serverIp := "127.0.0.1:8080"
client, err := rpc.DialHTTP("tcp", serverIp)
if err != nil {
fmt.Println(err)
}
i1 := 12
i2 := 13
args := Args{
i1, i2}
var reply int
err = client.Call("Last.Multiply", &args, &reply)
if err != nil {
fmt.Println(err)
}
fmt.Println(args.A, "*", args.B, "=", reply)
var query Query
err = client.Call("Last.Divide", args, &query)
if err != nil {
fmt.Println(err)
}
fmt.Println(args.A, "/", args.B, "=", query.X)
fmt.Println(args.A, "%", args.B, "=", query.Y)
}
边栏推荐
- Data storage practice based on left-order traversal
- 1527. Patients suffering from a disease
- (十一)元类
- 开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
- CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
- Turn: Charles Handy: Who you are is more important than what you do
- mysql can't Execute, please solve it
- Physical backup issues caused by soft links
- 腾讯云【Hiflow】新时代自动化工具
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
猜你喜欢

The usage of try...catch and finally in js

Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium

北斗三号短报文终端露天矿山高边坡监测方案

On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion

J9 Digital Currency: What is the creator economy of web3?

Cloud Native (32) | Introduction to Platform Storage System in Kubernetes

QT MV\MVC structure

静态方法获取配置文件数据

【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can

腾讯云【Hiflow】新时代自动化工具
随机推荐
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
用Unity发布APP到Hololens2无坑教程
Is your data safe in this hyperconnected world?
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
mysql can't Execute, please solve it
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
In 2022, you still can't "low code"?Data science can also play with Low-Code!
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
Syntax basics (variables, input and output, expressions and sequential statements)
1527. 患某种疾病的患者
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
In 2022, you still can't "low code"?Data science can also play with Low-Code!
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
语法基础(变量、输入输出、表达式与顺序语句)
Lexicon - the maximum depth of a binary tree
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
Linux下常见的开源数据库,你知道几个?
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
QT MV\MVC结构
public static <T> List<T> asList(T... a) 原型是怎么回事?