传输

rpcx可以通过TCP、HTTP连接、Unix域、QUIC和KCP通讯。你也可以使用一个http客户端通过网关或者HTTP请求来访问rpcx。

TCP

这个是用的最多的传输。高性能并且简单。你可以使用TLS来保证TCP安全。

例程101basic

服务器使用tcp作为network参数的名字,使用serviceName/tcp@ipaddress:port来注册服务。

// go server.go 

s.Serve("tcp", *addr)

客户端可以以以下方式连接到服务:

// go client.go

d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)
defer xclient.Close()

HTTP 连接

你可以发送 HTTP CONNECT方法到rpcx服务器。Rpcx服务器劫持该连接然后作为一个TCP连接来使用它。注意客户端和服务器不使用http请求/响应来通讯,而是使用二进制协议。

network参数名为http,注册格式为serviceName/http@ipaddress:port。

不推荐使用该连接方式,TCP方式是首选。

如果你想使用http请求/响应方式访问,你应该使用网关或者http请求。

Unix域

network参数名为unix。

例程:unix

QUIC

源自wikipeida

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.

network参数名为quic。

例程:quic

KCP

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

network参数名为kcp。

例程:kcp

reuseport

network参数名为reuseport。

例程:reuseport

它使用tcp协议栈,对于linux和unix服务器设置了SO_REUSEPORT

TLS

例程:TLS

你可以在服务器中设置TLS:

func main() {
    flag.Parse()

    cert, err := tls.LoadX509KeyPair("server.pem", "server.key")    
    if err != nil {
        log.Print(err)
        return
    }

    config := &tls.Config{Certificates: []tls.Certificate{cert}}

    s := server.NewServer(server.WithTLSConfig(config))
    s.RegisterName("Arith", new(example.Arith), "")
    s.Serve("tcp", *addr)
 }

在客户端中设置TLS:

func main() {
    flag.Parse()

    d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")

    option := client.DefaultOption

    conf := &tls.Config{
            InsecureSkipVerify: true,
    }

    option.TLSConfig = conf

    xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)    
    defer xclient.Close()

    args := &example.Args{
    A: 10,
    B: 20,
    }

    reply := &example.Reply{}
    err := xclient.Call(context.Background(), "Mul", args, reply)    
    if err != nil {
        log.Fatalf("failed to call: %v", err)
    }

    log.Printf("%d * %d = %d", args.A, args.B, reply.C)
}

results matching ""

    No results matching ""