gob: 未为接口注册类型:fasthbbftv1.BroadcastMessage

huangapple go评论90阅读模式
英文:

gob: type not registered for interface: fasthbbftv1.BroadcastMessage

问题

我的代码组织结构如下:

package_name/
    server/
        rpc_server.go
    client_test.go
  1. 我注册了一个RPC服务器来接收其他节点的消息。
type RPCMessageArgs struct {
	Operation string
	Info      Message
}

type Message struct {
	NodeID  uint64
	Payload interface{}
}

func init() {
	gob.Register(BroadcastMessage{})
}

func main() {
    // 启动RPC服务器
}
  1. 我使用命令go run rpc_server.go在server文件夹中启动了RPC服务器。
  2. 我在client_test.go中的测试代码如下:
package package_name

import (
	"encoding/gob"
	"log"
	"net/rpc"
	"strconv"
	"testing"
)

type BroadcastMessage struct {
	Payload interface{}
}

func TestClient(t *testing.T) {
	gob.Register(BroadcastMessage{})
	args := &RPCMessageArgs{}
	args.Info.NodeID = 3
	args.Info.Payload = &BroadcastMessage{}
	reply := &RPCMessageReply{}
	client, err := rpc.Dial("tcp", ":800"+strconv.Itoa(1))
	if err != nil {
		log.Fatal("dialing:", err)
	}
	err = client.Call("RPCServer.RemoteCall", args, reply)
	if err != nil {
		log.Fatal("call error:", err)
	}
}
  1. 我遇到了以下错误:
call error: gob: name not registered for interface: "package_name.BroadcastMessage"
英文:

My code organization structure is like this

package_name/
    server/
        rpc_server.go
    client_test.go
  1. I register an RPC server to receive messages from other nodes.

type RPCMessageArgs struct {
	Operation string
	Info      Message
}

type Message struct {
	NodeID  uint64
	Payload interface{}
}

func init() {
	gob.Register(BroadcastMessage{})
}

func main() {
    // start rpc server
}

  1. I start my RPC server in the server folder with the command go run rpc_server.go.
  2. My test code in the client_test.go is as follows
package package_name

import (
	"encoding/gob"
	"log"
	"net/rpc"
	"strconv"
	"testing"
)

type BroadcastMessage struct {
	Payload interface{}
}


func TestClient(t *testing.T) {
	gob.Register(BroadcastMessage{})
	args := &RPCMessageArgs{}
	args.Info.NodeID = 3
	args.Info.Payload = &BroadcastMessage{}
	reply := &RPCMessageReply{}
	client, err := rpc.Dial("tcp", ":800"+strconv.Itoa(1))
	if err != nil {
		log.Fatal("dialing: ", err)
	}
	err = client.Call("RPCServer.RemoteCall", args, reply)
	if err != nil {
		log.Fatal("call error: ", err)
	}
}

  1. I get this error
call error: gob: name not registered for interface: "package_name.BroadcastMessage"

答案1

得分: 0

我犯了一个非常低级的错误。我在两个文件中定义了两个type BroadcastMessage,一个在rpc_server.go中,一个在client_go.go中。我应该在一个包中定义这些结构,然后让这两个文件调用这些结构。
改变后,我的代码结构如下:

package_name/
    args/
        rpc_args.go
    server/
        rpc_server.go
    client_test.go

rpc_args.go文件中定义相关结构,在其他文件中导入args包,然后在rpc_server.goclient_test.go中调用gob.Register()命令以正常运行。

英文:

I made a very low-level mistake. I defined two type BroadcastMessage in two files, one in rpc_server.go and one in client_go.go. I should define these structures in a package, and then let these two files call these structures.
After the change, my code structure is as follows:

package_name/
    args/
        rpc_args.go
    server/
        rpc_server.go
    client_test.go

Define the relevant structure in the rpc_args.go file, import the args package in other files, and then call the gob.Register() command in rpc_server.go and client_test.go to run normally.

huangapple
  • 本文由 发表于 2021年7月9日 20:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68317156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定