当前位置:网站首页>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)
}
边栏推荐
- 腾讯云【Hiflow】新时代自动化工具
- torch.roll()
- 数据增强Mixup原理与代码解读
- Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
- VSCode Change Default Terminal 如何修改vscode的默认terminal
- The pit of std::string::find return value
- (11) Metaclass
- Summary of domestic environments supported by SuperMap
- word column notes
- 02 [Development Server Resource Module]
猜你喜欢

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

金仓数据库如何验证安装文件平台正确性

The linear table lookup

基于左序遍历的数据存储实践

CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations

倒计时 2 天|云原生 Meetup 广州站,等你来!

链表的简单描述及代码的简单实现

Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!

Use SuperMap iDesktopX data migration tool to migrate ArcGIS data

Data to enhance Mixup principle and code reading
随机推荐
Is your data safe in this hyperconnected world?
Syntax basics (variables, input and output, expressions and sequential statement completion)
1667. 修复表中的名字
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
Error: Not a signal or slot declaration
Everyone in China said data, you need to focus on core characteristic is what?
QT language file production
Snapback - same tree
倒计时 2 天|云原生 Meetup 广州站,等你来!
How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?
Study Notes-----Left-biased Tree
QT MV\MVC结构
剑指offer专项突击版第20天
人人都在说的数据中台,你需要关注的核心特点是什么?
QT: The Magical QVarient
Principle and Technology of Virtual Memory
shell statement to modify txt file or sh file
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?