首页 > 其他分享 >golang http rpc

golang http rpc

时间:2023-11-12 15:58:27浏览次数:30  
标签:Arith http log err args golang rpc type

server 端:

package main

import (
	"errors"
	"log"
	"net"
	"net/http"
	"net/rpc"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

// 定义type
type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func (t *Arith) Divide(args *Args, quo *Quotient) error {
	if args.B == 0 {
		return errors.New("divide by zero")
	}
	quo.Quo = args.A / args.B
	quo.Rem = args.A % args.B
	return nil
}

func main() {

	arith := new(Arith)
	err := rpc.Register(arith)
	if err != nil {
		log.Fatal(err)
	}
	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":1234")
	if err != nil {
		log.Fatal("listen error:", err)
	}
	log.Fatal(http.Serve(l, nil))
}

服务器端还是做几件事

  1. 定义类型,这里是Arith, 是一个type ,

  2. 定义func,有下面的规则
    Only methods that satisfy these criteria will be made available for remote access; other methods will be ignored:

    the method's type is exported.
    the method is exported.
    the method has two arguments, both exported (or builtin) types.
    the method's second argument is a pointer.
    the method has return type error.
    2个参数,第一个一般建一个Args结构体的类型做为输入,第二个是指针类型,方法返回error 。如果不正常,会被忽略。
    函数一般定义成
    func (t *T) MethodName(argType T1, replyType *T2) error
    这套东西还是有一些规范,需要安装他的规则走。

  3. 然后就是rpc.Register(type)

client:

package main

import (
	"fmt"
	"log"
	"net/rpc"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

func main() {
	client, err := rpc.DialHTTP("tcp", "localhost"+":1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}
	// Synchronous call
	args := &Args{12, 8}
	var reply int
	// core
	err = client.Call("Arith.Multiply", args, &reply)
	if err != nil {
		log.Fatal("arith error:", err)
	}
	// 收到请求,同步的
	fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
	// 除法
	var quo Quotient
	err = client.Call("Arith.Divide", args, &quo)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Arith: %d / %d=%d,%d\n", args.A, args.B, quo.Quo, quo.Rem)
}

https://pkg.go.dev/net/rpc

标签:Arith,http,log,err,args,golang,rpc,type
From: https://www.cnblogs.com/gqdw/p/17827278.html

相关文章

  • GitHub-fatal-unable-to-access-https-github-com-Failed-to-connect-to-github-com-p
    title:>-[GitHub]fatal:unabletoaccess'https://github.com/':Failedtoconnecttogithub.comport443:Operationtimedouttags:[github,git]categories:githubdate:2021-11-2311:11:002021年11月,由于众所周知的缘故,连接到github越来越微妙,分享一些MAC......
  • Golang布隆过滤器升级版
    作用:平常使用的布隆过滤器可以用来过滤Redis空数据,避免缓存穿透。升级点:将原本的bool数组位更改为int数组,实现便于删除操作的场景。代码如下:packagemainimport( "fmt")//BloomFilter布隆过滤器typeBloomFilterstruct{ bitArray[]int//升级版结构哈希所落位置+......
  • 如何使用 jest 测试使用 axios 的 httpClient?
    要使用Jest测试使用axios的httpClient,您可以使用Jest提供的模拟功能来伪造对外部API的请求和响应。下面是一个示例测试的代码:首先,安装所需的依赖:npminstallaxiosaxios-mock-adapterjest--save-dev然后,创建一个名为httpService.test.js的测试文件,编写以下代码:importaxiosfrom......
  • 想入坑golang web,向大佬们请教些问题?
    当你准备入坑Go语言的Web开发时,以下是一些常见的问题,你可以向大佬们请教:如何设置和启动一个GoWeb服务器?Go语言有哪些常用的Web开发框架?它们之间有什么区别和优劣势?Go语言中的路由是如何实现的?如何处理不同的HTTP请求方法和URL参数?Go语言如何处理请求和响应,以及如何......
  • golang json 序列化、反序列化 字符串反序列化
    golangjson序列化、反序列化字符串反序列化在使用Golang进行开发时,经常会遇到需要将一段JSON字符串进行序列化和反序列化的情况。JSON是一种轻量级数据交换格式,常用于前后端数据传输、存储等场景。Golang提供了内置的encoding/json包来处理JSON的序列化和反序列化。JSON的序列化......
  • odoo16前端框架源码阅读——rpc_service.js
    odoo16前端框架源码阅读——rpc_service.js先介绍点背景知识,这样方便阅读代码。一、JSONRPC的规范https://www.jsonrpc.org/specification中文翻译版本:https://wiki.geekdream.com/Specification/json-rpc_2.0.htmlJSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。本规......
  • 基于Golang协程实现流量统计系统项目开发
    基于Golang协程实现流量统计系统项目开发上一节课我们已经架设好了一个网站。,但是因为我们的网站没有流量。也生成不了大量的日志,靠我们自己点击生成那点日志也不够测试的。所以这次我们就用GO语言批量生成我们想要的日志。好了。我们开始写代码我用的IDE工具是GOLAND,没有为......
  • Maven打包项目时异常:Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/con
    package是报错Cannotaccessnexus-aliyun(http://maven.aliyun.com/nexus/content/groups/public)inofflinemodeandtheartifactorg.apache.maven.surefire:maven-surefire-common:pom:2.22.2hasnotbeendownloadedfromitbefore.根据翻译的意思:无法在脱机模式下......
  • HTTP代理,什么是HTTP代理?HTTP代理如何设置?HTTP代理的用途?
    HTTP代理可以用于许多不同的目的。其中一些用途包括:访问被封锁的网站:在一些地区,互联网服务提供商可能会限制某些网站或内容。使用HTTP代理可以绕过这些限制,访问网站或内容。加速网站访问:HTTP代理可以缓存常用的网站,从而加速网站访问速度。当用户访问这些网站时,代理服务器会直接返......
  • HTTPS和HTTP的区别是什么?
    广泛应用于互联网世界的HTTP想必是大家再熟悉不过的了,然而细心的朋友可能发现淘宝、百度、网上银行等网站都变成HTTPS开头,并且还有一把小绿锁挂在地址栏,那么HTTPS和HTTP的区别是什么呢?一、什么是HTTPSHTTPS是在HTTP上建立SSL加密层,并对传输数据进行加密,是HTTP协议的安全版。HTTPS......