銆愬垝姘寸郴鍒椼€慻o-zero rpc demo
Posted ronething
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬垝姘寸郴鍒椼€慻o-zero rpc demo相关的知识,希望对你有一定的参考价值。
馃挰
璋佹潵鍓湀鍏夈€?/p>
馃捇
鏈€杩戝紑濮嬪涔?go-zero[1] 寮€濮嬩娇鐢ㄦ妗嗘灦鏉ヨ繘琛岄」鐩紑鍙戯紝(鍙兘寰堝浜洪兘鐭ラ亾杩欎釜鐜板湪姣旇緝鐩涜鐨勬鏋朵簡)銆傛帴涓嬫潵鍏堜粠鏈€绠€鍗曠殑 rpc sample 璇磋捣锛屼綋楠屼竴涓?goctl 鐢熸垚閫氱敤浠g爜鐨勮兘鍔涳紝浠ュ強鍚庣画鐨勫睍鏈涖€?/p>
鐜瀹夎
1銆佸畨瑁?go 鐜锛屽彲浠ヨ€冭檻浣跨敤 gvm[2] 鏉ュ仛 go 澶氱増鏈幆澧冪鐞?/p>
2銆佸紑鍚?go module锛岄厤缃?goproxy
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io # 涔熷彲浠ヤ娇鐢?nbsp;goproxy.cn
3銆佸畨瑁?goctl
go get -u github.com/tal-tech/go-zero/tools/goctl
# 姝e父瀹夎鎴愬姛涔嬪悗 浜岃繘鍒剁▼搴忎細鍦?nbsp;$GOPATH/bin 鐩綍涓?nbsp;鍙€氳繃 goctl -v 鏌ョ湅鐗堟湰杩涜瀹夎缁撴灉楠岃瘉
4銆佸畨瑁?protoc & protoc-gen-go
-
protoc
杩涘叆 releases[3] 椤甸潰涓嬭浇瀵瑰簲绯荤粺鐨勫帇缂╁寘
PROTOC_ZIP=protoc-3.15.6-linux-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/$PROTOC_ZIP
unzip $PROTOC_ZIP
mv bin/protoc $GOPATH/bin
# protoc --version
-
瀹夎 protoc-gen-go
go get -u github.com/golang/protobuf/protoc-gen-go@v1.3.2
绀轰緥寮€鍙?span class="mq-48">
-
鍒濆鍖栭」鐩?
cd Documents
mkdir go-zero-sample & cd go-zero-sample
go mod init go-zero-sample
go get -u github.com/tal-tech/go-zero
-
鐢熸垚 proto 妯$増
goctl rpc template -o=hello.proto
# ashing @ ashing-virtual-machine in ~/Documents/go-zero-sample [0:57:41] C:1
$ cat hello.proto
syntax = "proto3";
package hello;
message Request {
string ping = 1;
}
message Response {
string pong = 1;
}
service Hello {
rpc Ping(Request) returns(Response);
}
-
鏍规嵁 proto 鏂囦欢鐢熸垚 rpc 鏈嶅姟鐨勭浉搴旀枃浠?
goctl rpc proto -src hello.proto -dir .
鐩綍缁撴瀯濡備笅
# ashing @ ashing-virtual-machine in ~/Documents/go-zero-sample [0:51:07]
$ tree
.
鈹溾攢鈹€ etc
鈹?nbsp; 鈹斺攢鈹€ hello.yaml
鈹溾攢鈹€ go.mod
鈹溾攢鈹€ go.sum
鈹溾攢鈹€ hello
鈹?nbsp; 鈹斺攢鈹€ hello.pb.go
鈹溾攢鈹€ helloclient
鈹?nbsp; 鈹斺攢鈹€ hello.go
鈹溾攢鈹€ hello.go
鈹溾攢鈹€ hello.proto
鈹斺攢鈹€ internal
鈹溾攢鈹€ config
鈹?nbsp; 鈹斺攢鈹€ config.go
鈹溾攢鈹€ logic
鈹?nbsp; 鈹斺攢鈹€ pinglogic.go
鈹溾攢鈹€ server
鈹?nbsp; 鈹斺攢鈹€ helloserver.go
鈹斺攢鈹€ svc
鈹斺攢鈹€ servicecontext.go
8 directories, 11 files
-
鍩虹璁炬柦閰嶇疆
鏈嶅姟娉ㄥ唽鍙戠幇渚濊禆浜?etcd[4]锛屾墍浠ユ湰鍦伴渶瑕佸厛鍚姩涓€涓?etcd 鏈嶅姟绔?/p>
wget https://github.com/etcd-io/etcd/releases/download/v3.2.32/etcd-v3.2.32-linux-amd64.tar.gz
tar -zxvf etcd-v3.2.32-linux-amd64.tar.gz
cd etcd-v3.2.32-linux-amd64
./etcd
骞朵娇鐢?etcdctl watch 鍓嶇紑涓?hello.rpc
鐨?key
ETCDCTL_API=3 etcdctl watch hello.rpc --prefix
-
涓氬姟閫昏緫淇敼
// internal/logic/pinglogic.go
func (l *PingLogic) Ping(in *hello.Request) (*hello.Response, error) {
return &hello.Response{
Pong: fmt.Sprintf("hello: %s", in.Ping),
}, nil
}
-
鍚姩鏈嶅姟
# 鍦ㄩ」鐩牴鐩綍鎵ц
go run hello.go
-
瀹㈡埛绔姹傛祴璇?
package helloclient
import (
"context"
"github.com/tal-tech/go-zero/core/discov"
"github.com/tal-tech/go-zero/zrpc"
"testing"
)
func TestPing(t *testing.T) {
client := zrpc.MustNewClient(zrpc.RpcClientConf{
Etcd: discov.EtcdConf{
Hosts: []string{"127.0.0.1:2379"},
Key: "hello.rpc",
},
})
h := NewHello(client)
resp, err :=h.Ping(context.TODO(), &Request{
Ping: "ashing",
})
if err != nil {
t.Error(err)
return
}
t.Log(resp.GetPong())
}
# ashing @ ashing-virtual-machine in ~/Documents/go-zero-sample/helloclient [1:36:20] C:2
$ go test -v -test.run TestPing
=== RUN TestPing
2021/03/30 01:36:45 {"@timestamp":"2021-03-30T01:36:45.648+08","level":"stat","content":"p2c - conn: 127.0.0.1:8989, load: 824, reqs: 1"}
hello_test.go:28: hello: ashing
--- PASS: TestPing (0.01s)
PASS
ok go-zero-sample/helloclient 0.014s
-
灏忕粨
鑷虫锛屼竴涓娇鐢?go-zero 鎼缓鐨?rpc demo 宸插畬鎴愶紝鍙互鐪嬪埌涓昏鐨勫伐浣滃湪浜?proto 鏂囦欢鐨勭紪鍐欎互鍙婁笟鍔¢€昏緫鐨勭紪鍐欙紝goctl 浼氱敓鎴愮浉鍏崇殑浠g爜锛屽綋鐒惰繖鏄悊鎯虫儏鍐碉紝渚嬪鎴戜滑闇€瑕佷娇鐢ㄥ叾浠栫殑涓€浜涗腑闂翠欢鏁版嵁搴撻厤缃偅涔堝簲璇ユ€庝箞鍔炲憿锛屽叾瀹炲氨娑夊強鍒颁竴浜涘搴旂殑淇敼銆備箣鍚庣户缁ˉ鍏呫€?/p>
馃幐
鎵€浠ユ殏鏃跺皢浣犵溂鐫涢棴浜嗚捣鏉
以上是关于銆愬垝姘寸郴鍒椼€慻o-zero rpc demo的主要内容,如果未能解决你的问题,请参考以下文章