日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

go微服务框架-gomicro试用

發(fā)布時(shí)間:2025/6/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go微服务框架-gomicro试用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

主要參考資料:

????https://segmentfault.com/a/1190000017572032

?

consul

????- 安裝

? ? ? ? 參考:https://www.cnblogs.com/speeddog/p/7183943.html

????????啟動(dòng)命令改為

????????????consul agent -data-dir=/usr/local/consuldatal -server -bootstrap -bind 127.0.0.1 -dev

? ? ??

? ? ? ? ? ? 嘗試設(shè)置為外網(wǎng)地址,被提示有錯(cuò)誤,只好用回本地IP:

????????????????Error starting agent: Failed to start Consul server: Failed to start RPC layer: listen tcp 外網(wǎng)IP:8300: bind: cannot assign requested address

???????、

安裝go-micro框架???????

????- go get github.com/micro/micro

????????隱含需要:

????????????git支持

????????????????yum -y install git

????????????protoc支持

git clone https://github.com/google/protobuf.git cd protobuf/ git clone https://github.com/google/googlemock.git mv googlemock gmock ./autogen.sh ./configure make make check make installexport LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64

?

代碼編譯

????- proto文件

????????- 文件

????????????proto/hello.proto

????????- 內(nèi)容

syntax = "proto3"; service Hello {rpc Ping(Request) returns (Response) {} }message Request {string name = 1; }message Response {string msg = 1; }

????????- 編譯

????????????protoc --go_out=. --micro_out=. ./proto/hello.proto

????????- 結(jié)果

????????????hello.micro.go??

????????????hello.pb.go??

????- service

????????- 文件

????????????services/hello.go

????????- 內(nèi)容

package mainimport ("context""fmt"proto "../proto"???????????????????????????// 路徑需指向上述 proto 文件所在目錄micro "github.com/micro/go-micro" )type Hello struct{}func (h *Hello) Ping(ctx context.Context, req *proto.Request, res *proto.Response) error {res.Msg = "Hello " + req.Namereturn nil }func main() {service := micro.NewService(micro.Name("hellooo"), // 服務(wù)名稱(chēng))service.Init()proto.RegisterHelloHandler(service.Server(), new(Hello))if err := service.Run(); err != nil {fmt.Println(err)} }

????????- 運(yùn)行

????????????go run services/hello.go

????- client

????????- 文件

????????????clients/helloclient.go

????????- 內(nèi)容

package mainimport ("context""fmt"proto "../proto"micro "github.com/micro/go-micro" )func main() {service := micro.NewService(micro.Name("hello.client")) // 客戶(hù)端服務(wù)名稱(chēng)service.Init()helloservice := proto.NewHelloService("hellooo", service.Client())res, err := helloservice.Ping(context.TODO(), &proto.Request{Name: "World ^_^"})if err != nil {fmt.Println(err)}fmt.Println(res.Msg) }

????????- 運(yùn)行

????????????go run clients/helloclient.go

FAQ:????????????

????Q:protoc-gen-go: program not found or is not executable

????A:

????????需要加入環(huán)境變量

????????????在~/.bashrc 中加入,并使生效

????????????export PATH = "PATH:$GOPATH/bin"

????????????

轉(zhuǎn)載于:https://my.oschina.net/kakablue/blog/3050248

總結(jié)

以上是生活随笔為你收集整理的go微服务框架-gomicro试用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。