golang http rpc

发布时间 2023-11-12 15:48:36作者: 过去的我

server 端:

package main

import (
	"errors"
	"log"
	"net"
	"net/http"
	"net/rpc"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

// 定义type
type Arith int

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
}

func main() {

	arith := new(Arith)
	err := rpc.Register(arith)
	if err != nil {
		log.Fatal(err)
	}
	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":1234")
	if err != nil {
		log.Fatal("listen error:", err)
	}
	log.Fatal(http.Serve(l, nil))
}

服务器端还是做几件事

  1. 定义类型,这里是Arith, 是一个type ,

  2. 定义func,有下面的规则
    Only methods that satisfy these criteria will be made available for remote access; other methods will be ignored:

    the method's type is exported.
    the method is exported.
    the method has two arguments, both exported (or builtin) types.
    the method's second argument is a pointer.
    the method has return type error.
    2个参数,第一个一般建一个Args结构体的类型做为输入,第二个是指针类型,方法返回error 。如果不正常,会被忽略。
    函数一般定义成
    func (t *T) MethodName(argType T1, replyType *T2) error
    这套东西还是有一些规范,需要安装他的规则走。

  3. 然后就是rpc.Register(type)

client:

package main

import (
	"fmt"
	"log"
	"net/rpc"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

func main() {
	client, err := rpc.DialHTTP("tcp", "localhost"+":1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}
	// Synchronous call
	args := &Args{12, 8}
	var reply int
	// core
	err = client.Call("Arith.Multiply", args, &reply)
	if err != nil {
		log.Fatal("arith error:", err)
	}
	// 收到请求,同步的
	fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
	// 除法
	var quo Quotient
	err = client.Call("Arith.Divide", args, &quo)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Arith: %d / %d=%d,%d\n", args.A, args.B, quo.Quo, quo.Rem)
}

https://pkg.go.dev/net/rpc