您当前的位置: 首页 >  微服务

使用 gorpc 开发一个微服务

蔚1 发布时间:2020-04-02 23:30:43 ,浏览量:6

gorpc —— 一个简单,易用,高性能,可插拔的微服务框架

源码可见:https://github.com/lubanproj/gorpc

  • 高性能,性能远远超过 gRpc
  • 支持反射、代码生成两种调用方式
  • 可插拔
  • 多协议支持,目前支持 TCP、UDP、HTTP,后续会支持更多协议
  • 实现了拦截器,支持业务自定义拦截器
  • 实现了连接池,支持业务自定义连接池
  • 支持服务发现
  • 支持负载均衡
  • 支持分布式链路追踪
  • 支持多种序列化方式
  • ……
一、安装

在安装 gorpc 之前,您需要安装 go 并配置 go 环境。

gopath 模式下,您只需执行以下命令即可安装:

go get -u -v github.com/lubanproj/gorpc

go modules 模式下,您只需导入软件包 “ github.com/lubanproj/gorpc”,该软件包在执行 go [build | run | test] 时会自动下载依赖项。

二、快速开始
git clone https://github.com/lubanproj/gorpc.gitcd gorpc/examples/helloworld# start servergo run server/server.go# start client,start another terminal and executego run client/client.go
三、示例

发起一个服务调用只需要以下三个步骤:

  1. 定义一个服务
  2. server 发布服务
  3. 使用一个客户端发起调用

1.定义一个服务

type Service struct {}type HelloRequest struct {    Msg string}type HelloReply struct {    Msg string}func (s *Service) SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error) {    rsp := &HelloReply{        Msg : "world",    }    return rsp, nil}

2. server 发布服务

func main() {    opts := []gorpc.ServerOption{        gorpc.WithAddress("127.0.0.1:8000"),        gorpc.WithNetwork("tcp"),        gorpc.WithSerializationType("msgpack"),        gorpc.WithTimeout(time.Millisecond * 2000),    }    s := gorpc.NewServer(opts ...)    if err := s.RegisterService("/helloworld.Greeter", new(helloworld.Service)); err != nil {        panic(err)    }    s.Serve()}

3. 使用一个客户端发起调用

func main() {    opts := []client.Option {        client.WithTarget("127.0.0.1:8000"),        client.WithNetwork("tcp"),        client.WithTimeout(2000 * time.Millisecond),        client.WithSerializationType("msgpack"),    }    c := client.DefaultClient    req := &helloworld.HelloRequest{        Msg: "hello",    }    rsp := &helloworld.HelloReply{}    err := c.Call(context.Background(), "/helloworld.Greeter/SayHello", req, rsp, opts ...)    fmt.Println(rsp.Msg, err)}

更多细节可以参考 helloworld

四、文档
  • Examples.
  • FAQ
五、特性
  • 高性能,性能远远超过 grpc ,详情可以参考 性能
  • 支持 反射, 代码生成 两种调用方式
  • 可插拔 所有插件都是可插拔、支持业务自定义的
  • 多协议支持,目前支持 tcp、udp、http,后续会支持更多协议
  • 实现了拦截器,支持业务自己定义拦截器
  • 实现了连接池,支持业务自定义连接池
  • 支持服务发现,提供了基于 consul 的默认服务发现实现,支持业务自定义服务发现实现。
  • 支持负载均衡 ,提供了随机、轮询、加权轮询、一致性哈希等默认负载均衡实现,支持业务自定义负载均衡实现。
  • 支持分布式链路追踪,遵循业界 opentracing 规范,提供了基于 jaeger 的分布式链路追踪默认实现,支持业务自定义。
  • 支持多种序列化方式,框架默认采用 protocol 和 msgpack 序列化,用代码生成方式调用会使用 protocol 序列化。用反射方式调用会采用 msgpack 序列化,支持业务自定义序列化方式。
  • 更多特性正在陆续支持中 ……
六、性能

环境 :

  • CPU : Intel(R) Xeon(R) Gold 61xx CPU @2.44GHz
  • CPU cores : 8
  • Memory : 16G
  • Disk : 540G

Result :使用 gorpc-benchmark 进行测试,测试三次取性能最大值

gorpc :

git clone https://github.com/lubanproj/gorpc-benchmark.gitcd gorpc-benchmark# start gorpc servergo run server.go# start gorpc-benchmark client,start another terminal and executego run client.go -concurrency=100 -total=1000000

性能测试结果如下:

> go run client.go -concurrency=100 -total=10000002020/02/29 15:56:57 client.go:71: [INFO] took 5214 ms for 1000000 requests2020/02/29 15:56:57 client.go:72: [INFO] sent     requests      : 10000002020/02/29 15:56:57 client.go:73: [INFO] received requests      : 10000002020/02/29 15:56:57 client.go:74: [INFO] received requests succ : 10000002020/02/29 15:56:57 client.go:75: [INFO] received requests fail : 02020/02/29 15:56:57 client.go:76: [INFO] throughput  (TPS)      : 191791

grpc :

在相同机器上进行 grpc 性能测试,如下:

git clone https://github.com/lubanproj/gorpc-benchmark.gitcd gorpc-benchmark/grpc# run gorpc servergo run server.go# run gorpc-benchmark client, start another terminal and execute go run client.go -concurrency=100 -total=1000000

性能测试结果如下:

> go run client.go -concurrency=100 -total=10000002020/02/29 15:46:14 client.go:77: [INFO] took 17169 ms for 1000000 requests2020/02/29 15:46:14 client.go:78: [INFO] sent     requests      : 10000002020/02/29 15:46:14 client.go:79: [INFO] received requests      : 10000002020/02/29 15:46:14 client.go:80: [INFO] received requests succ : 10000002020/02/29 15:46:14 client.go:81: [INFO] received requests fail : 02020/02/29 15:46:14 client.go:82: [INFO] throughput  (TPS)      : 58244
七、贡献

可以参考 Contributing

阅读全文: http://gitbook.cn/gitchat/activity/5e85503141cb08243edae005

您还可以下载 CSDN 旗下精品原创内容社区 GitChat App ,阅读更多 GitChat 专享技术内容哦。

FtooAtPSkEJwnW-9xkCLqSTRpBKX

关注
打赏
1688896170
查看更多评论

蔚1

暂无认证

  • 6浏览

    0关注

    4645博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0713s