当前位置:网站首页>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)
}
边栏推荐
- Why is the pca component not associated
- dmp (dump) dump file
- 倒计时 2 天|云原生 Meetup 广州站,等你来!
- Syntax basics (variables, input and output, expressions and sequential statements)
- 22-07-31周总结
- Chinese characters to Pinyin
- .NET Application -- Helloworld (C#)
- UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
- Programmer's Tanabata Romantic Moment
- 论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
猜你喜欢
【已解决】Unity Coroutinue 协程未有效执行的问题
【软件测试】自动化测试之unittest框架
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
用CH341A烧录外挂Flash (W25Q16JV)
【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】
【 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
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
Talking about data security governance and privacy computing
人人都在说的数据中台,你需要关注的核心特点是什么?
随机推荐
1873. The special bonus calculation
One hundred - day plan -- -- DAY2 brush
Question about #sql shell#, how to solve it?
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
Physical backup issues caused by soft links
Lexicon - the maximum depth of a binary tree
大像素全景制作完成后,推广方式有哪些?
J9 Digital Currency: What is the creator economy of web3?
Details such as compiling pretreatment
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
How to sort multiple fields and multiple values in sql statement
Simple description of linked list and simple implementation of code
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
ASP.NET application--Hello World
剑指Offer--找出数组中重复的数字(三种解法)
QT:神奇QVarient
.NET应用程序--Helloworld(C#)
word分栏小记
Multithreading (2)