golang中rpc调用

server

 package main
    import (
    "net/http"
    "net/rpc"
    "net"
    "github.com/astaxie/beego"
    "io"
    )
    
    type Wilson string;
    func (this *Wilson)Getinfo(argType string, replyType *string) error {
    	beego.Info(argType)
    	*replyType ="helllo ...." +string(argType)
    	return nil
    }
    func main() {
    	http.HandleFunc("/Wilson",pandatext)
    
    	pd :=new(Wilson)
    	//注册服务
    	rpc.Register(pd)
    	rpc.HandleHTTP()
    	ln , err :=net.Listen("tcp","127.0.0.1:8080")
    	if err != nil{
    		beego.Info("网络连接失败")
    	}
    	beego.Info("listen ....8080")
    	http.Serve(ln,nil)
    }
    func pandatext(w http.ResponseWriter, r *http.Request) {
    	io.WriteString(w,"Wilson")
    }

client

package main
import (
"net/rpc"
"github.com/astaxie/beego"
	"fmt"
)
func main() {
	cli,err := rpc.DialHTTP("tcp","127.0.0.1:8080")
	if err !=nil {
		beego.Info("网络连接失败")
	}
	var val string
	err =cli.Call("Wilson.Getinfo","log....",&val)
	if err!=nil{
		fmt.Println(err)
	}
	beego.Info("返回结果",val)
}

golang中rpc调用_第1张图片

你可能感兴趣的:(golang)