golang rpc demo
RPC工作流程图
- 1.调用客户端句柄;执行传送参数
- 2.调用本地系统内核发送网络消息
- 3.消息传送到远程主机
- 4.服务器句柄得到消息并取得参数
- 5.执行远程过程
- 6.执行的过程将结果返回服务器句柄
- 7.服务器句柄返回结果,调用远程系统内核
- 8.消息传回本地主机
- 9.客户句柄由内核接收消息
- 10.客户接收句柄返回的数据
Go语言提供对RPC的支持:HTTP、TCP、JSPNRPC
,但是在Go
中RPC
是独一无二的,它采用了GoLang Gob
编码,只能支持Go语言!
- GoLang Gob:是Golang包自带的一个数据结构序列化的编码/解码工具。编码使用Encoder,解码使用Decoder。一种典型的应用场景就是RPC(remote procedure calls)。
HTTP RPC Demo
- 服务端的代码
package main import (
"errors"
"fmt"
"net/http"
"net/rpc"
) type Arith int func rpcDemo() {
arith := new(Arith)
fmt.Println("arith===", arith) rpc.Register(arith)
//HandleHTTP将RPC消息的HTTP处理程序注册到Debug服务器
//DEFAUTUPCPATH和Debug调试路径上的调试处理程序。
//仍然需要调用http.Services(),通常是在GO语句中。
rpc.HandleHTTP()
err := http.ListenAndServe(":1234", nil)
if err != nil {
fmt.Println("err=====", err.Error())
}
} type Args struct {
A, B int
} type Quotient struct {
Quo, Rem int
} //函数必须是导出的(首字母大写)
//必须有两个导出类型的参数,
//第一个参数是接收的参数,第二个参数是返回给客户端的参数,第二个参数必须是指针类型的
//函数还要有一个返回值error
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
fmt.Println("这个方法执行了啊---嘿嘿--- Multiply ", reply) return nil
} func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == {
return errors.New("divide by zero")
} quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
fmt.Println("这个方法执行了啊---嘿嘿--- Divide quo==", quo) return nil
} func main() {
rpcDemo()
}服务端运行:go run server.go
- 客户端的代码
package main import (
"flag"
"fmt"
"log"
"net/rpc"
"strconv"
) type ArgsTwo struct {
A, B int
} type QuotientTwo struct {
Quo, Rem int
} type Conf struct {
serverAddress string
i1 string
i2 string
} var conf = Conf{} func SetConfiguration() {
flag.StringVar(&conf.serverAddress, "address", "127.0.0.1:1234", "The address of the rpc")
flag.StringVar(&conf.i1, "i1", "", "")
flag.StringVar(&conf.i2, "i2", "", "")
} func main() {
SetConfiguration()
flag.Parse()
fmt.Println("severAddress = ", conf.serverAddress) // DelayHTTP在指定的网络地址连接到HTTP RPC服务器
// 在默认HTTP RPC路径上监听。
client, err := rpc.DialHTTP("tcp", conf.serverAddress)
if err != nil {
log.Fatal("发生错误了 在这里地方 DialHTTP", err)
} i1_, _ := strconv.Atoi(conf.i1)
i2_, _ := strconv.Atoi(conf.i2)
args := ArgsTwo{A: i1_, B: i2_}
var reply int //调用调用命名函数,等待它完成,并返回其错误状态。
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("Call Multiply 发生错误了哦 arith error:", err)
}
fmt.Printf("Arith 乘法: %d*%d=%d\n", args.A, args.B, reply) var quot QuotientTwo
//调用调用命名函数,等待它完成,并返回其错误状态。
err = client.Call("Arith.Divide", args, ")
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith 除法取整数: %d/%d=%d 余数 %d\n", args.A, args.B, quot.Quo, quot.Rem)
}客户端编译:go build client.go
客户端运行: [root@wangjq rpc]# ./client
severAddress = 127.0.0.1:
Arith 乘法: *=
Arith 除法取整数: /= 余数
[root@wangjq rpc]#
[root@wangjq rpc]# ./client --address 127.0.0.1: -i1 -i2
severAddress = 127.0.0.1:
Arith 乘法: *=
Arith 除法取整数: /= 余数
golang rpc demo的更多相关文章
- win7环境下,golang thrift demo代码编译不通过
用官方的教程代码:http://thrift.apache.org/tutorial/go 用网友提供的代码:Golang RPC 之 Thrift 都出现如下情况 状况1: 编辑器中就会提醒 Can ...
- Windows RPC Demo实现
Windows RPC Demo实现 本文参考并整理以下相关文章 1. <远程过程调用> -百度百科 2. <RPC 编程> -http://www.ibm.com/devel ...
- Golang RPC 性能测试
Golang RPC 性能测试 | KDF5000 http://kdf5000.com/2017/03/28/Golang-RPC-性能测试/
- golang RPC通信读写超时设置
golang RPC通信中,有时候就怕读写hang住. 那是否可以设置读写超时呢? 1.方案一: 设置连接的读写超时 1.1 client RPC通信基于底层网络通信,可以通过设置connection ...
- golang 并发demo 写入 redis
原文链接:golang 并发demo 写入 redis 源代码: package main import ( "fmt" "runtime" "str ...
- golang grpc demo
1.grpm 安装: git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc 2.proto, ...
- golang rpc 简单范例
RPC(Remote Procedure Call Protocol)--远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. 它的工作流程如下图: go ...
- golang rpc介绍
rpc包提供了通过网络或其他I/O连接对一个对象的导出方法的访问.服务端注册一个对象,使它作为一个服务被暴露,服务的名字是该对象的类型名.注册之后,对象的导出方法就可以被远程访问.服务端可以注册多个不 ...
- golang ctrie demo
下载ctrie: go get -t github.com/Workiva/go-datastructures/trie/ctrie 测试demo1: package main import ( &q ...
随机推荐
- 2018年5月15日的sqlite安装和数据库记录
sqlite数据库安装在d:\sqlite_files运行sqlite3查看数据表,命令,.tables 数据库文件 d:\sqlite_files\device.db create table de ...
- 删除表中重复数据,只删除重复数据中ID最小的
delete t_xxx_user where recid in ( select recid from t_xxx_user where recid in ( select min(recid) f ...
- 爬虫(三)-之Urllib库的基本使用
什么是Urllib Urllib是python内置的HTTP请求库 包括以下模块 urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解 ...
- 如何在sed中使用变量,两种方法
第一 在sed条件中是不认识变量取值的 sed '/$x/d' test 所以要想它能够识别变量 sed "/$x/d/" test 方法简单就是把"单引号"变 ...
- python处理excel文件(xls和xlsx)
一.xlrd和xlwt 使用之前需要先安装,windows上如果直接在cmd中运行python则需要先执行pip3 install xlrd和pip3 install xlwt,如果使用pycharm ...
- 解决node 运行接口 出现 Cannot destructure property `us` of 'undefined' or 'null'.
出现 参数是 undefined or null 一.检查是否安装 body-parser server.js中是否引入 app.use(bodyParser.urlencoded({ extende ...
- PHP password_get_info() 函数
password_get_info() 函数用于返回指定散列(hash)的相关信息. PHP 版本要求: PHP 5 >= 5.5.0, PHP 7高佣联盟 www.cgewang.com 语法 ...
- PHP ftp_quit() 函数
定义和用法 ftp_quit() 函数关闭 FTP 连接. 语法 ftp_quit(ftp_connection) 参数 描述 ftp_connection 必需.规定要关闭的 FTP 连接. 提示和 ...
- 学学Viewbinding
Viewbinding 1.环境需求 环境上,需要Android Studio 3.6 Canary 11+ 同样的Gradle也需要升级(这年头都4.0了,应该没有还在用低版本的了吧...) 2.配 ...
- Pytorch_第六篇_深度学习 (DeepLearning) 基础 [2]---神经网络常用的损失函数
深度学习 (DeepLearning) 基础 [2]---神经网络常用的损失函数 Introduce 在上一篇"深度学习 (DeepLearning) 基础 [1]---监督学习和无监督学习 ...