go-micro v3入门之hello world

 阅读大约需要3分钟

前面一篇文章https://yingzhou.online/zh-cn/2021/09/go-micro-v3%E5%85%A5%E9%97%A8/ 介绍了什么是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文件。