当前位置:网站首页>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)
}
边栏推荐
- Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
- Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
- Solve connect: The requested address is not valid in its context
- How to simulate the background API call scene, very detailed!
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
- 1873. The special bonus calculation
- Syntax basics (variables, input and output, expressions and sequential statements)
- Error: Not a signal or slot declaration
- Lexicon - the maximum depth of a binary tree
- Hash table lookup (hash table)
猜你喜欢
dmp (dump) dump file
shell语句修改txt文件或者sh文件
The design idea of DMicro, the Go microservice development framework
北斗三号短报文终端露天矿山高边坡监测方案
J9 Digital Currency: What is the creator economy of web3?
Data storage practice based on left-order traversal
word column notes
虚拟内存原理与技术
沃谈小知识 |“远程透传”那点事儿
Everyone in China said data, you need to focus on core characteristic is what?
随机推荐
Matlab drawing 3
QStyle platform style
正则表达式,匹配中间的某一段字符串
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
Syntax basics (variables, input and output, expressions and sequential statements)
mysql没法Execute 大拿们求解
2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
word column notes
1873. The special bonus calculation
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
C student management system Insert the student node at the specified location
dmp(dump)转储文件
Tencent Cloud [Hiflow] New Era Automation Tool
UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
02 [Development Server Resource Module]
The pit of std::string::find return value
(11) Metaclass
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
Syntax basics (variables, input and output, expressions and sequential statements)
View handler stepping record