In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍Go语言中net包RPC远程调用方式有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
一、服务端
在代码中,启动了三个服务
package mainimport ( "log" "net" "net/http" "net/rpc" "net/rpc/jsonrpc" "sync") //go对RPC的支持,支持三个级别:TCP、HTTP、JSONRPC//go的RPC只支持GO开发的服务器与客户端之间的交互,因为采用了gob编码 //注意字段必须是导出type Params struct { Width, Height int} type Rect struct{} //函数必须是导出的//必须有两个导出类型参数//第一个参数是接收参数//第二个参数是返回给客户端参数,必须是指针类型//函数还要有一个返回值errorfunc (r *Rect) Area(p Params, ret *int) error { *ret = p.Width * p.Height return nil} func (r *Rect) Perimeter(p Params, ret *int) error { *ret = (p.Width + p.Height) * 2 return nil} func main() { rect := new(Rect) //注册一个rect服务 rpc.Register(rect) var wg sync.WaitGroup wg.Add(3) go func() { //把服务处理绑定到http协议上 rpc.HandleHTTP() err := http.ListenAndServe(":8080", nil) wg.Wait() if err != nil { log.Fatal(err) defer wg.Done() } }() log.Println("http rpc service start success addr:8080") go func() { tcpaddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8081") tcplisten, err := net.ListenTCP("tcp", tcpaddr) if err != nil { log.Fatal(err) defer wg.Done() } for { conn, err3 := tcplisten.Accept() if err3 != nil { continue } go rpc.ServeConn(conn) } }() log.Println("tcp rpc service start success addr:8081") go func() { tcpaddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8082") tcplisten, err := net.ListenTCP("tcp", tcpaddr) if err != nil { log.Fatal(err) defer wg.Done() } for { conn, err3 := tcplisten.Accept() if err3 != nil { continue } go jsonrpc.ServeConn(conn) } }() log.Println("tcp json-rpc service start success addr:8082") wg.Wait()}二、http客户端package mainimport ( "net/rpc" "log" "fmt")type Params struct { Width, Height int}func main() { //连接远程rpc服务 rpc, err := rpc.DialHTTP("tcp", "127.0.0.1:8080") if err != nil { log.Fatal(err) } ret := 0; //调用远程方法 //注意第三个参数是指针类型 err2 := rpc.Call("Rect.Area", Params{50, 100}, &ret) if err2 != nil { log.Fatal(err2) } fmt.Println(ret) err3 := rpc.Call("Rect.Perimeter", Params{50, 100}, &ret) if err3 != nil { log.Fatal(err3) } fmt.Println(ret)}三、TCP客户端package main import ( "net/rpc" "fmt" "log") type Params struct { Width, Height int} func main() { //连接远程rpc服务 //这里使用Dial,http方式使用DialHTTP,其他代码都一样 rpc, err := rpc.Dial("tcp", "127.0.0.1:8081") if err != nil { log.Fatal(err) } ret := 0 //调用远程方法 //注意第三个参数是指针类型 err2 := rpc.Call("Rect.Area", Params{50, 100}, &ret) if err2 != nil { log.Fatal(err2) } fmt.Println(ret) err3 := rpc.Call("Rect.Perimeter", Params{50, 100}, &ret) if err3 != nil { log.Fatal(err3) } fmt.Println(ret)}四、json客户端package main import ( "fmt" "log" "net/rpc/jsonrpc") type Params struct { Width, Height int} func main() { //连接远程rpc服务 rpc, err := jsonrpc.Dial("tcp", "127.0.0.1:8082") if err != nil { log.Fatal(err) } ret := 0 //调用远程方法 //注意第三个参数是指针类型 err2 := rpc.Call("Rect.Area", Params{150, 100}, &ret) if err2 != nil { log.Fatal(err2) } fmt.Println(ret) err3 := rpc.Call("Rect.Perimeter", Params{150, 100}, &ret) if err3 != nil { log.Fatal(err3) } fmt.Println(ret)}五、运行结果
The above is "Go language net package RPC remote call what" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 228
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.