package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
imooc "showproto/proto/cap"
)
type CapServer struct {}
//需要实现的方法
func (c *CapServer) SayHello(ctx context.Context,req *imooc.SayRequest,res *imooc.SayResponse) error {
res.Answer="我们的口号是"+req.Message
return nil
}
func main() {
//创建新的服务
service:=micro.NewService(
micro.Name("cap.imooc.server"),
)
//初始化方法
service.Init()
//注册服务
imooc.RegisterCapHandler(service.Server(),new(CapServer))
//运行服务
if err:=service.Run();err!=nil{
fmt.Println(err)
}
}
syntax ="proto3";
option go_package = "/proto/cap/;cap";
service Cap {
rpc SayHello(SayRequest) returns(SayResponse) {}
}
message SayRequest {
string message =1;
}
message SayResponse {
string answer =1;
}
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
imooc "showproto/proto/cap"
)
func main() {
//创建新的服务
service:=micro.NewService(
micro.Name("cap.imooc.client"),
)
//初始化
service.Init()
capImooc:= imooc.NewCapService("cap.imooc.server",service.Client())
res,err:=capImooc.SayHello(context.TODO(),&imooc.SayRequest{
Message: "跟着 cap老师学习微服务"})
if err!=nil{
fmt.Println(err)
}
fmt.Print(res.Answer)
}