Gateway

Gateway is a http gateway for rpcx services.

You can write rpc http clients in any programming languages such as Java、Python、C#、Node.js、Php、C\C++、Rust and others. See examples for many languages.

That means, you can use another approach to access rpcx services instead of implementing protocol.

Deployment models

There are two deployment modes: Gateway and Agent

Gateway

5.1. Gateway  - 图1

You can deploy this as gateway model. Gateway is running at independent servers. Clients sends http requests to gateways, and gateways translate http requests into raw rpcx requests. And then gateways send raw rpcx requests to rpcx services.

when gateways receive rpcx responses, they translates rpcx responses into http responses and return clients.

You can scale gateway easily because it is stateless.

It works like a http load balancer.

Agent

5.1. Gateway  - 图2

You can also deploy this as agent model. Agents are deploy with clients. They are installed as a daemon application in node of clients. If there are multiple clients in one node, you only need to install one agent.

Clients send http requests to their local agent. The local agent translate http requests into rpcx requests, send rpcx requests to rpcx services, translate rpcx responses into http ressponses.

It works like service mesh.

Protocol

You can send a http request in any programming languages. You must set some extra http headers in requests.

  • X-RPCX-Version: rpcx version
  • X-RPCX-MesssageType: set 0 as request
  • X-RPCX-Heartbeat: is heartbeat request or not, false in default.
  • X-RPCX-Oneway: is one-way request or not, false in default.
  • X-RPCX-SerializeType: 0 as raw bytes, 1 as JSON, 2 as protobuf, 3 as msgpack
  • X-RPCX-MessageID: message ID, uint64 type
  • X-RPCX-ServicePath: service path
  • X-RPCX-ServiceMethod: service method
  • X-RPCX-Meta: extra metadata
    For the http response, there are some extra http headers:

  • X-RPCX-Version: rpcx version

  • X-RPCX-MesssageType: 1 as response
  • X-RPCX-Heartbeat: is heartbeat response or not
  • X-RPCX-MessageStatusType: Error or Normal. Error means there is call failure
  • X-RPCX-SerializeType: 0 as raw bytes, 1 as JSON, 2 as protobuf, 3 as msgpack
  • X-RPCX-MessageID: message ID, uint64 type
  • X-RPCX-ServicePath: service path
  • X-RPCX-ServiceMethod: service method
  • X-RPCX-Meta: extra metadata
  • X-RPCX-ErrorMessage: error message if X-RPCX-MessageStatusType is Error

Example

There is a golang http client example:

  1. package main
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "github.com/rpcx-ecosystem/rpcx-gateway"
  8. "github.com/smallnest/rpcx/codec"
  9. )
  10. type Args struct {
  11. A int
  12. B int
  13. }
  14. type Reply struct {
  15. C int
  16. }
  17. func main() {
  18. cc := &codec.MsgpackCodec{}
  19. // request
  20. args := &Args{
  21. A: 10,
  22. B: 20,
  23. }
  24. data, _ := cc.Encode(args)
  25. req, err := http.NewRequest("POST", "http://127.0.0.1:9981/", bytes.NewReader(data))
  26. if err != nil {
  27. log.Fatal("failed to create request: ", err)
  28. return
  29. }
  30. // set extra headers
  31. h := req.Header
  32. h.Set(gateway.XMessageID, "10000")
  33. h.Set(gateway.XMessageType, "0")
  34. h.Set(gateway.XSerializeType, "3")
  35. h.Set(gateway.XServicePath, "Arith")
  36. h.Set(gateway.XServiceMethod, "Mul")
  37. // send to gateway
  38. res, err := http.DefaultClient.Do(req)
  39. if err != nil {
  40. log.Fatal("failed to call: ", err)
  41. }
  42. defer res.Body.Close()
  43. // handle http response
  44. replyData, err := ioutil.ReadAll(res.Body)
  45. if err != nil {
  46. log.Fatal("failed to read response: ", err)
  47. }
  48. // parse reply
  49. reply := &Reply{}
  50. err = cc.Decode(replyData, reply)
  51. if err != nil {
  52. log.Fatal("failed to decode reply: ", err)
  53. }
  54. log.Printf("%d * %d = %d", args.A, args.B, reply.C)
  55. }

By smallnest updated 2018-12-04 11:47:26