The installation
First, you need to install RPCX:
go get -u -v github.com/smallnest/rpcx/...
Copy the code
This step installs only the basic functionality of RPCX. If you want to use etCD as the registry, you need to add the LABEL ETCD. (see build tags)
go get -u -v -tags "etcd" github.com/smallnest/rpcx/...
Copy the code
If you want to use quIC, you also need to label it quic.
go get -u -v -tags "quic etcd" github.com/smallnest/rpcx/...
Copy the code
For convenience, I recommend you install all tags, even if you don’t need them right now:
go get -u -v -tags "reuseport quic kcp zookeeper etcd consul ping" github.com/smallnest/rpcx/...
Copy the code
tag:
- Quic: Supports QUIC
- KCP: supports the KCP protocol
- Zookeeper: Supports the ZooKeeper registry
- Etcd: Supports the ETCD registry
- Consul: Supports the Consul registry
- Ping: Supports network quality load balancing
- Reuseport reuseport: support
IO /part1/quick…
Simple Service implementation
Implementing a Sevice is like writing a simple Go construct:
import "context"
type Args struct {
A int
B int
}
type Reply struct {
C int
}
type Arith int
func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
reply.C = args.A * args.B
return nil
}
Copy the code
Register a simple service
S := server.newServer () s.registerName ("Arith", new(Arith), "") s.server (" TCP ", ":8972") // Enables the QUIC (UDP)/ TCP/HTTP serviceCopy the code
Hand write an interceptor
The PostReadRequest method of plugin is implemented
SignPlugin struct {} // SignPlugin func (SignPlugin *SignPlugin) PostReadRequest(CTX context.context, req *protocol.Message, Err Error) error {// fmt.println (req.servicemethod) // fmt.println (req.servicepath) // I am using json to send p := & your structure err = json.Unmarshal(req.Payload, P) // decodeMap := Payload = decodeMap return err}Copy the code
Add interceptors to the startup service
S := server.newServer () siginPlugin := & controller.signplugin {} s.pringins.add (siginPlugin) // Register the interceptor with the service S.registername ("Arith", new(Arith), "") s.server (" TCP ", ":8972") // Enables the QUIC (UDP)/ TCP/HTTP serviceCopy the code
This interceptor has been implemented. The client code is attached below
Option := client.defaultoption option.serializeType = protocol.JSON // #1 d := Client. NewPeer2PeerDiscovery ([email protected]: "8972", "") / / # 2 xclient: = client. NewXClient (" Arith", client Failtry, client.RandomSelect, d,option) defer xclient.Close() // #3 args := &example.Args{ A: 10, B: 20, } // #4 reply := &example.Reply{} // #5 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)Copy the code
Using QUIC (UDP) and TLS certificates Note:
- When using quic(UDP), s.server (” TCP “, “localhost:8972”). Address must be complete and localhost cannot be omitted
- When using QUIC (UDP), RPCX support for the QUIC protocol is implemented through Build Tags. Quic files are not compiled by default. If you do, you must manually specify tags yourself
- Server + client needs
go run -tags quic main.go Copy the code
- When using QUIC (UDP), TLS certificates must be used
- The server code
Cert, err := tls.LoadX509KeyPair(" public key path ", "private key path ") if err! = nil {panic(" Error :" + err.error ())} configs := & tls.config {Certificates: []tls.Certificate{cert}} s = server.NewServer(server.WithTLSConfig(configs))Copy the code
- The client code
conf := &tls.Config{ InsecureSkipVerify: true, // true= skip TLS certificate validation} option := client.defaultoption option.serializeType = protocol.JSON // Transfer data format option.tlsconfig = Conf / / using the TLS certificate: d = client. NewPeer2PeerDiscovery (" [email protected]:8972 ", "") xclient: = client. NewXClient (" Arith", client.Failtry, client.RandomSelect, d,option)Copy the code