52f7603705c3ac30294c5c32c0df3aca
gorpc 源码解读 —— 从 0 到 1实现一款 rpc 框架

之前发过很多 grpc 源码解读的文章,这里是题主去年自己实现的 rpc 框架,性能已经超过 grpc 的两倍,源码非常简洁, 默认支持服务发现 (提供了 consul 实现)、负载均衡 (支持随机、轮询、加权轮询、一致性哈希)、分布式链路追踪(接入 opentracing,提供了 jaeger 实现)、连接池、拦截器等功能 ,支持代码生成和反射两种调用方式(定义一个 go 结构体就可以发布服务)
求 start + 关注,欢迎大家来一起提 issue 和 pr

本专栏后续也会陆续解读 gorpc 的原理和实现 !!! 先贴上官网介绍,更多详情可以查看 gorpc

gorpc

A simple, easy to use, high performance, pluggable micro services framework

Installation

Before installing gorpc, you need to install go and configure the go environment

In gopath mode, you simply execute the following command to install

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

In the go modules mode, you simply import the package "github.com/lubanproj/gorpc", which automatically downloads the dependency when you execute the go [build|run|test]

Quick Start

git clone https://github.com/lubanproj/gorpc.git
cd gorpc/examples/helloworld
# start server
go run server/server.go
# start client,start another terminal and execute
go run client/client.go

Example

You only need three steps to complete a service call

  1. define a service
  2. use a server to publish a service
  3. use a client to initiate a service call

1. define a service

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. use a server to publish a service

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. use a client to initiate a service call

```
func main() {
opts := []client.Option {
client.WithTarget("127.0.0.1:8000"),
client.WithNetwork("tcp"),
client.WithTimeout(2000 * time.Millisecond),
client.WithSerializationType("msgpack"),

top Created with Sketch.