Transport

rpcx 可以通过 TCP、HTTP、UnixDomain、QUIC和KCP通信。你也可以使用http客户端通过网关或者http调用来访问rpcx服务。

TCP

这是最常用的通信方式。高性能易上手。你可以使用TLS加密TCP流量。

Example: 101basic

服务端使用 tcp 做为网络名并且在注册中心注册了名为 serviceName/tcp@ipaddress:port 的服务。
```go server.go
s.Serve(“tcp”, *addr)

  1. 客户端可以这样访问服务:
  2. ```go
  3. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
  4. xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)
  5. defer xclient.Close()

HTTP Connect

你可以发送 HTTP CONNECT 方法给 rpcx 服务器。 Rpcx 服务器会劫持这个连接然后将它作为TCP连接来使用。
需要注意,客户端和服务端并不使用http请求/响应模型来通信,他们仍然使用二进制协议。

网络名称是 http, 它注册的格式是 serviceName/http@ipaddress:port

HTTP Connect并不被推荐。 TCP是第一选择。

如果你想使用http 请求/响应 模型来访问服务,你应该使用网关或者http_invoke。

Unixdomain

网络名称是 unix

Example: unix

QUIC

维基百科上QUIC的介绍 wikipedia

QUIC (Quick UDP Internet Connections, pronounced quick) is an experimental transport layer network protocol designed by Jim Roskind at Google, initially implemented in 2012, and announced publicly in 2013 as experimentation broadened. QUIC supports a set of multiplexed connections between two endpoints over User Datagram Protocol (UDP), and was designed to provide security protection equivalent to TLS/SSL, along with reduced connection and transport latency, and bandwidth estimation in each direction to avoid congestion. QUIC’s main goal is to improve perceived performance of connection-oriented web applications that are currently using TCP. It also moves control of the congestion avoidance algorithms into the application space at both endpoints, rather than the kernel space, which it is claimed will allow these algorithms to improve more rapidly.

In June 2015, an Internet Draft of a specification for QUIC was submitted to the IETF for standardization. A QUIC working group was established in 2016. The QUIC working group foresees multipath support and optional forward error correction (FEC) as the next step. The working group also focuses on network management issues that QUIC may introduce, aiming to produce an applicability and manageability statement in parallel to the actual protocol work. Internet statistics in 2017 suggest that QUIC now accounts for more than 5% of Internet traffic.

网络名称是 quic

Example: quic

KCP

KCP 是一个快速并且可靠的ARQ协议。

网络名称是 kcp

Example: kcp

reuseport

网络名称是 reuseport

Example: reuseport

它使用tcp协议并且在linux/uxix服务器上开启 SO_REUSEPORT socket 选项。

TLS

Example: TLS

你可以在服务端配置 TLS:

  1. func main() {
  2. flag.Parse()
  3. cert, err := tls.LoadX509KeyPair("server.pem", "server.key")
  4. if err != nil {
  5. log.Print(err)
  6. return
  7. }
  8. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  9. s := server.NewServer(server.WithTLSConfig(config))
  10. s.RegisterName("Arith", new(example.Arith), "")
  11. s.Serve("tcp", *addr)
  12. }

你可以在客户端设置 TLS:

  1. func main() {
  2. flag.Parse()
  3. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
  4. option := client.DefaultOption
  5. conf := &tls.Config{
  6. InsecureSkipVerify: true,
  7. }
  8. option.TLSConfig = conf
  9. xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)
  10. defer xclient.Close()
  11. args := &example.Args{
  12. A: 10,
  13. B: 20,
  14. }
  15. reply := &example.Reply{}
  16. err := xclient.Call(context.Background(), "Mul", args, reply)
  17. if err != nil {
  18. log.Fatalf("failed to call: %v", err)
  19. }
  20. log.Printf("%d * %d = %d", args.A, args.B, reply.C)
  21. }