Hello world of go-micro v3 entry

 4 minutes to read

The previous article https://yingzhou.online/2021/09/introduction-to-go-micro-v3 Introduced what is go-micro v3. In this article, let’s learn about the hello world of go-micro v3.

Let’s install gomu first. Gomu is a command line tool to help us develop go-micro projects.

Execute the following commands in the command line window:

go install github.com/asim/go-micro/cmd/gomu@latest

In this way, an executable binary file will be generated in the bin directory of gopath. Remember to add $GOPATH/bin to the environment variable. So we can use the gomu command on the command line.

Execute the gomu -h command, we can see the commands supported by 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)

Let’s create a new service using gomu new command

$ gomu new service helloworld Then execute the following command

cd helloworld
make proto tidy
gomu run

So we start a service, and then we can execute the following command

gomu call helloworld Helloworld.Call'{"name": "John"}'

Call the started service, and then you can see the information returned by the service

{"msg":"Hello John"}

Let’s take a look at what the commands in the Makefile file do

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 is used to generate .micro.go files, and protoc-gen-go is used to generate .pb.go files

The address github.com/golang/protobuf/protoc-gen-go is obsolete, we should use the new address google.golang.org/protobuf/cmd/protoc-gen-go

The correct command should be

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

From github, both protoc-gen-micro and gomu are under development, and errors will inevitably occur.

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

This command is to generate .pb.go and .pb.micro.go files.