luslin@local:~/software/zipkin$ MICRO_REGISTRY=etcd micro list services
com.foo.service.micro
go.micro.web
micro.http.broker
com.foo.service.micro 是我们的目标服务,micro.http.broker 是默认的中间件服务。
在调用前,使用get命令查看服务的详情,端点定义,元数据和节点信息等
luslin@local:~/software/zipkin$ MICRO_REGISTRY=etcd micro get service com.foo.service.micro
service com.foo.service.micro
version latest
ID Address Metadata
com.foo.service.micro-06796a43-b7ee-47e9-ae4e-2fbd208de511 192.168.1.88:34441 protocol=grpc,registry=etcd,server=grpc,transport=grpc,broker=http
com.foo.service.micro-fafcc3b2-e1b0-475e-b1b7-45799abb8775 192.168.1.88:43877 broker=http,protocol=grpc,registry=etcd,server=grpc,transport=grpc
Endpoint: Micro.Call
Request: {
name string
}
Response: {
msg string
}
Endpoint: Micro.PingPong
Metadata: stream=true
Request: {}
Response: {}
Endpoint: Micro.Stream
Metadata: stream=true
Request: {}
Response: {}
Endpoint: Micro.Handle
Metadata: subscriber=true,topic=go.micro.service.micro
Request: {
say string
}
Response: {}
通过命令行调用服务
luslin@local:~/software/zipkin$ MICRO_REGISTRY=etcd micro call com.foo.service.micro Micro.Call '{"name":"bill"}'
{
"msg": "Hello bill"
}
luslin@local:~/software/zipkin$ MICRO_REGISTRY=etcd micro cli
micro> help
Commands:
call Call a service
deregister Deregister a service
exit Exit the CLI
get Get service info
health Get service health
help CLI usage
list List services, peers or routes
publish Publish a message to a topic
quit Exit the CLI
register Register a service
stats Get service stats
stream Stream a call to a service
互动模式的命令不同,因为非互动模式下支持了更多的功能
micro> list
com.foo.service.micro
go.micro.web
micro.http.broker
micro> get com.foo.service.micro
service com.foo.service.micro
version latest
ID Address Metadata
com.foo.service.micro-06796a43-b7ee-47e9-ae4e-2fbd208de511 192.168.1.88:34441 broker=http,protocol=grpc,registry=etcd,server=grpc,transport=grpc
com.foo.service.micro-fafcc3b2-e1b0-475e-b1b7-45799abb8775 192.168.1.88:43877 broker=http,protocol=grpc,registry=etcd,server=grpc,transport=grpc
Endpoint: Micro.Call
Request: {
name string
}
Response: {
msg string
}
Endpoint: Micro.PingPong
Metadata: stream=true
Request: {}
Response: {}
Endpoint: Micro.Stream
Metadata: stream=true
Request: {}
Response: {}
Endpoint: Micro.Handle
Metadata: topic=go.micro.service.micro,subscriber=true
Request: {
say string
}
Response: {}
micro> call com.foo.service.micro Micro.Call {"name":"lin"} // 这里不需要使用json字符串
{
"msg": "Hello lin"
}
在启动web后,可以进入http://127.0.0.1:8082/client
创建一个项目,布局如下:
luslin@local:~/go/workspace/tools/micro-hello-cli$ tree
.
├── go.mod
├── go.sum
├── main.go
├── micro_hello_cli
├── plugin.go
└── proto
└── hello
├── micro.pb.go
├── micro.pb.micro.go
└── micro.proto
main.go
package main
import (
"context"
"fmt"
"github.com/micro/go-micro/v2"
hello "micro-hello-cli/proto/hello"
)
func main() {
// New Service
service := micro.NewService( // 创建服务
micro.Name("com.foo.service.micro.clinet"),
micro.Version("latest"),
)
service.Init() // 服务初始化
resp,err := hello.NewMicroService("com.foo.service.micro",service.Client()).Call(context.TODO(),&hello.Request{Name: "lin"}) // 创建client并调用call方法
if err != nil {
panic(err)
}
fmt.Println(resp.Msg)
}
结果
Hello lin