RPC服务孵化
微服务孵化器是封装的rpcx框架,相比与直接使用原生rpcxserver,孵化器生成的server 增加了如下新特性:
总之,我们的目标是尽量减少人为的重复工作!
创建一个微服务
server 主入口文件
package mainimport ("context""github.com/tal-tech/odinPlugin/wrap""github.com/tal-tech/xtools/addrutil"bs "github.com/tal-tech/hera/bootstrap"rpcxplugin "github.com/tal-tech/odinPlugin""github.com/tal-tech/hera/rpcxserver""github.com/tal-tech/xtools/confutil")func main() {//加载配置文件,默认是 可执行文件所在目录的兄弟目录`conf`下的`conf.ini`//-app/// -bin/serverexe// -conf/conf.iniconfutil.InitConfig()//获取当前机器的真实IP地址addr, err := addrutil.Extract("")if err != nil {panic(err)}//通过孵化器生成一个rpcx server实例s := rpcxserver.NewServer(rpcxserver.Addr(addr))//注册服务启动前执行函数s.AddBeforeServerStartFunc(bs.InitLogger(),bs.InitTraceLogger("HS-Golang", "0.1"),s.InitConfig(),s.InitRegistry(),//新增插件类型recovery、costwarn,注意顺序s.InitRpcxPlugin(rpcxplugin.RatelimitOption()),s.RegisterServiceWithPlugin("myRpcServerName", new(MyRpcServer), ""))//注册服务停止后执行函数s.AddAfterServerStopFunc(bs.CloseLogger())//启动服务err = s.Serve()if err != nil {panic(err)}}type MyRpcServer struct {//server 必须包含此匿名变量,否则无法应用插件功能*rpcxplugin.RpcxPlugin}type HelloArgs struct {Param string}type HelloResponse struct {Msg string}func (this *MyRpcServer) SayHello(ctx context.Context, args *HelloArgs, reply *HelloResponse) (err error) {tag := "rpc.MyRpcServer.SayHello"fn := func(w *wrap.Wrap) error {reply = &HelloResponse{Msg:args.Param,}return nil}return this.Wrapcall(ctx, tag, fn)}
编译
服务注册与发现的配置中心,支持etcd和zookeeper 两种方式。
在编译的时候利用-tags参数选项来选择配置中心。
以zookeeper为注册中心,则编译命令如下:
$ go build -tags "zookeeper" -o myserver .
配置文件
任意位置创建一个conf.ini文件,假定和main.go 在同一目录
[Server];网络协议network=tcp;微服务端口port=18000[Registry];是否启用注册中心 on:启用 off:不启用status=on;;注册中心地址,多个地址以空格分割addrs=127.0.0.1:2181 127.0.0.1:2182;在配置中心的根目录basePath=/xes_test;注册更新时间 1m表示一分钟updateInterval=1m;注册服务的组,只有同组下的client/server 才能数据交互group=sam_test
启动服务
$ ./myserver -c conf.ini -p /path_to_your_conf_diruse NUX_ROOTFS = as file system root2019/12/28 18:39:27 CONF INIT,path:conf.ini2019/12/28 18:39:27 Conf,err:section 'Redis' not found2019/12/28 18:39:27 Connected to 127.0.0.1:21812019/12/28 18:39:27 authenticated: id=175151211264791228, timeout=100002019/12/28 18:39:27 re-submitting `0` credentials after reconnect[18:39:27 CST 2019/12/28] [WARN] (config.go:23:561420000000001 localhost.localdomain) Rpcx-plugin Conf get filepath fail2019/12/28 18:39:27 server.go:174: INFO : server pid:294372019/12/28 18:39:27 service.go:264: INFO : methodDoReregister has wrong number of ins:12019/12/28 18:39:27 service.go:264: INFO : methodGetMetadata has wrong number of ins:12019/12/28 18:39:27 service.go:264: INFO : methodSetReregister has wrong number of ins:22019/12/28 18:39:27 service.go:264: INFO : methodUpdate has wrong number of ins:12019/12/28 18:39:27 service.go:289: INFO : methodWrapcall reply type not a pointer:wrap.EndPoint[18:39:27 CST 2019/12/28] [INFO] (server.go:77:561420000000001 localhost.localdomain) RpcxServer server start listen on tcp@192.168.56.142:180002019/12/28 18:39:27 server.go:174: INFO : server pid:29437
一个标准的rpc微服务就建成了,接下您可以简单写一个rpcx client 进行测试!
实战案例
微服务框架Odin 就是我们用Hera孵化的案例,点我查看代码
有人可能会有疑问,怎么没有孵化rpcx客户端的功能?
