In the previous article, we created a service, and now we can invoke it. The easiest way to do it is via the versatile command-line tool micro .

Calling service with command-line tools

First, we use the command list to list all available services:

$ micro list services com.foo.service.hello

go.micro.nats.broker

The service named com.foo.srv.hello is our target service, and go.micro.http.broker is the default implementation of the message broker service, which will not be discussed for now.

Before calling, we can use the command get to view the details of this service, which includes the service version, endpoint definition, metadata and node information, etc.

$ micro get service com.foo.service.hello

service com.foo.service.hello version latest ID Address Metadata

com.foo.service.hello-c992ec4e-7cf2-480e-881e-ff7371378b98 192.168.3.184:59610 transport=grpc,broker=eats,protocol=grpc,registry=mdns,server=grpc Endpoint: Hello.Call Request: {

name string

}

Response: {

msg string

} ...

Then we can invoke the service with the command call :

$ micro call com.foo.service.hello Hello.Call '{"name": "Bill"}' error calling com.foo.service.hello.Hello.Call:

{"id":"go.micro.client","code":408,"detail":"context deadline exceeded","status":"Request Timeout"}

We constructed the request in JSON string (which quoted with single quotes) and invoke the service. Unfortunately, we got an error. This is a known bug of Micro v2, hope it could be resolved soon.

In addition, micro also supports interactive mode.

Calling service in interactive mode

After calling the cli command, we will enter interactive mode:

$ micro cli

micro>

Note: The commands in interactive and non-interactive modes are not identical. For example, the interactive command to list all services is list instead of list services . The reason is that in the non-interactive mode, micro needs to support richer semantics (for example, you can use list nodes to list all nodes in the network). IMO this is a design decision, it brings some conceptual inconsistencies and is not easy for understanding.

$ micro cli

micro> list

com.foo.service.hello

go.micro.nats.broker micro>

micro> get com.foo.service.hello

service com.foo.service.hello version latest ID Address Metadata

com.foo.service.hello-df4e5d69-9226-4c12-9b1e-1c06def9426e 192.168.3.184:49297 broker=eats,protocol=grpc,registry=mdns,server=grpc,transport=grpc

... micro>

micro> call com.foo.service.hello Hello.Call {"name": "Bill2"} error calling com.foo.service.hello.Hello.Call: {"id":"go.micro.client","code":408,"detail":"context deadline exceeded","status":"Request Timeout"} micro>

Note that we don’t need to quote the JSON string when building the request in interactive mode. The call command fails too, due to the aforementioned bug.

Calling service with web-based client

Micro also provides a web-based client that supports users to call services in a browser.

After running micro web , you will find the following interface at http://127.0.0.1:8082/client. You can call the selected service/endpoint by clicking the “Execute” button, then the response content will be shown on the right side in JSON format.

Calling service via code

The methods mentioned above just facilitate runtime debugging. In general, we call service via code.

Let’s create a client project with the layout as below:

.

├── main.go

├── plugin.go

├── proto/hello

│ └── hello.proto

│ └── hello.pb.go

│ └── hello.pb.micro.go

├── go.mod

├── go.sum

Except for the main.go, the content of other files are the same as described in the previous article, and will not be repeated here.

Here’re the codes in main.go：

client/main.go

First, create and initialize an instance of micro.Service and name it as “ com.foo.service.hello.client” . The name has no special meaning and is likely to be different in real projects.

and name it as “ . The name has no special meaning and is likely to be different in real projects. Then create a client instance of HelloSevice . The first parameter we pass to the method hello.NewHelloService is the name of the service to be invoked. The second parameter is an instance of github.com/micro/go-micro/v2/client.Client, which is obtained from the service instance.

. The first parameter we pass to the method is the name of the service to be invoked. The second parameter is an instance of which is obtained from the service instance. The next step is to call the service method. First, pass in the context.Context instance. Here, we use context.TODO() to simplify the example. In a real project, you should pass in the actual context instance; then we pass in the request object.

After codes preparation, you can compile and run:

$ go run main.go plugin.go

Hello Bill 4

$

The expected response will be displayed.

If the service is not running at this time, an error will be reported:

$ go run main.go plugin.go

{"id":"go.micro.client","code":500,"detail":"service com.foo.service.hello: not found","status":"Internal Server Error"}

$

At this point, you’re done.