{"id":13713238,"url":"https://github.com/sunquakes/jsonrpc4go","last_synced_at":"2026-05-31T05:03:56.456Z","repository":{"id":38286426,"uuid":"331852371","full_name":"sunquakes/jsonrpc4go","owner":"sunquakes","description":"A go client and server over http or tcp implementation of JSON-RPC 2.0. Support consul \u0026 nacos.","archived":false,"fork":false,"pushed_at":"2024-08-22T13:18:56.000Z","size":192,"stargazers_count":10,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-13T23:33:22.458Z","etag":null,"topics":["client","consul","go","golang","http","json-rpc2","jsonrpc","jsonrpc2","nacos","server","tcp"],"latest_commit_sha":null,"homepage":"https://www.moonquakes.io/guide/golang.html","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/sunquakes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-01-22T06:04:12.000Z","updated_at":"2024-08-22T13:19:00.000Z","dependencies_parsed_at":"2024-08-22T14:57:18.956Z","dependency_job_id":null,"html_url":"https://github.com/sunquakes/jsonrpc4go","commit_stats":null,"previous_names":["sunquakes/go-jsonrpc"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunquakes%2Fjsonrpc4go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunquakes%2Fjsonrpc4go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunquakes%2Fjsonrpc4go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunquakes%2Fjsonrpc4go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunquakes","download_url":"https://codeload.github.com/sunquakes/jsonrpc4go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252787209,"owners_count":21804218,"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":["client","consul","go","golang","http","json-rpc2","jsonrpc","jsonrpc2","nacos","server","tcp"],"created_at":"2024-08-02T23:01:30.443Z","updated_at":"2026-05-31T05:03:56.438Z","avatar_url":"https://github.com/sunquakes.png","language":"Go","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"English | [🇨🇳中文](README_ZH.md)\n# jsonrpc4go\n## 🧰 Installing\n```\ngo get -u github.com/sunquakes/jsonrpc4go\n```\n## 📖 Getting started\n- Server\n```go\npackage main\n\nimport (\n    \"github.com/sunquakes/jsonrpc4go\"\n)\n\ntype IntRpc struct{}\n\ntype Params struct {\n\tA int `json:\"a\"`\n\tB int `json:\"b\"`\n}\n\nfunc (i *IntRpc) Add(params *Params, result *int) error {\n\t*result = params.A + params.B\n\treturn nil\n}\n\nfunc main() {\n\ts, _ := jsonrpc4go.NewServer(\"http\", 3232) // the protocol is http\n\ts.Register(new(IntRpc))\n\ts.Start()\n}\n```\n- Client\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/sunquakes/jsonrpc4go\"\n)\n\ntype Params struct {\n\tA int `json:\"a\"`\n\tB int `json:\"b\"`\n}\n\ntype Result2 struct {\n\tC int `json:\"c\"`\n}\n\nfunc main() {\n\tresult := new(int)\n\tc, _ := jsonrpc4go.NewClient(\"IntRpc\", \"http\", \"127.0.0.1:3232\")\n\terr := c.Call(\"Add\", Params{1, 6}, result, false)\n\t// data sent: {\"id\":\"1604283212\", \"jsonrpc\":\"2.0\", \"method\":\"IntRpc/Add\", \"params\":{\"a\":1,\"b\":6}}\n\t// data received: {\"id\":\"1604283212\", \"jsonrpc\":\"2.0\", \"result\":7}\n\tfmt.Println(err) // nil\n\tfmt.Println(*result) // 7\n}\n```\n## ⚔️ Test\n```\ngo test -v ./test/...\n```\n## 🚀 More features\n- TCP protocol\n```go\ns, _ := jsonrpc4go.NewServer(\"tcp\", 3232) // the protocol is tcp\n\nc, _ := jsonrpc4go.NewClient(\"IntRpc\", \"tcp\", \"127.0.0.1:3232\") // the protocol is tcp\n```\n- Hooks (Add the following code before 's.Start()')\n```go\n// Set the hook function of before method execution\ns.SetBeforeFunc(func(id interface{}, method string, params interface{}) error {\n    // If the function returns an error, the program stops execution and returns an error message to the client\n    // return errors.New(\"Custom Error\")\n    return nil\n})\n// Set the hook function of after method execution\ns.SetAfterFunc(func(id interface{}, method string, result interface{}) error {\n    // If the function returns an error, the program stops execution and returns an error message to the client\n    // return errors.New(\"Custom Error\")\n    return nil\n})\n```\n- Rate limit (Add the following code before 's.Start()')\n```go\ns.SetRateLimit(20, 10) //The maximum concurrent number is 10, The maximum request speed is 20 times per second\n```\n- Custom package EOF when the protocol is tcp\n```go\n// Add the following code before 's.Start()'\ns.SetOptions(server.TcpOptions{\"aaaaaa\", nil}) // Custom package EOF when the protocol is tcp\n// Add the following code before 'c.Call()' or 'c.BatchCall()'\nc.SetOptions(client.TcpOptions{\"aaaaaa\", nil}) // Custom package EOF when the protocol is tcp\n```\n- Notify\n```go\n// notify\nresult2 := new(Result2)\nerr2 := c.Call(\"Add2\", Params{1, 6}, result2, true)\n// data sent: {\"jsonrpc\":\"2.0\",\"method\":\"IntRpc/Add2\",\"params\":{\"a\":1,\"b\":6}}\n// data received: {\"jsonrpc\":\"2.0\",\"result\":{\"c\":7}}\nfmt.Println(err2) // nil\nfmt.Println(*result2) // {7}\n```\n- Batch call\n```go\n// batch call\nresult3 := new(int)\nerr3 := c.BatchAppend(\"Add1\", Params{1, 6}, result3, false)\nresult4 := new(int)\nerr4 := c.BatchAppend(\"Add\", Params{2, 3}, result4, false)\nc.BatchCall()\n// data sent: [{\"id\":\"1604283212\",\"jsonrpc\":\"2.0\",\"method\":\"IntRpc/Add1\",\"params\":{\"a\":1,\"b\":6}},{\"id\":\"1604283212\",\"jsonrpc\":\"2.0\",\"method\":\"IntRpc/Add\",\"params\":{\"a\":2,\"b\":3}}]\n// data received: [{\"id\":\"1604283212\",\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"Method not found\",\"data\":null}},{\"id\":\"1604283212\",\"jsonrpc\":\"2.0\",\"result\":5}]\nfmt.Println((*err3).Error()) // Method not found\nfmt.Println(*result3) // 0\nfmt.Println(*err4) // nil\nfmt.Println(*result4) // 5\n```\n- Client-Side Load-Balancing\n```go\nc, _ := jsonrpc4go.NewClient(\"IntRpc\", \"tcp\", \"127.0.0.1:3232,127.0.0.1:3233,127.0.0.1:3234\")\n```\n\n## Service registration \u0026 discovery\n### Consul\n```go\n/**\n * check: true or false. The switch of the health check.\n * interval: The interval of the health check. For example: 10s.\n * timeout: Timeout. For example: 10s.\n * instanceId: Instance ID. Distinguish the same service in different nodes. For example: 1.\n */\ndc, _ := consul.NewConsul(\"http://localhost:8500?check=true\u0026instanceId=1\u0026interval=10s\u0026timeout=10s\")\n\n// Set in the server.\ns, _ := jsonrpc4go.NewServer(\"tcp\", 3614)\n// If the default node ip is used, the second parameter can be set \"\"\ns.SetDiscovery(dc, \"127.0.0.1\")\ns.Register(new(IntRpc))\ns.Start()\n\n// Set in the client\nc, _ := jsonrpc4go.NewClient(\"IntRpc\", \"tcp\", dc)\n```\n### Nacos\n```go\ndc, _ := nacos.NewNacos(\"http://127.0.0.1:8849\")\n\n// Set in the server.\ns, _ := jsonrpc4go.NewServer(\"tcp\", 3616)\n// If the default node ip is used, the second parameter can be set \"\"\ns.SetDiscovery(dc, \"127.0.0.1\")\ns.Register(new(IntRpc))\ns.Start()\n\n// Set in the client\nc, _ := jsonrpc4go.NewClient(\"IntRpc\", \"tcp\", dc)\n```\n\n## 📄 License\nSource code in `jsonrpc4go` is available under the [Apache-2.0 license](/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunquakes%2Fjsonrpc4go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunquakes%2Fjsonrpc4go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunquakes%2Fjsonrpc4go/lists"}