10秒钟,让你的方法变为RPC服务

opopenme 6年前
   <p><a href="/misc/goto?guid=4959714190375689268" rel="nofollow,noindex">rpcx</a> 一个服务治理的Go RPC框架, 拥有非常多的特性,支持跨语言的服务调用。 众多的特性可以参考 <a href="/misc/goto?guid=4959757092080389512" rel="nofollow,noindex">doc.rpcx.site</a> 。它的服务治理的特性深受阿里巴巴的Dubbo框架的启发。</p>    <p>在实际的产品应用中,用户使用两台服务器+8台日志搜集服务(Client),轻松处理每天几十亿的服务调用, 除了中间一个路由器硬件闪断, 整个系统平稳运行多半年。 相比较之前Java的实现, 服务器节省了一般。 用户使用rpcx框架重构后的系统每月为公司节省了几十万港元的成本。</p>    <p>rpcx框架的一个设计哲学就是 <strong>简单</strong> 。不希望用户需要花费大量的时间在框架的学习上,并且不需要proto文件或者重复且冗余的服务配置。最少只需要10行代码就可以创建一个服务, 如果需要额外的配置,也只需要几十行的代码。</p>    <p>虽然rpcx开发简单,但是作为开发人员来说,如果可以更加的偷懒, 那更是极好的一件事情了,这就是 <a href="/misc/goto?guid=4959757092174313138" rel="nofollow,noindex">xgen</a> 开发的目的。</p>    <p>这个工具可以搜寻指定的 package 下可以配置成rpcx服务的类型, 并且生成一个服务器程序,将这些服务注册到服务器程序中。你可以指定是否需要 zookeeper 、 etcd 、 consul 作为注册中心。</p>    <p>这个工具的开发参考了Go的tools的实现以及DigitalOcean公司的Fatih Arslan</p>    <p>开发的 <a href="/misc/goto?guid=4959757092267044305" rel="nofollow,noindex">gomodifytags</a> 的实现。</p>    <p>首先看一下这个工具参数:</p>    <pre>  <code class="language-go">$ xgen -h  Usage of xgen:    -o string       specify the filename of the output    -pkg       process the whole package instead of just the given file    -r string       registry type. support etcd, consul, zookeeper, mdns    -tags string       build tags to add to generated file  </code></pre>    <p>你可以使用 xgen file1.go file2.go file3.go 搜寻指定的文件生成服务,也可以 xgen -pkg github.com/rpcx-ecosystem/rpcx-examples3/xgen 为 GOPATH 中指定的 package 生成服务。 -pkg 选项优先于程序参数。</p>    <p>-o 选项指定生成的程序输出到哪个文件,如果不指定,则输出到控制台 Stdout 。</p>    <p>-r 选项指定注册中心的类型,支持 zookeeper 、 etcd 、 consul 和 mdns 。如果不指定,则采用点对点的rpc调用方式。</p>    <p>-tags 选项指定生成的文件是否要加上 build conditions 。</p>    <p>看一个例子, <a href="/misc/goto?guid=4959757092372167050" rel="nofollow,noindex">rpcx-examples3/xgen</a> 中有一个server.go文件,它定义几个类型和方法。</p>    <pre>  <code class="language-go">package xgen    import (   "context"   "fmt"   "time"     example "github.com/rpcx-ecosystem/rpcx-examples3"  )    type Arith int    func (t *Arith) Mul(ctx context.Context, args example.Args, reply *example.Reply) error {   reply.C = args.A * args.B   return nil  }    func (t *Arith) Add(ctx context.Context, args *example.Args, reply *example.Reply) error {   reply.C = args.A + args.B   return nil  }    type Echo string    func (s *Echo) Echo(ctx context.Context, args string, reply *string) error {   *reply = fmt.Sprintf("Hello %s from %s", args, *reply)   return nil  }    type TimeS struct{}    func (s *TimeS) Time(ctx context.Context, args time.Time, reply *time.Time) error {   *reply = time.Now()   return nil  }  </code></pre>    <p>这三个类型 Arith 、 Echo 、 TimeS 都有符合rpcx服务的方法。</p>    <p>rpcx的服务的方法需要满足下面的规则:</p>    <ul>     <li>类型和参数都是exported</li>     <li>方法有三个参数,并且第一个参数是 context.Context</li>     <li>方法的第三个参数是指针类型</li>     <li>方法类型为error</li>    </ul>    <p>现在你就可以使用 xgen 生成服务端代码。</p>    <pre>  <code class="language-go">xgen -o cmd/main.go -r "etcd" -pkg github.com/rpcx-ecosystem/rpcx-examples3/xgen  </code></pre>    <p>或者</p>    <pre>  <code class="language-go">xgen -o cmd/main.go -r "etcd" ./server.go  </code></pre>    <p>这样就生成了一个服务器端的代码。</p>    <p>你可以运行你的服务器了: go run cmd/main.go ,就这么简单。</p>    <p> </p>    <p>来自:http://colobu.com/2018/02/13/make-your-methods-as-services-in-10-seconds/</p>    <p> </p>