golang调用rpc golang两种调用rpc的方法
轩脉刃 人气:0想了解golang两种调用rpc的方法的相关内容吗,轩脉刃在本文为您仔细讲解golang调用rpc的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:golang,调用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语言程序设计有所帮助。
加载全部内容