前面一篇文章https://yingzhou.online/zh-tw/2021/09/go-micro-v3%E5%85%A5%E9%96%80/ 介紹了什麼是go-micro v3,本篇文章我們來學習下go-micro v3的hello world。
我們首先安裝一下gomu,Gomu是一個幫助我們開發go-micro項目的命令行工具。
在命令行窗口執行如下命令:
go install github.com/asim/go-micro/cmd/gomu@latest
這樣在gopath的bin目錄就會生成可執行的二進製文件,記得要把$GOPATH/bin加入到環境變量中, 這樣我們就可以在命令行使用gomu命令了。
執行 gomu -h
命令,我們可以看到gomu支持的命令,
NAME:
gomu - The Go Micro CLI tool
USAGE:
gomu [global options] command [command options] [arguments...]
VERSION:
latest
COMMANDS:
call Call a service, e.g. gomu call helloworld Helloworld.Call '{"name": "John"}'
describe Describe a resource
new Create a project template
run Build and run a service continuously
services List services in the registry
stream Create a service stream
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--auth value Auth for role based access control, e.g. service [$MICRO_AUTH]
--auth_id value Account ID used for client authentication [$MICRO_AUTH_ID]
--auth_namespace value Namespace for the services auth account (default: "go.micro") [$MICRO_AUTH_NAMESPACE]
--auth_private_key value Private key for JWT auth (base64 encoded PEM) [$MICRO_AUTH_PRIVATE_KEY]
--auth_public_key value Public key for JWT auth (base64 encoded PEM) [$MICRO_AUTH_PUBLIC_KEY]
--auth_secret value Account secret used for client authentication [$MICRO_AUTH_SECRET]
--broker value Broker for pub/sub. http, nats, rabbitmq [$MICRO_BROKER]
--broker_address value Comma-separated list of broker addresses [$MICRO_BROKER_ADDRESS]
--client value Client for go-micro; rpc [$MICRO_CLIENT]
--client_pool_size value Sets the client connection pool size. Default: 1 (default: 0) [$MICRO_CLIENT_POOL_SIZE]
--client_pool_ttl value Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m [$MICRO_CLIENT_POOL_TTL]
--client_request_timeout value Sets the client request timeout. e.g 500ms, 5s, 1m. Default: 5s [$MICRO_CLIENT_REQUEST_TIMEOUT]
--client_retries value Sets the client retries. Default: 1 (default: 1) [$MICRO_CLIENT_RETRIES]
--config value The source of the config to be used to get configuration [$MICRO_CONFIG]
--profile value Debug profiler for cpu and memory stats [$MICRO_DEBUG_PROFILE]
--register_interval value Register interval in seconds (default: 30) [$MICRO_REGISTER_INTERVAL]
--register_ttl value Register TTL in seconds (default: 60) [$MICRO_REGISTER_TTL]
--registry value Registry for discovery. etcd, mdns [$MICRO_REGISTRY]
--registry_address value Comma-separated list of registry addresses [$MICRO_REGISTRY_ADDRESS]
--runtime value Runtime for building and running services e.g local, kubernetes [$MICRO_RUNTIME]
--runtime_source value Runtime source for building and running services e.g github.com/micro/service (default: "github.com/micro/services") [$MICRO_RUNTIME_SOURCE]
--selector value Selector used to pick nodes for querying [$MICRO_SELECTOR]
--server value Server for go-micro; rpc [$MICRO_SERVER]
--server_address value Bind address for the server. 127.0.0.1:8080 [$MICRO_SERVER_ADDRESS]
--server_advertise value Used instead of the server_address when registering with discovery. 127.0.0.1:8080 [$MICRO_SERVER_ADVERTISE]
--server_id value Id of the server. Auto-generated if not specified [$MICRO_SERVER_ID]
--server_metadata value A list of key-value pairs defining metadata. version=1.0.0 [$MICRO_SERVER_METADATA]
--server_name value Name of the server. go.micro.srv.example [$MICRO_SERVER_NAME]
--server_version value Version of the server. 1.1.0 [$MICRO_SERVER_VERSION]
--store value Store used for key-value storage [$MICRO_STORE]
--store_address value Comma-separated list of store addresses [$MICRO_STORE_ADDRESS]
--store_database value Database option for the underlying store [$MICRO_STORE_DATABASE]
--store_table value Table option for the underlying store [$MICRO_STORE_TABLE]
--tracer value Tracer for distributed tracing, e.g. memory, jaeger [$MICRO_TRACER]
--tracer_address value Comma-separated list of tracer addresses [$MICRO_TRACER_ADDRESS]
--transport value Transport mechanism used; http [$MICRO_TRANSPORT]
--transport_address value Comma-separated list of transport addresses [$MICRO_TRANSPORT_ADDRESS]
--help, -h show help (default: false)
--version, -v print the version (default: false)
讓我們使用gomu new 命令創建一個新的服務
$ gomu new service helloworld
接著執行如下命令
cd helloworld
make proto tidy
gomu run
這樣我們就啟動了一個服務,接著我們可以執行如下命令 gomu call helloworld Helloworld.Call '{"name": "John"}'
調用啟動的服務,之後可以看到服務返回的信息 {"msg":"Hello John"}
我們看一下Makefile文件中命令都做了些什麼
go get -u google.golang.org/protobuf/proto
go install github.com/golang/protobuf/protoc-gen-go@latest
go install github.com/asim/go-micro/cmd/protoc-gen-micro/v3@latest
protoc-gen-micro用於生成.micro.go文件,protoc-gen-go用於生成.pb.go文件
github.com/golang/protobuf/protoc-gen-go這個地址已經廢棄,我們應該使用新的地址google.golang.org/protobuf/cmd/protoc-gen-go
正確的命令應該是
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
從github上看protoc-gen-micro和gomu都是在開發狀態難免會發生錯誤。
github.com/asim/go-micro/cmd/gomu v0.0.0-20210904061721-270d910b7328 // indirect
github.com/asim/go-micro/cmd/protoc-gen-micro/v3 v3.0.0-20210904061721-270d910b7328 // indirect
make proto
protoc --proto_path=. --micro_out=. --go_out=:. proto/helloworld.proto
這行命令就是生成.pb.go和.pb.micro.go文件。