rpc 框架
基于 http 调用
type.go
server.go
client.go
type.go
server.go
client.go
package common
import "errors"
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith struct{}
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
package main
import (
"framework/book_src/golang/stdlib/rpc/demo/t00/common"
"net"
"net/http"
"net/rpc"
)
func main() {
arith := new(common.Arith)
err := rpc.Register(arith)
if err != nil {
panic(err)
}
rpc.HandleHTTP()
l, err := net.Listen("tcp", ":1234")
if err != nil {
panic(err)
}
defer l.Close()
panic(http.Serve(l, nil))
}
package main
import (
"fmt"
"framework/book_src/golang/stdlib/rpc/demo/t00/common"
"net/rpc"
"os"
)
func synchRpc(client *rpc.Client) (reply int, err error) {
args := &common.Args{A: 7, B: 8}
err = client.Call("Arith.Multiply", args, &reply)
return reply, err
}
func asyncRpc(client *rpc.Client) (reply common.Quotient, err error) {
args := &common.Args{A: 9, B: 8}
divCall := client.Go("Arith.Divide", args, &reply, nil)
replyCall := <-divCall.Done
return reply, replyCall.Error
}
func main() {
mode := "sync"
if len(os.Args) > 1 {
mode = os.Args[1]
}
client, err := rpc.DialHTTP("tcp", ":1234")
if err != nil {
panic(err)
}
var reply any
switch mode {
case "sync":
reply, err = synchRpc(client)
if err != nil {
panic(err)
}
case "async":
reply, err = asyncRpc(client)
if err != nil {
panic(err)
}
}
fmt.Printf("reply:%v\n", reply)
}
package common
import "errors"
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith struct{}
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
package main
import (
"framework/book_src/golang/stdlib/rpc/demo/t00/common"
"net"
"net/rpc"
)
func main() {
arith := new(common.Arith)
err := rpc.Register(arith)
if err != nil {
panic(err)
}
rpc.HandleHTTP()
l, err := net.Listen("tcp", ":1234")
if err != nil {
panic(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go rpc.ServeConn(conn)
}
}
package main
import (
"fmt"
"framework/book_src/golang/stdlib/rpc/demo/t00/common"
"net/rpc"
"os"
)
func synchRpc(client *rpc.Client) (reply int, err error) {
args := &common.Args{A: 7, B: 8}
err = client.Call("Arith.Multiply", args, &reply)
return reply, err
}
func asyncRpc(client *rpc.Client) (reply common.Quotient, err error) {
args := &common.Args{A: 9, B: 8}
divCall := client.Go("Arith.Divide", args, &reply, nil)
replyCall := <-divCall.Done
return reply, replyCall.Error
}
func main() {
mode := "sync"
if len(os.Args) > 1 {
mode = os.Args[1]
}
client, err := rpc.Dial("tcp", ":1234")
if err != nil {
panic(err)
}
var reply any
switch mode {
case "sync":
reply, err = synchRpc(client)
if err != nil {
panic(err)
}
case "async":
reply, err = asyncRpc(client)
if err != nil {
panic(err)
}
}
fmt.Printf("reply:%v\n", reply)
}