[raw] go micro

房东的猫 提交于 2020-02-28 09:30:42

go-micro

1 安装

  • 安装golang version>=1.13
  • micro-gen: go get github.com/micro/protoc-gen-micro/v2
protoc --proto_path=. --micro_out=. --go_out=. proto/greeter.proto
package main

import (
	"context"
	"fmt"
	proto "micro_test/proto"

	"github.com/micro/go-micro/v2"
)

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))

	// Run the server
	if err := service.Run(); err != nil {
		fmt.Println(err)
	}
}


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!