我正在尝试使用json-rpc
软件包在我的应用程序中创建gorilla/rpc
服务器。
这是我的代码
func (server *Server) ListenRPC(port string) {
fmt.Println(fmt.Sprintf(MessageListeningRPCServer, port))
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
s.RegisterService(new(RPCServer), "")
r := mux.NewRouter()
r.Handle("/", s)
http.ListenAndServe(fmt.Sprintf(portFormatter, port), r)
}
type RPCServer struct {
}
type Response string
type Args struct {
A, B int
}
func (server *RPCServer) Abc(r *http.Request, args *Args, response *Response) error {
*response = "asdasdasd"
return nil
}
因此在cmd中,我运行curl
命令如下
curl -H 'Content-Type: application/json' -X POST -d '{"method":"RPCServer.Abc","params":[{"a":3}],"id":1}' http://localhost:8090
并得到如下错误
rpc: can't find service "RPCServer.Abc"