微服务里得HelloWorld
作者:互联网
greeter.proto
protoc greeter.proto
syntax = "proto3";
//服务
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
//输入参数
message HelloRequest {
string name = 1;
}
//输出参数
message HelloResponse {
string greeting = 2;
}
server.go
package main
import (
"context"
"log"
"time"
proto "github.com/micro/examples/helloworld/proto"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry/consul"
"github.com/micro/go-micro/registry"
)
type Greeter struct{}
//平时写业务就是实现这里
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
//指定服务发现的网络位置(也可以在命令行指定)
reg := consul.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
"wuxiangege.com:8500",//公网
"127.0.0.1:8500",//内网
}
})
//初始化
service := micro.NewService(
micro.Registry(reg), //指定服务发现的网络位置
micro.Name("greeter"), //微服务名字
micro.RegisterTTL(time.Second*30), //微服务信息过期时间
micro.RegisterInterval(time.Second*20),//再次注册的时间间隔
)
service.Init()
//关联
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
运行微服务
go run main.go
服务发现
标签:服务,service,proto,HelloWorld,micro,go,Greeter,com 来源: https://blog.csdn.net/wuxiangege/article/details/99091101