go-micro微服务 -利用etcd服务注册
本篇是对上一篇go-micro开发第一个微服务 的升级版
在本机安装etcd
从官网下载编译文件 https://github.com/etcd-io/etcd/releases ,选择适合自己的版本,然后直接运行etcd 文件即可。
命令行执行 etcd
开始编写代码
服务端 :
microserver/proto/hello.proto
syntax = "proto3";
package myfirstmicro;
option go_package = "proto;mymicro";
service Hi {
rpc MyHello (HelloReq) returns (HelloRes) ;
}
message HelloReq {
string Name = 1;
}
message HelloRes {
string ResName = 1 ;
}
在项目目录下执行:protoc -I=./proto --go_out=. --micro_out=. ./proto/hello.proto
生成 microserver/proto/hello.pb.go 和 microserver/proto/hello.pb.micro.go两个文件
然后编写 microserver/hanlder/hello.go:
package hanlder
import (
"context"
"fmt"
proto "microserver/proto"
)
type MyhelloStruct struct {
}
func (m *MyhelloStruct) MyHello(ctx context.Context, req *proto.HelloReq, res *proto.HelloRes) error {
fmt.Print("11111")
res.ResName = "hello" + req.Name
return nil
}
编写main.go文件
package main
import (
"microserver/hanlder"
proto "microserver/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/etcd"
)
func main() {
// create a new service
service := micro.NewService(
micro.Name("helloworld"),
//etcd注册服务
micro.Registry(etcd.NewRegistry(registry.Addrs("127.0.0.1:2379"))),
)
// initialise flags
service.Init()
proto.RegisterHiHandler(service.Server(), &hanlder.MyhelloStruct{})
// start the service
service.Run()
}
项目启动:go run main.go
客户端 :
microclient/main.go
package main
import (
"context"
"fmt"
pb "microserver/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/registry/etcd"
)
func main() {
service := micro.NewService(
micro.Registry(etcd.NewRegistry(registry.Addrs("127.0.0.1:2379"))),
)
service.Init()
req := pb.NewHiService("helloworld", service.Client())
reqParam := &pb.HelloReq{
Name: "mario",
}
res, err := req.MyHello(context.Background(), reqParam)
fmt.Print("res:", res, "err:", err)
}
项目启动:go run main.go
通过etcd查看服务注册的内容
命令行执行 etcdctl get / --prefix //查看以"/" 为前缀的服务注册内容,查到的内容如下:
/micro/registry/helloworld/helloworld--:
{
"name": "helloworld",
"version": "latest",
"metadata": null,
"endpoints": [
{
"name": "Hi.MyHello",
"request": {
"name": "HelloReq",
"type": "HelloReq",
"values": [
{
"name": "MessageState",
"type": "MessageState",
"values": [
{
"name": "NoUnkeyedLiterals",
"type": "NoUnkeyedLiterals",
"values": null
},
{
"name": "DoNotCompare",
"type": "DoNotCompare",
"values": null
},
{
"name": "DoNotCopy",
"type": "DoNotCopy",
"values": null
},
{
"name": "MessageInfo",
"type": "MessageInfo",
"values": null
}
]
},
{
"name": "int32",
"type": "int32",
"values": null
},
{
"name": "unknownFields",
"type": "[]uint8",
"values": null
},
{
"name": "Name",
"type": "string",
"values": null
}
]
},
"response": {
"name": "HelloRes",
"type": "HelloRes",
"values": [
{
"name": "MessageState",
"type": "MessageState",
"values": [
{
"name": "NoUnkeyedLiterals",
"type": "NoUnkeyedLiterals",
"values": null
},
{
"name": "DoNotCompare",
"type": "DoNotCompare",
"values": null
},
{
"name": "DoNotCopy",
"type": "DoNotCopy",
"values": null
},
{
"name": "MessageInfo",
"type": "MessageInfo",
"values": null
}
]
},
{
"name": "int32",
"type": "int32",
"values": null
},
{
"name": "unknownFields",
"type": "[]uint8",
"values": null
},
{
"name": "ResName",
"type": "string",
"values": null
}
]
},
"metadata": {}
}
],
"nodes": [
{
"id": "helloworld-295fcd5c-708e-4f31-8aba-d5325a799d85",
"address": "10.7.11.196:44065",
"metadata": {
"broker": "http",
"protocol": "grpc",
"registry": "etcd",
"server": "grpc",
"transport": "grpc"
}
}
]
}