当前位置:网站首页>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)
}
边栏推荐
- 通过模拟Vite一起深入其工作原理
- 1484. Sell Products by Date
- 解决端口占用问题 Port xxxx was already in use
- The usage of try...catch and finally in js
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
- How OpenGL works
- How to Add Category-Specific Widgets in WordPress
- AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
- torch.roll()
- private封装
猜你喜欢
随机推荐
1667. 修复表中的名字
Object.defineProperty monitors data changes in real time and updates the page
How to transfer a single node of Youxuan database to a cluster
Tencent Cloud [Hiflow] New Era Automation Tool
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
静态方法获取配置文件数据
Bubble Sort and Quick Sort
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
沃谈小知识 |“远程透传”那点事儿
How OpenGL works
通过模拟Vite一起深入其工作原理
QStyle平台风格
百日刷题计划 ———— DAY2
Never put off till tomorrow what you can put - house lease management system based on the SSM
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
汉字转拼音
[Solved] Unity Coroutine coroutine is not executed effectively
627. 变更性别
QT language file production
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!








