Go comes with RPC easy development
Server code server.go
package main
import (
"net"
"net/rpc"
)
type HelloService struct{}
func (s *HelloService) Hello(request string, reply *string) error {
// The return value is by modifying the value of reply
*reply = "hello, " + request
return nil
}
func main(a) {
1. Instantiate a server
listener, _ := net.Listen("tcp".": 1234")
// 2. Register the logic handler
_ = rpc.RegisterName("HelloService", &HelloService{})
// 3. Start the service
conn, _ := listener.Accept()
rpc.ServeConn(conn)
}
Copy the code
The client code client.go
package main
import (
"fmt"
"net/rpc"
)
func main(a) {
client, err := rpc.Dial("tcp"."localhost:1234")
iferr ! =nil {
panic("Link failed")}var reply string
err = client.Call("HelloService.Hello"."bobby",&reply)
iferr ! =nil {
panic("Call failed")
}
fmt.Println(reply)
}
Copy the code
Replace the serialization protocol for RPC with JSON
Server code server.go
package main
import (
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
type HelloService struct{}
func (s *HelloService) Hello(request string, reply *string) error {
// The return value is by modifying the value of reply
*reply = "hello, " + request
return nil
}
func main(a) {
1. Instantiate a server
listener, _ := net.Listen("tcp".": 1234")
// 2. Register the logic handler
_ = rpc.RegisterName("HelloService", &HelloService{})
for {
conn, _ := listener.Accept() // When a new connection comes in
go rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
Copy the code
The client client. Go
package main
import (
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
func main(a) {
conn, err := net.Dial("tcp"."localhost:1234")
iferr ! =nil {
panic("Link failed")}var reply string
client := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn))
err = client.Call("HelloService.Hello"."bobby",&reply)
iferr ! =nil {
panic("Call failed")
}
fmt.Println(reply)
}
Copy the code
client.py
import json
import socket
request = {
"id": 0."params": ["bobby"]."method": "HelloService.Hello"
}
client = socket.create_connection(("localhost".1234))
client.sendall(json.dumps(request).encode())
rsp = client.recv(4096)
rsp = json.loads(rsp.decode())
print(rsp["result"])
Copy the code
The transport protocol that replaces RPC is HTTP
package main
import (
"io"
"net/http"
"net/rpc"
"net/rpc/jsonrpc"
)
type HelloService struct{}
func (s *HelloService) Hello(request string, reply *string) error {
// The return value is by modifying the value of reply
*reply = "hello, " + request
return nil
}
func main(a) {
_ = rpc.RegisterName("HelloService", &HelloService{})
http.HandleFunc("/jsonrpc".func(w http.ResponseWriter, r *http.Request) {
var conn io.ReadWriteCloser = struct {
io.Writer
io.ReadCloser
}{
ReadCloser: r.Body,
Writer: w,
}
rpc.ServeRequest(jsonrpc.NewServerCodec(conn))
})
http.ListenAndServe(": 1234".nil)}Copy the code
import requests
request = {
"id": 0."params": ["bobby"]."method": "HelloService.Hello"
}
rsp = requests.post("http://localhost:1234/jsonrpc", json=request)
print(rsp.text)
Copy the code
Further code modification
handler.go
package handler
const HelloServiceName = "handler/HelloService"
type NewHelloService struct{}
func (s *NewHelloService) Hello(request string, reply *string) error {
// The return value is by modifying the value of reply
*reply = "hello, " + request
return nil
}
Copy the code
server.go
package main
import (
"Test/handler"
"Test/server_proxy"
"net"
"net/rpc"
)
func main(a) {
1. Instantiate a server
listener, _ := net.Listen("tcp".": 1234")
// 2. Register the logic handler
_ = server_proxy.RegisterHelloService(&handler.NewHelloService{})
// 3. Start the service
for {
conn, _ := listener.Accept()
go rpc.ServeConn(conn)
}
}
Copy the code
server_proxy.go
package server_proxy
import (
"Test/handler"
"net/rpc"
)
type HelloServicer interface {
Hello(request string, reply *string) error
}
func RegisterHelloService(srv HelloServicer) error {
return rpc.RegisterName(handler.HelloServiceName, srv)
}
Copy the code
client.go
package main
import (
"Test/client_proxy"
"fmt"
)
func main(a) {
client := client_proxy.NewHelloServiceClient("tcp"."localhost:1234")
var reply string
err := client.Hello("bobby", &reply)
iferr ! =nil {
panic("Call failed")
}
fmt.Println(reply)
}
Copy the code
client_proxy.go
package client_proxy
import (
"Test/handler"
"net/rpc"
)
type HelloServiceStub struct {
*rpc.Client
}
func NewHelloServiceClient (protcol, address string) HelloServiceStub {
conn, err := rpc.Dial(protcol, address)
iferr ! =nil {
panic("connect error!")}return HelloServiceStub{conn}
}
func (c *HelloServiceStub)Hello(request string, reply *string) error {
err := c.Call(handler.HelloServiceName+".Hello", request, reply)
iferr! =nil {
return err
}
return nil
}
Copy the code