{"id":20734395,"url":"https://github.com/byebyebruce/natsrpc","last_synced_at":"2025-04-23T23:06:03.301Z","repository":{"id":43027294,"uuid":"373802656","full_name":"byebyebruce/natsrpc","owner":"byebyebruce","description":"NATSRPC, transport by nats, define by proto file(gen code),use like grpc","archived":false,"fork":false,"pushed_at":"2024-01-25T11:18:23.000Z","size":408,"stargazers_count":18,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T23:05:49.465Z","etag":null,"topics":["golang","grpc","nats","natsrpc","proto","protobuf","protoc","protoc-gen-go","rpc"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/byebyebruce.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-04T10:16:17.000Z","updated_at":"2024-05-16T13:38:41.000Z","dependencies_parsed_at":"2024-06-19T05:40:32.164Z","dependency_job_id":null,"html_url":"https://github.com/byebyebruce/natsrpc","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byebyebruce%2Fnatsrpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byebyebruce%2Fnatsrpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byebyebruce%2Fnatsrpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byebyebruce%2Fnatsrpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byebyebruce","download_url":"https://codeload.github.com/byebyebruce/natsrpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250528732,"owners_count":21445516,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","grpc","nats","natsrpc","proto","protobuf","protoc","protoc-gen-go","rpc"],"created_at":"2024-11-17T05:30:21.476Z","updated_at":"2025-04-23T23:06:03.247Z","avatar_url":"https://github.com/byebyebruce.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"```\n  _   _       _______ _____   _____  _____   _____ \n | \\ | |   /\\|__   __/ ____| |  __ \\|  __ \\ / ____|\n |  \\| |  /  \\  | | | (___   | |__) | |__) | |     \n | . ` | / /\\ \\ | |  \\___ \\  |  _  /|  ___/| |     \n | |\\  |/ ____ \\| |  ____) | | | \\ \\| |    | |____ \n |_| \\_/_/    \\_\\_| |_____/  |_|  \\_\\_|     \\_____|\n```\n\n## What is NATSRPC\n\u003e NATSRPC 是一个基于[NATS](https://nats.io/)作为消息通信，使用[gRPC](https://www.grpc.io/)的方式来定义接口的RPC框架\n\n![GitHub release (with filter)](https://img.shields.io/github/v/release/byebyebruce/natsrpc)\n![](https://hits.sh/github.com/byebyebruce/natsrpc/doc/hits.svg?label=visit)\n\n## Motivation  \nNATS收发消息需要手动定义subject，request，reply，handler等繁琐且易出错的代码。\ngRPC需要连接到可知endpoint才能发送请求。\nNATRPC的目的就是要像gRPC一样定义接口，像NATS一样不关心具体网络位置，只需要监听和发送就能完成RPC调用。\n\n## Feature\n* 使用gRPC接口定义方式，使用简单，一键生成代码\n* 支持空间隔离,也可以指定id发送\n* 多服务可以负载均衡(nats的同组内随机)\n* 支持Header和返回Error\n* 支持单协程和多协程handle\n* 支持中间件\n* 支持延迟回复消息\n* 支持自定义编码器\n\n## How It Works\n上层通过Server、Service、Client对nats.Conn和Subscription进行封装。  \n底层通过nats的request和publish来传输消息。一个Service会创建一个以service name为subject的Subscription，如果有publish方法会在创建一个用于接收publish的sub。  \nClient发请求时会的subject是service 的name，并且nats msg的header传递method name。  \nService收到消息后取出method name，然后调用对应的handler，handler返回的结果会通过nats msg的reply subject返回给Client。\n\n## Install Tools\n1. protoc(v3.17.3) \n   - [Linux](https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip)\n   - [MacOS](https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-osx-x86_64.zip)\n   - [Windows](https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-win64.zip)\n   \n2. protoc-gen-gogo \n   ```shell\n   go install github.com/gogo/protobuf/protoc-gen-gogo@v1.3.2\n   ```\n3. protoc-gen-natsrpc \n   ```shell\n   go install github.com/byebyebruce/natsrpc/cmd/protoc-gen-natsrpc@v0.7.0\n   ```\n\n## Quick Start\n* [nats-server](https://github.com/nats-io/nats-server/releases)\u003e=2.2.0\n1. 引用包\n   ```shell\n   go get github.com/byebyebruce/natsrpc\n   ```\n2. 定义服务接口 example.proto\n    ```\n    syntax = \"proto3\";\n\n    package example;\n    option go_package = \"github.com/byebyebruce/natsrpc/example;example\";\n\n    message HelloRequest {\n      string name = 1;\n    }\n\n    message HelloReply {\n      string message = 1;\n    }\n\n    service Greeter {\n      rpc Hello (HelloRequest) returns (HelloReply) {}\n    }\n    ```\n   \n3. 生成客户端和服务端代码\n    ```shell\n    protoc --proto_path=. \\\n    --gogo_out=paths=source_relative:. \\\n    --natsrpc_out=paths=source_relative:. \\\n    *.proto\n    ```\n4. Server端实现接口并创建服务\n   ```\n   type HelloSvc struct {\n   }\n\n   func (s *HelloSvc) Hello(ctx context.Context, req *example.HelloRequest) (*example.HelloReply, error) {\n       return \u0026example.HelloReply{\n           Message: \"hello \" + req.Name,\n       }, nil\n   }\n\n   func main() {\n       conn, err := nats.Connect(*nats_url)\n       defer conn.Close()\n\n       server, err := natsrpc.NewServer(conn)\n       defer server.Close(context.Background())\n\n       svc, err := example.RegisterGreetingNRServer(server, \u0026HelloSvc{})\n       defer svc.Close()\n       \n       select{\n       }\n   }\n\n   ```\n   \n5. Client 调用 rpc\n   ```\n   client:=natsrpc.NewClient(conn)\n   \n   cli := example.NewGreeterNRClient(client)\n   rsp,err:=cli.Hello(context.Background(), \u0026example.HelloRequest{Name: \"natsrpc\"})\n   ```\n \n## Examples\n[here](./example)\n\n## Bench Tool\n1. 请求 `go run ./example/tool/request_bench -url=nats://127.0.0.1:4222`\n2. 广播 `go run ./example/tool/publish_bench -url=nats://127.0.0.1:4222`\n\n## TODO\n- [x] service 定义文件改成gRPC标准\n- [x] 支持返回错误\n- [x] 支持Header\n- [x] 生成Client接口\n- [x] 支持中间件\n- [x] 默认多线程，同时支持单一个线程\n- [ ] 支持goroutine池\n- [ ] 支持字节池\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyebyebruce%2Fnatsrpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyebyebruce%2Fnatsrpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyebyebruce%2Fnatsrpc/lists"}