go微服务框架-gomicro试用
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)題。
- 上一篇: Spring系列教程四:Spring对B
- 下一篇: java版spring cloud+sp