找不到服务器micro,go
我按照go-micro官网介绍,写了一个demo,但是发现服务器可以运行,客服端显示找不到服务器,为什么呢?
server端:
package main
import (
"context"
"fmt" "github/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/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"}
找不到服务器micro,go
我按照go-micro官网介绍,写了一个demo,但是发现服务器可以运行,客服端显示找不到服务器,为什么呢?
server端:
package main
import (
"context"
"fmt" "github/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/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"}