最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

找不到服务器micro,go

IT圈 admin 35浏览 0评论

找不到服务器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"}

发布评论

评论列表 (0)

  1. 暂无评论