小編給大家分享一下Golang中g(shù)rpc怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比通城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式通城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋通城地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
1. grpc安裝
pip install --upgrade pip
pip install grpcio --user
pip install protobuf --user
pip install grpcio-tools --user
sudo yum -y install protobuf-compiler protobuf-static protobuf protobuf-devel
dnf install protobuf-compiler.x86_64
pip install googleapis-common-protos --user
2. 導(dǎo)入grpc包
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/protoc-gen-go
3. 編寫test.proto文件
// 指定版本
syntax = "proto3";
option objc_class_prefix = "HLW";
// 定義包名
package demo;
// 定義服務(wù)
service ServerBase{
// 定義接口
// 方法
rpc MakeMD5(Request) returns (Response){}
rpc SayHello(HelloRequest) returns (HelloReplay){}
}
//請(qǐng)求的結(jié)構(gòu)體
message HelloRequest{
// 類型 字段 = 標(biāo)識(shí)號(hào)
string name = 1;
}
//返回的結(jié)構(gòu)體
message HelloReplay{
string message = 1;
}
// 定義請(qǐng)求結(jié)構(gòu)體
message Request{
string Data = 1;
}
// 定義返回接口體數(shù)據(jù)
message Response{
string Msg = 1;
}4. 生成.go文件
protoc --go_out=plugins=grpc: . test.proto
這時(shí)候目錄低下會(huì)自動(dòng)生成test.pb.go文件
5. 簡(jiǎn)單的書寫server.go
package main
import (
"crypto/md5"
"errors"
"fmt"
"github.com/srlemon/note/grpc_"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
)
const (
PORT = ":5003"
)
func main() {
// 開啟監(jiān)聽
lis, err := net.Listen("tcp", PORT)
if err != nil {
log.Fatal(err)
}
// new
s := grpc.NewServer()
// 注冊(cè)服務(wù)
demo.RegisterServerBaseServer(s, &Serve{})
log.Println("rpc服務(wù)已經(jīng)開啟")
s.Serve(lis)
}
// Serve 服務(wù)端
type Serve struct {
}
// MakeMD5 方法
func (s *Serve) MakeMD5(ctx context.Context, req *demo.Request) (ret *demo.Response, err error) {
if req == nil {
err = errors.New("請(qǐng)求數(shù)據(jù)為空")
return
}
md := md5.New()
data := md.Sum([]byte(req.Data))
ret = new(demo.Response)
ret.Msg = fmt.Sprintf("%x", data)
return
}
// SayHello 方法
func (s *Serve) SayHello(ctc context.Context, req *demo.HelloRequest) (ret *demo.HelloReplay, err error) {
if req == nil {
err = errors.New("請(qǐng)求數(shù)據(jù)為空")
return
}
ret = new(demo.HelloReplay)
ret.Message = req.Name
return
}
go run server.go 開啟rpc服務(wù)
6. 寫client.go文件
package main
import (
"fmt"
demo "github.com/srlemon/note/grpc_"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
)
const (
addr = "127.0.0.1:5003"
)
func main() {
ctx := context.Background()
// 連接代理
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
log.Fatal("did no connect", err)
}
defer conn.Close()
// 生成一個(gè)客戶端
client := demo.NewServerBaseClient(conn)
var (
data *demo.Request
res *demo.Response
)
data = new(demo.Request)
data.Data = "哈哈,和黑"
// 使用服務(wù)端方法
if res, err = client.MakeMD5(ctx, data); err != nil {
log.Fatal("sssssssssss", err)
}
// 輸出進(jìn)行加密后的信息
fmt.Println(string(res.Msg))
// 使用服務(wù)端方法
_d, _ := client.SayHello(ctx, &demo.HelloRequest{Name: "哈哈哈"})
// 輸出要說的內(nèi)容
fmt.Println(_d.Message)
}
go run client.go
終端輸出:

以上是“Golang中g(shù)rpc怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:Golang中g(shù)rpc怎么用
當(dāng)前URL:http://chinadenli.net/article22/ipsocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、定制開發(fā)、ChatGPT、、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)