golang两种调用rpc的方法
程序员文章站
2022-04-10 12:12:46
本文实例讲述了golang两种调用rpc的方法。分享给大家供大家参考,具体如下:
golang的rpc有两种方法进行调用,一种是rpc例子中给的:
复制代码 代码如下:...
本文实例讲述了golang两种调用rpc的方法。分享给大家供大家参考,具体如下:
golang的rpc有两种方法进行调用,一种是rpc例子中给的:
复制代码 代码如下:
package main
import (
"net/rpc"
"net/http"
"log"
"net"
"time"
)
type args struct {
a, b int
}
type arith int
func (t *arith) multiply(args *args, reply *([]string)) error {
*reply = append(*reply, "test")
return nil
}
func main() {
arith := new(arith)
rpc.register(arith)
rpc.handlehttp()
l, e := net.listen("tcp", ":1234")
if e != nil {
log.fatal("listen error:", e)
}
go http.serve(l, nil)
time.sleep(5 * time.second)
client, err := rpc.dialhttp("tcp", "127.0.0.1" + ":1234")
if err != nil {
log.fatal("dialing:", err)
}
args := &args{7,8}
reply := make([]string, 10)
err = client.call("arith.multiply", args, &reply)
if err != nil {
log.fatal("arith error:", err)
}
log.println(reply)
}
import (
"net/rpc"
"net/http"
"log"
"net"
"time"
)
type args struct {
a, b int
}
type arith int
func (t *arith) multiply(args *args, reply *([]string)) error {
*reply = append(*reply, "test")
return nil
}
func main() {
arith := new(arith)
rpc.register(arith)
rpc.handlehttp()
l, e := net.listen("tcp", ":1234")
if e != nil {
log.fatal("listen error:", e)
}
go http.serve(l, nil)
time.sleep(5 * time.second)
client, err := rpc.dialhttp("tcp", "127.0.0.1" + ":1234")
if err != nil {
log.fatal("dialing:", err)
}
args := &args{7,8}
reply := make([]string, 10)
err = client.call("arith.multiply", args, &reply)
if err != nil {
log.fatal("arith error:", err)
}
log.println(reply)
}
另一种是使用newserver
这种是当rpc已经注册的时候就要使用了另外一种了。即一个server只能在defaultrpc中注册一种类型。
当server使用rpc.newserver的时候,client也需要进行下改动了
复制代码 代码如下:
package main
import (
"net/rpc"
//"net/http"
"log"
"net"
"time"
)
type args struct {
a, b int
}
type arith int
func (t *arith) multiply(args *args, reply *([]string)) error {
*reply = append(*reply, "test")
return nil
}
func main() {
newserver := rpc.newserver()
newserver.register(new(arith))
l, e := net.listen("tcp", "127.0.0.1:1234") // any available address
if e != nil {
log.fatalf("net.listen tcp :0: %v", e)
}
go newserver.accept(l)
newserver.handlehttp("/foo", "/bar")
time.sleep(2 * time.second)
address, err := net.resolvetcpaddr("tcp", "127.0.0.1:1234")
if err != nil {
panic(err)
}
conn, _ := net.dialtcp("tcp", nil, address)
defer conn.close()
client := rpc.newclient(conn)
defer client.close()
args := &args{7,8}
reply := make([]string, 10)
err = client.call("arith.multiply", args, &reply)
if err != nil {
log.fatal("arith error:", err)
}
log.println(reply)
}
import (
"net/rpc"
//"net/http"
"log"
"net"
"time"
)
type args struct {
a, b int
}
type arith int
func (t *arith) multiply(args *args, reply *([]string)) error {
*reply = append(*reply, "test")
return nil
}
func main() {
newserver := rpc.newserver()
newserver.register(new(arith))
l, e := net.listen("tcp", "127.0.0.1:1234") // any available address
if e != nil {
log.fatalf("net.listen tcp :0: %v", e)
}
go newserver.accept(l)
newserver.handlehttp("/foo", "/bar")
time.sleep(2 * time.second)
address, err := net.resolvetcpaddr("tcp", "127.0.0.1:1234")
if err != nil {
panic(err)
}
conn, _ := net.dialtcp("tcp", nil, address)
defer conn.close()
client := rpc.newclient(conn)
defer client.close()
args := &args{7,8}
reply := make([]string, 10)
err = client.call("arith.multiply", args, &reply)
if err != nil {
log.fatal("arith error:", err)
}
log.println(reply)
}
第二个例子中的
复制代码 代码如下:
newserver.handlehttp("/foo", "/bar")
可以任意设置,第一个例子其实是设置了默认的两个
这里也顺便将reply作为[]slice的例子给演示了下
希望本文所述对大家go语言程序设计有所帮助。
上一篇: Go语言基础学习教程
下一篇: Go语言接口定义与用法示例