当前位置:网站首页>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)
}
边栏推荐
- 语法基础(变量、输入输出、表达式与顺序语句完成情况)
- 【已解决】Unity Coroutinue 协程未有效执行的问题
- 【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
- Turn: Charles Handy: Who you are is more important than what you do
- rpc-remote procedure call demo
- 为什么pca分量没有关联
- Data to enhance Mixup principle and code reading
- private封装
- 高项 02 信息系统项目管理基础
- STM32 uses stm32cubemx LL library series tutorial
猜你喜欢
Data storage practice based on left-order traversal
Talking about data security governance and privacy computing
dmp(dump)转储文件
【 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
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
Is your data safe in this hyperconnected world?
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
基于生长的棋盘格角点检测方法
How OpenGL works
随机推荐
dmp(dump)转储文件
STM32 uses stm32cubemx LL library series tutorial
word分栏小记
Open Source License Description LGPL
2022-08-04 The sixth group, hidden from spring, study notes
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
tree table lookup
public static <T> List<T> asList(T... a) 原型是怎么回事?
冒泡排序与快速排序
How OpenGL works
QT MV\MVC结构
1527. 患某种疾病的患者
sql server installation prompts that the username does not exist
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
QStyle平台风格
905. 区间选点
北斗三号短报文终端露天矿山高边坡监测方案
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
(11) Metaclass