{"id":83941,"date":"2020-05-12T05:11:01","date_gmt":"2020-05-11T21:11:01","guid":{"rendered":"http:\/\/4563.org\/?p=83941"},"modified":"2020-05-12T05:11:01","modified_gmt":"2020-05-11T21:11:01","slug":"%e6%8e%a8%e8%8d%90%e4%b8%80%e6%ac%be-golang-%e5%be%ae%e6%9c%8d%e5%8a%a1%e9%9b%86%e6%88%90%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=83941","title":{"rendered":"\u63a8\u8350\u4e00\u6b3e golang \u5fae\u670d\u52a1\u96c6\u6210\u6846\u67b6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u63a8\u8350\u4e00\u6b3e golang \u5fae\u670d\u52a1\u96c6\u6210\u6846\u67b6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>golang \u5fae\u670d\u52a1\u96c6\u6210\u6846\u67b6<\/h3>\n<h1>gmsec<\/h1>\n<p> <\/p>\n<h3>\u7279\u70b9<\/h3>\n<ul>\n<li>\u6253\u901a grpc + gin\uff0c\u540c\u65f6\u652f\u6301 grpc \u8ddf restful \u6a21\u5f0f<\/li>\n<li>grpc , gin \u516c\u7528\u7aef\u53e3<\/li>\n<li>gorm \u5d4c\u5165\uff0c\u81ea\u52a8\u6570\u636e\u5e93\u4ee3\u7801\u751f\u6210<\/li>\n<\/ul>\n<h3>\u652f\u6301\u5217\u8868<\/h3>\n<ul>\n<li>grpc<\/li>\n<li>gorm \u81ea\u52a8\u6784\u5efa(gormt)<\/li>\n<li>gin \u53c2\u6570\u81ea\u52a8\u7ed1\u5b9a\u5de5\u5177(ginrpc)<\/li>\n<li>dns \u6ce8\u518c\u53d1\u73b0(mdns)<\/li>\n<li>markdown\/mindoc \u6587\u6863\u81ea\u52a8\u5bfc\u51fa<\/li>\n<\/ul>\n<h2>\u5b89\u88c5<\/h2>\n<ul>\n<li>\n<p>install<\/p>\n<\/li>\n<li>\n<p>proto \u73af\u5883\u5b89\u88c5<\/p>\n<\/li>\n<\/ul>\n<pre><code> make install  <\/code><\/pre>\n<ul>\n<li>\u672c\u5730\u73af\u5883\u642d\u5efa(gmsec \u4e3a\u4f8b)<\/li>\n<\/ul>\n<pre><code>make gen <\/code><\/pre>\n<h2>proto \u5b9a\u4e49<\/h2>\n<pre><code>syntax = \"proto3\"; \/\/ \u6307\u5b9a proto \u7248\u672c package proto;     \/\/ \u6307\u5b9a\u5305\u540d option go_package = \".;proto\"; \/\/ \u6307\u5b9a\u8def\u5f84  \/\/ \u5b9a\u4e49 Hello \u670d\u52a1 service Hello {     \/\/ \u5b9a\u4e49 SayHello \u65b9\u6cd5     rpc SayHello(HelloRequest) returns (HelloReply) {} } \/\/ HelloRequest \u8bf7\u6c42\u7ed3\u6784 message HelloRequest {     string name = 1; \/\/ \u540d\u5b57 } \/\/ HelloReply \u54cd\u5e94\u7ed3\u6784 message HelloReply {     string message = 1; \/\/ \u6d88\u606f } <\/code><\/pre>\n<h2>\u670d\u52a1\u7aef\u4ee3\u7801\u793a\u4f8b<\/h2>\n<pre><code>package main  import (  \"context\"  \"fmt\"  proto \"gmsec\/rpc\"   \"github.com\/gin-gonic\/gin\"  \"github.com\/gmsec\/goplugins\/plugin\"  \"github.com\/gmsec\/micro\"  \"github.com\/xxjwxc\/ginrpc\" )  func main() {  \/\/ grpc \u76f8\u5173 \u521d\u59cb\u5316\u670d\u52a1  service := micro.NewService(   micro.WithName(\"lp.srv.eg1\"),  )  h := new(hello)  proto.RegisterHelloServer(service.Server(), h) \/\/ \u670d\u52a1\u6ce8\u518c  \/\/ ----------- end   \/\/ gin \u76f8\u5173  base := ginrpc.New(ginrpc.WithCtx(Ctx), ginrpc.WithDebug(true), ginrpc.WithGroup(\"xxjwxc\"))  router := gin.Default()  \/\/ gen \u5bf9\u8c61  base.Register(router, h) \/\/ genrpc \u5bf9\u8c61\u6ce8\u518c  \/\/ ------ end   plg, _ := plugin.Run(plugin.WithMicro(service),\/\/ grpc \u5165\u53e3   plugin.WithGin(router), \/\/ http \u5165\u53e3         plugin.WithAddr(\":8080\")) \/\/ \u5f00\u59cb\u670d\u52a1(\u516c\u7528\u7aef\u53e3)          plg.Wait() \/\/ \u7b49\u5f85\u7ed3\u675f(ctrl+c)       fmt.Println(\"done\") }  \/\/ Ctx gin.Context \u5230 context.Context \u7684\u8f6c\u6362 func Ctx(c *gin.Context) interface{} {  return context.Background() } <\/code><\/pre>\n<h2>\u5ba2\u6237\u7aef\u4ee3\u7801:<\/h2>\n<pre><code>package main  import (  \"context\"  \"fmt\"  proto \"gmsec\/rpc\"   \"github.com\/gmsec\/micro\" )  func main() {     \/\/ reg := registry.NewDNSNamingRegistry()  \/\/ grpc \u76f8\u5173 \u6ce8\u518c\u670d\u52a1\u53d1\u73b0\u7b49  micro.NewService(         micro.WithName(\"lp.srv.eg1\"),         \/\/ micro.WithRegisterTTL(time.Second*30),      \/\/\u6307\u5b9a\u670d\u52a1\u6ce8\u518c\u65f6\u95f4         \/\/ micro.WithRegisterInterval(time.Second*15), \/\/\u8ba9\u670d\u52a1\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u91cd\u65b0\u6ce8\u518c         \/\/ micro.WithRegistryNameing(reg),  )  \/\/ ----------- end   say := proto.GetHelloClient()  ctx := context.Background()  resp, _ := say.SayHello(ctx, &amp;proto.HelloRequest{Name:\"xxjwxc\"})  fmt.Println(\"result:\", resp) } <\/code><\/pre>\n<h2>\u66f4\u591a\u793a\u4f8b =&gt; \u4f20\u9001\u95e8<\/h2>\n<h2>\u6b63\u5728\u505a<\/h2>\n<ul>\n<li>etcdv3<\/li>\n<\/ul>\n<h2>\u6b22\u8fce\u4e00\u8d77\u5171\u5efa\u5171\u4eab<\/h2>\n<h2>\u4f20\u9001\u95e8<\/h2>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1538013\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qq1340691923 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9876\u4e00\u4e2a\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1538014\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @qq1340691923 \u611f\u8c22 \u2728                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1538015\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yph007595 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             go-micro                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1538016\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yph007595 \u8fd9\u4e2a\u6846\u67b6\u5c31\u662f\u53c2\u8003\u7684 go-micro,\u53bb\u6389\u4e86\u5f88\u591a go-micro \u590d\u6742\u7684\u529f\u80fd\u3002\u6bd4\u5982\u81ea\u5b9a\u4e49 rpc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u63a8\u8350\u4e00\u6b3e golang \u5fae\u670d\u52a1\u96c6\u6210&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/83941"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83941"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/83941\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}