go-micro 找不到服务,server not found是什么原因?编译出来在linux下面可以,windows不行。

我按照go-micro官网介绍,写了一个demo,但是发现服务器可以运行,客服端显示找不到服务器,为什么呢?

server端:

package main  
  
import (  
   "context"  
 "fmt" "github.com/micro/go-micro"  proto "microservice/src/server/proto"  
)  
  
type Greeter struct{}  
  
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {  
   rsp.Greeting = "Hello " + req.Name  
   return nil  
}  
  
func main() {  
   // 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 handler  
  _ = proto.RegisterGreeterHandler(service.Server(), new(Greeter))  
   fmt.Println(service.Name())  
   // Run the server  
  if err := service.Run(); err != nil {  
      fmt.Println(err)  
   }  
}

client端

package main  
  
import (  
   "context"  
 "fmt" "github.com/micro/go-micro"  proto "microservice/src/server/proto"  
)  
  
func main() {  
   // Create a new service  
  service := micro.NewService(  
      micro.Name("greeter.client"),  
  )  
   // Initialise the client and parse command line flags  
  service.Init()  
  
   // Create new greeter client  
  greeter := proto.NewGreeterService("greeter", service.Client())  
  
   // Call the greeter  
  rsp, err := greeter.Hello(context.TODO(), &proto.Request{Name: "John"})  
   if err != nil {  
      fmt.Println(err)  
   }  
  
   // Print response  
  fmt.Println(rsp.Greeting)  
}

proto

syntax = "proto3";  
  
service Greeter {  
   rpc Hello(Request) returns (Response) {}  
}  
  
message Request {  
   string name = 1;  
}  
  
message Response {  
   string greeting = 2;  
}

提示:

{"id":"go.micro.client","code":500,"detail":"service greeter: not found","status":"Internal Server Error"}
阅读 9k
2 个回答

windows下无法使用命令行参数,接受不到 --registry=consul

func init(){  
   reg := consul.NewRegistry(func(op *registry.Options) {  
      op.Addrs = []string{  
         "127.0.0.1:8500",  
      }  
   })  
   // 创建apigw的对象及一个api的rpc service  
  service := micro.NewService()  
   // 初始化,绑定registry参数 
  service.Init(micro.Registry(reg))  
   cli := service.Client()  
  
   // 初始化一个rpcClient  
  userCli = proto.NewUserService("go.micro.service.user",cli)  
}

init中将命令行要用的参数直接做进去就可以使用了,效果等同于linux系统下加命令行参数

我的正好是在linux下不行(开启防火墙的情况下),windows下可以。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题