当前位置:网站首页>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)
}
边栏推荐
- 静态方法获取配置文件数据
- Talking about data security governance and privacy computing
- 使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
- Syntax basics (variables, input and output, expressions and sequential statement completion)
- How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?
- Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
- Common open source databases under Linux, how many do you know?
- How to transfer a single node of Youxuan database to a cluster
- 【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
- sql server installation prompts that the username does not exist
猜你喜欢

21 Days Learning Challenge (2) Use of Graphical Device Trees

A small tool to transfer files using QR code - QFileTrans 1.2.0.1

Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors

开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..

The linear table lookup

QT language file production
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)

dmp(dump)转储文件

Cloud Native (32) | Introduction to Platform Storage System in Kubernetes

How to Add Category-Specific Widgets in WordPress
随机推荐
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
Step by step how to perform data risk assessment
Question about #sql shell#, how to solve it?
torch.roll()
How Jin Cang database correctness verification platform installation file
高项 02 信息系统项目管理基础
Never put off till tomorrow what you can put - house lease management system based on the SSM
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
软链接引发的物理备份问题
Tencent Cloud [Hiflow] New Era Automation Tool
How to transfer a single node of Youxuan database to a cluster
How OpenGL works
627. Change of gender
百日刷题计划 ———— DAY2
2022-08-04 第六小组 瞒春 学习笔记
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
通过模拟Vite一起深入其工作原理
Talking about data security governance and privacy computing
(11) Metaclass