当前位置:网站首页>rpc-remote procedure call demo
rpc-remote procedure call demo
2022-08-05 02:57:00 【ALEX_CYL】
RPC:Remote Procedure Call
rpc对函数的要求:
1.字母大写,即要求是public
2.含有两个可导出类型的参数,返回给客户端的参数必须是指针类型
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("%v中存在零值", 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在DefaultServer中发布接接收类型的方法。
rpc.Register(La)
// HandleHTTP在DefaultRPCPath上为RPC消息向DefaultServer注册了一个HTTP处理器,
// 在DefaultDebugPath上注册了一个调试处理器。
// 仍然需要调用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)
}
边栏推荐
- 采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
- 627. Change of gender
- 【 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
- Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
- Tencent Cloud [Hiflow] New Era Automation Tool
- VSCode Change Default Terminal how to modify the Default Terminal VSCode
- dmp (dump) dump file
- DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
- The pit of std::string::find return value
- How OpenGL works
猜你喜欢

word column notes

【软件测试】自动化测试之unittest框架

Dynamic management of massive service instances

【 2 】 OpenCV image processing: basic knowledge of OpenCV

使用二维码传输文件的小工具 - QFileTrans 1.2.0.1

线上MySQL的自增id用尽怎么办?
![Tencent Cloud [Hiflow] New Era Automation Tool](/img/ac/5c61424f22cd9fed74dcd529fdb6a4.png)
Tencent Cloud [Hiflow] New Era Automation Tool

The Tanabata copywriting you want has been sorted out for you!

甘特图来啦,项目管理神器,模板直接用

Matlab画图3
随机推荐
【 2 】 OpenCV image processing: basic knowledge of OpenCV
(11) Metaclass
1873. 计算特殊奖金
Details such as compiling pretreatment
Error: Not a signal or slot declaration
The 22-07-31 weeks summary
语法基础(变量、输入输出、表达式与顺序语句)
软链接引发的物理备份问题
Pisanix v0.2.0 released | Added support for dynamic read-write separation
Lexicon - the maximum depth of a binary tree
甘特图来啦,项目管理神器,模板直接用
Flink 1.15.1 Cluster Construction (StandaloneSession)
1484. Sell Products by Date
Gantt chart is here, project management artifact, template is used directly
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
Open Source License Description LGPL
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
C student management system Insert the student node at the specified location
Distributed systems revisited: there will never be a perfect consistency scheme...
汉字转拼音