当前位置:网站首页>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)
}
边栏推荐
- torch.roll()
- Common open source databases under Linux, how many do you know?
- 腾讯云【Hiflow】新时代自动化工具
- The usage of try...catch and finally in js
- Hash table lookup (hash table)
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
- Open Source License Description LGPL
- The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
- Physical backup issues caused by soft links
- Lexicon - the maximum depth of a binary tree
猜你喜欢
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
毕设-基于SSM房屋租赁管理系统
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
public static <T> List<T> asList(T... a) 原型是怎么回事?
Bubble Sort and Quick Sort
How to Add Category-Specific Widgets in WordPress
The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
告白数字化转型时代,时速云镌刻价值新起点
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
随机推荐
北斗三号短报文终端露天矿山高边坡监测方案
Simple description of linked list and simple implementation of code
High Item 02 Information System Project Management Fundamentals
你要的七夕文案,已为您整理好!
dmp (dump) dump file
627. 变更性别
Dynamic management of massive service instances
Principle and Technology of Virtual Memory
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
Native js realizes the effect of selecting and canceling all the multi-select boxes
十五. 实战——mysql建库建表 字符集 和 排序规则
大像素全景制作完成后,推广方式有哪些?
mysql can't Execute, please solve it
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
Details such as compiling pretreatment
Talking about data security governance and privacy computing
数据增强Mixup原理与代码解读
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
开源协议说明LGPL
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data