You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"context""fmt""github.com/micro/go-micro"
proto "watching/proto"
)
typeGreeterstruct{}
func (g*Greeter) Hello(ctx context.Context, req*proto.HelloRequest, rsp*proto.HelloResponse) error {
rsp.Greeting="Hello "+req.Namereturnnil
}
funcmain() {
// Create a new service. Optionally include some options here.service:=micro.NewService(
micro.Name("greeter"),
)
// Init will parse the command line flags.service.Init()
// Register handlerproto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the serveriferr:=service.Run(); err!=nil {
fmt.Println(err)
}
}
go get
go get
运行server
go run main.go
client/client.go
package main
import (
"context""fmt""github.com/micro/go-micro"
proto "watching/proto"
)
funcmain() {
// Create a new service. Optionally include some options here.service:=micro.NewService(micro.Name("greeter.client"))
service.Init()
// Create new greeter clientgreeter:=proto.NewGreeterService("greeter", service.Client())
// Call the greeterrsp, err:=greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
iferr!=nil {
fmt.Println(err)
}
// Print responsefmt.Println(rsp.Greeting)
}
运行Client
go run client/client.go
micro cli
//非项目目录
go get github.com/micro/micro
➜ micro list services
greeter
The text was updated successfully, but these errors were encountered:
go-micro 入门实战第一篇
目录结构
安装
greeter.proto
gen
main.go
go get
运行server
client/client.go
运行Client
micro cli
The text was updated successfully, but these errors were encountered: