go-micro搭建

------------------------------------
 go get github.com/micro/micro/v2  //會在GOPATH的bin目錄下安裝一個micro將其路徑加入環境變量

micro --version查看是否安裝成功
 brew install etcd
 # install
brew install etcd
# run
etcd 
# Use flag
micro --registry=etcd list services

# Use env var
MICRO_REGISTRY=etcd micro list services`
#指定etcd不在同一主機上的地址。
MICRO_REGISTRY_ADDRESS=127.0.0.1:2379

# install protobuf 本機已安裝,故註釋
#brew install protobuf

# install protoc-gen-go 
#go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

# install protoc-gen-micro
#go get -u github.com/micro/protoc-gen-micro/v2
##生成示例模板
micro new github.com/micro/example


##該命令將輸出

#example/
#    Dockerfile    # A template docker file
#    README.md    # A readme with command used
#    handler/    # Example rpc handler
#    main.go        # The main Go program
#    proto/        # Protobuf directory
#    subscriber/    # Example pubsub Subscriber##

#cd 到 example目錄
protoc --proto_path=. --micro_out=. --go_out=. proto/example/example.proto
# cd into your service directory
cd github.com/micro/example

# run the service
micro run service --local  ##linux未通過
##啓動模板服務
go run main.go

##查看到 go.micro.srv.example
micro list services

##輸出量
service  go.micro.srv.example
version latest
micro get service go.micro.srv.example

通過CLI進行RPC調用。查詢以json的形式發送。
micro call go.micro.srv.example Example.Call '{"name": "John"}'
輸出量

{
    "msg": "Hello John"
}

MICRO_API_HANDLER=rpc \
MICRO_API_NAMESPACE=go.micro.srv \
micro api

使用json向api發出POST請求
curl -XPOST -H 'Content-Type: application/json' -d '{"name": "John"}' http://localhost:8080/example/call
輸出量
{"msg":"Hello John"}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章