I have just cloned the example code from micro/go-grpc and tried to build a grpc server in localhost.
proto file
syntax = "proto3";
package go.micro.srv.greeter;
service Say {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
server/main.go
package main
import (
"context"
"log"
"github.com/micro/go-micro"
"github.com/micro/go-grpc"
hello "github.com/micro/go-grpc/examples/greeter/server/proto/hello"
)
type Say struct{}
func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp
*hello.Response) error {
log.Print("Received Say.Hello request")
rsp.Msg = "Hello " + req.Name
return nil
}
func main() {
service := grpc.NewService(
micro.Name("go.micro.srv.greeter"),
)
// optionally setup command line usage
service.Init()
// Register Handlers
hello.RegisterSayHandler(service.Server(), new(Say))
// Run server
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
client/main.go
package main
import (
"context"
"fmt"
"github.com/micro/cli"
"github.com/micro/go-grpc"
hello "github.com/micro/go-grpc/examples/greeter/server/proto/hello"
"github.com/micro/go-micro"
)
var (
// service to call
serviceName string
)
func main() {
service := grpc.NewService()
service.Init(
micro.Flags(cli.StringFlag{
Name: "service_name",
Value: "go.micro.srv.greeter",
Destination: &serviceName,
}),
)
cl := hello.NewSayService(serviceName, service.Client())
rsp, err := cl.Hello(context.TODO(), &hello.Request{
Name: "John",
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(rsp.Msg)
}
My OS is MacOsX, go version is 1.11.1.
When i run the server side example code, everything looks fine:
$ go run ./main.go --server_address:localhost:9999
2018/11/18 20:08:05 Listening on 127.0.0.1:9999
2018/11/18 20:08:05 Broker Listening on [::]:62739
2018/11/18 20:08:05 Registering node: go.micro.srv.greeter-9b2818b0-eb2a-11e8-bfef-720008acb800
But if I run the client side example code, always received:
{"id":"","code":0,"detail":"transport: received the unexpected content-type "text/plain"","status":""}
I tried to remove the --server_address
and still the same. I tried to add the mdns
registry, not working either. I tried to use $ micro health go.micro.srv.greeter
, it returned the same result.
Wonder whats wrong with my setup?
I just got this same error because I was pointing to the wrong port. I'm sure my setup is different from yours and it's a different issue, but the problem was that I was trying to make a GRPC request to an http server, which returned a 404 not found with a content-type of "text/plain" for html. If you have the same problem when removing your server address, it's likely either you aren't reading the param correctly, or the value you have set is still pointing to a place where there is an http server and not a GRPC server.