{"id":6385,"date":"2024-07-18T20:56:48","date_gmt":"2024-07-18T12:56:48","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6385"},"modified":"2024-07-18T20:56:48","modified_gmt":"2024-07-18T12:56:48","slug":"go-rpc-%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6385","title":{"rendered":"Go RPC \u673a\u5236"},"content":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0 RPC\uff08Remote Procedure Call\uff0c\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u901a\u4fe1\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 <code>net\/rpc<\/code> \u5305\u3002\u8fd9\u4e2a\u5305\u63d0\u4f9b\u4e86\u57fa\u672c\u7684 RPC \u529f\u80fd\uff0c\u5141\u8bb8\u4f60\u5728 Go \u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u8fdb\u884c\u8fdc\u7a0b\u8c03\u7528\u3002<\/p>\n<h3>\u4f7f\u7528 <code>net\/rpc<\/code> \u5b9e\u73b0 RPC<\/h3>\n<h4>1. \u5b9a\u4e49\u670d\u52a1\u548c\u65b9\u6cd5<\/h4>\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a RPC \u670d\u52a1\u548c\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5fc5\u987b\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u65b9\u6cd5\u7684\u7c7b\u578b\u662f\u5bfc\u51fa\u7684\u3002<\/li>\n<li>\u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8f93\u5165\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8f93\u51fa\u53c2\u6570\uff0c\u90fd\u662f\u6307\u9488\u7c7b\u578b\u3002<\/li>\n<li>\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u662f <code>error<\/code>\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u548c\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-go\">\/\/ \u5b9a\u4e49\u670d\u52a1\u7c7b\u578b\ntype Arith struct {}\n\n\/\/ \u5b9a\u4e49\u670d\u52a1\u65b9\u6cd5\nfunc (a *Arith) Multiply(args *Args, reply *int) error {\n    *reply = args.A * args.B\n    return nil\n}\n\n\/\/ \u5b9a\u4e49\u8f93\u5165\u53c2\u6570\u7ed3\u6784\u4f53\ntype Args struct {\n    A, B int\n}<\/code><\/pre>\n<h4>2. \u6ce8\u518c\u670d\u52a1<\/h4>\n<p>\u5c06\u670d\u52a1\u7c7b\u578b\u6ce8\u518c\u5230 RPC \u670d\u52a1\u4e2d\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u8fdc\u7a0b\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/ \u521b\u5efa Arith \u5b9e\u4f8b\n    arith := new(Arith)\n\n    \/\/ \u6ce8\u518c Arith \u670d\u52a1\n    rpc.Register(arith)\n\n    \/\/ \u542f\u52a8 TCP \u670d\u52a1\uff0c\u76d1\u542c\u7aef\u53e3\n    listener, err := net.Listen(&quot;tcp&quot;, &quot;:1234&quot;)\n    if err != nil {\n        log.Fatalf(&quot;Error listening: %v&quot;, err)\n    }\n    defer listener.Close()\n\n    log.Println(&quot;RPC server is listening on port 1234&quot;)\n\n    \/\/ \u63a5\u6536\u8fde\u63a5\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u542f\u52a8\u4e00\u4e2a\u65b0\u7684 goroutine \u5904\u7406\u8bf7\u6c42\n    for {\n        conn, err := listener.Accept()\n        if err != nil {\n            log.Fatalf(&quot;Error accepting connection: %v&quot;, err)\n        }\n        go rpc.ServeConn(conn)\n    }\n}<\/code><\/pre>\n<h4>3. \u5ba2\u6237\u7aef\u8c03\u7528<\/h4>\n<p>\u7f16\u5199\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u8c03\u7528\u8fdc\u7a0b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/ \u8fde\u63a5 RPC \u670d\u52a1\n    client, err := rpc.Dial(&quot;tcp&quot;, &quot;localhost:1234&quot;)\n    if err != nil {\n        log.Fatalf(&quot;Error connecting to RPC server: %v&quot;, err)\n    }\n    defer client.Close()\n\n    \/\/ \u51c6\u5907\u8f93\u5165\u53c2\u6570\n    args := &amp;Args{7, 8}\n    var reply int\n\n    \/\/ \u8c03\u7528\u8fdc\u7a0b\u65b9\u6cd5 Multiply\n    err = client.Call(&quot;Arith.Multiply&quot;, args, &amp;reply)\n    if err != nil {\n        log.Fatalf(&quot;Error calling RPC method: %v&quot;, err)\n    }\n\n    log.Printf(&quot;Arith: %d * %d = %d\\n&quot;, args.A, args.B, reply)\n}<\/code><\/pre>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u5728\u5b9a\u4e49\u670d\u52a1\u65b9\u6cd5\u65f6\uff0c\u786e\u4fdd\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u7c7b\u578b\u548c\u987a\u5e8f\u4e0e\u670d\u52a1\u7aef\u4e00\u81f4\u3002<\/li>\n<li>\u5728\u5ba2\u6237\u7aef\u8c03\u7528\u65f6\uff0c\u63d0\u4f9b\u6b63\u786e\u7684\u65b9\u6cd5\u540d\u548c\u53c2\u6570\uff0c\u4f7f\u7528\u6307\u9488\u4f20\u9012\u53c2\u6570\u548c\u83b7\u53d6\u8fd4\u56de\u503c\u3002<\/li>\n<li><code>net\/rpc<\/code> \u5305\u57fa\u4e8e Gob \u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u6709\u9650\uff0c\u5982\u6709\u9700\u8981\u53ef\u4ee5\u4f7f\u7528 JSON \u6216\u5176\u4ed6\u5e8f\u5217\u5316\u683c\u5f0f\u8fdb\u884c\u6269\u5c55\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528 <code>net\/rpc<\/code> \u5305\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u7b80\u5355\u7684 RPC \u901a\u4fe1\uff0c\u9002\u5408\u4e8e\u9700\u8981\u57fa\u672c\u8fdc\u7a0b\u8c03\u7528\u529f\u80fd\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u6216\u9ad8\u6027\u80fd\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982 gRPC\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\u548c\u66f4\u9ad8\u7ea7\u7684\u7279\u6027\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0 RPC\uff08Remote Procedure Call\uff0c\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u901a\u4fe1\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6385\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[131],"class_list":["post-6385","post","type-post","status-publish","format-standard","hentry","category-123","tag-rpc"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6385"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6385\/revisions"}],"predecessor-version":[{"id":6387,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6385\/revisions\/6387"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}