go to achieve RPC httprpc, tcprpc, jsonrpc etc.

一、HTTP RPC

package main

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

type Args struct {
	A, B int
}
type Math int

func (m *Math) Multiply(args *Args, reply *int) error{
	*reply = args.A * args.B
	return nil
}

//
type Quotient struct {
	Quo, Rem int    //商、余数
}

func (m *Math) 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()  {
	math := new(Math)
	rpc.Register(math)  //注册
	rpc.HandleHTTP()

	err := http.ListenAndServe(":1234", nil)
	if err != nil {
		fmt.Println(err.Error())
	}
}
package  main

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

//需要注意的是 两边均需要定义的类型存在
type Args struct {
	A, B int
}
type Quotient struct {
	Quo, Rem int
}

//请求调用rpc
func main()  {
	if len(os.Args) != 2 {  //获取命令行参数 参数个数若不为2
		fmt.Println("Usage: ", os.Args[0], "server")
		os.Exit(1)   //退出
	}
	serverAddr := os.Args[1]
	//走的是tcp  http也是基于tcp的
	client, err := rpc.DialHTTP("tcp", serverAddr+":1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}

	args := Args{17, 8}   //入参
	var reply int  // 返回值
	err = client.Call("Math.Multiply", args,&reply)

	if err != nil {
		log.Fatal("Math error:", err)
	}
	fmt.Printf("Math: %d*%d=%d\n",args.A, args.B, reply)

	var quo Quotient
	err = client.Call("Math.Divide", args, &quo)
	if err != nil {
		log.Fatal("Math error:", err)
	}
	fmt.Printf("Math: %d/%d=%d remainder %d\n", args.A, args.B, quo.Quo, quo.Rem)
}

First start servergo, then start client.go (localhost pass parameters) found the following output:
Here Insert Picture Description
following the changed tcp http RPC RPC actually belong to downgrade, because http is based on the tcp

二、TCP RPC

type Args struct {
	A, B int
}
type Math int

//提供乘法运算
func (m *Math) Multiply(args *Args, reply *int) error{
	*reply = args.A * args.B
	return nil
}

//
type Quotient struct {
	Quo, Rem int    //商、余数
}

//提供除法运算
func (m *Math) 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()  {
	math := new(Math)
	rpc.Register(math)  //注册

	tcpAddr, err := net.ResolveTCPAddr("tcp",":1234") //tcp网络 tcp地址

	if err != nil {
		fmt.Println("Fatal error:", err)
		os.Exit(2)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)  //在指定地址 监听tcp网络请求
	if err != nil {
		fmt.Println("Fatal error:", err)
		os.Exit(2)
	}
	for {
		// 发现一个请求 就会产生一个连接对象
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("conn error")
			continue
		}
		rpc.ServeConn(conn)  //只需要把上面建立连接返回的对象 传进去即可 使用了
		//这就是使用Go 标准库 rpc的好处了
	}
}

The following client.go change only one line:
Here Insert Picture Description
first run go run server.go
then run RUN client.go localhost Go
, as shown below:
Here Insert Picture Description
three, JSON RPC implementation
server.go file only changed to the following two:
Here Insert Picture Description
Here Insert Picture Description

Then client.go file is only changed two lines of code:
Here Insert Picture Description
Here Insert Picture Description

Published 100 original articles · won praise 15 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37767455/article/details/104919014