当前位置:网站首页>Using golang to implement RPC (1)
Using golang to implement RPC (1)
2022-07-24 08:27:00 【micro_ cloud_ fly】
Use golang The official library net/rpc
Server side
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/rpc"
"os"
)
type Rectangle struct {
}
type RectangleRequest struct {
Length int
Width int
}
type RectangleResponse struct {
Area int
Perimeter int
}
// Area area
func (this *Rectangle) Area(req RectangleRequest, res *RectangleResponse) error {
res.Area = req.Length * req.Width
return nil
}
// Perimeter Perimeter
func (this *Rectangle) Perimeter(req RectangleRequest, res *RectangleResponse) error {
res.Perimeter = 2 * (req.Length + req.Width)
return nil
}
func main() {
rpc.Register(new(Rectangle)) // register rpc service
rpc.HandleHTTP() // use http Act of agreement rpc carrier
lis, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
log.Fatalln("fatal error: ", err)
}
fmt.Fprintf(os.Stdout, "%s", "start connection")
http.Serve(lis, nil)
}
Client calls
package main
import (
"fmt"
"log"
"net/rpc"
)
type Rectangle struct {
A int
B int
}
type RectangleRequest struct {
Length int
Width int
}
type RectangleResponse struct {
Area int
Perimeter int
}
func main() {
conn, err := rpc.DialHTTP("tcp", "127.0.0.1:8080")
if err != nil {
log.Fatalln("dialing error: ", err)
}
req := RectangleRequest{
9, 2}
var res RectangleResponse
err = conn.Call("Rectangle.Area", req, &res) // area
if err != nil {
log.Fatalln("arith error: ", err)
}
fmt.Printf("%d * %d = %d\n", req.Length, req.Width, res.Area)
err = conn.Call("Rectangle.Perimeter", req, &res)
if err != nil {
log.Fatalln("arith error: ", err)
}
fmt.Printf("lenght:%d width:%d, perimeter is %d,\n", req.Length, req.Width, res.Perimeter)
}
边栏推荐
- Uva572 oil deposits problem solution
- Kotlin learning note 1 - variables, functions
- Digital collections are both outlets and risks!
- [MySQL] installation tutorial and master-slave configuration
- Error reported by Nacos: error Nacos failed to start, please see d:\nacos\logs\nacos log for more details.
- Solution of p3916 graph traversal problem
- SVG 从入门到后悔,怎么不早点学起来(图解版)
- Crypto giants all in metauniverse, and platofarm may break through
- Wxml template concise tutorial
- "Solution" friend of Vulcan
猜你喜欢

【游戏合集】手机都要被塞爆了,6款优质Pygame游戏合集降临~(附源码)

My six months at Microsoft

SVG 从入门到后悔,怎么不早点学起来(图解版)

Wechat applet host environment, applet architecture, concise operation structure

Figure New Earth: how to import CAD files with modified elevation datum (ellipsoid)

Web3≠NFT? A digital Renaissance?

Vscode code style notes (vetur)

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao

Mysql database advanced

Crypto giants all in metauniverse, and platofarm may break through
随机推荐
[tools] a few lines of code can realize complex excel import and export tool classes, which is really strong!!!
Wechat applet file types and functions
Go: how to gracefully time out
Assembly | screen display numbers
Kotlin learning note 1 - variables, functions
Figure New Earth: how to import CAD files with modified elevation datum (ellipsoid)
【JDBC】JDBC经典面试题,持续更新中......
Saining Techtalk attack and defense drill: attack combination fist "stable, accurate and ruthless" penetration
[multithreading] five communication modes between multithreads
P1305 new binary tree solution
In the next bull market, can platofarm, the leading project in the Web3 world, set foot on the top of the mountain
"Move to earn" motion metauniverse, move starts a new journey
WXS syntax reference -wxs module
SOA and microservice examples
Web3≠NFT? A digital Renaissance?
warning: could not execute support code to read Objective-C class data in the process.
Read and understand move2earn project - move
学习-用do…while循环按公式e=1+1/1!+1/2!+1/3!+…+1/n!计算 e 的值(精度为 1e-6)
Chinese brands in the historical process
nacos报错: ERROR Nacos failed to start, please see D:\nacos\logs\nacos.log for more details.