前言
之前用的consul进行服务发现,现在转为使用etcd
开始
1 安装etcd
docker 启动etcd (单机测试版)
docker pull quay.io/coreos/etcd
编辑docker-compose.yml
version: "3"
services:
etcd:
image: quay.io/coreos/etcd:latest
ports:
- "2379:2379"
- "4001:4001"
- "2380:2380"
environment:
- ETCDCTL_API=3
volumes:
- "/data/etcd/dir:/data"
command:
/usr/local/bin/etcd
-data-dir /data
-name node1
-advertise-client-urls http://0.0.0.0:2379
-listen-client-urls http://0.0.0.0:2379
-initial-cluster etcd0=http://0.0.0.0:2380
-listen-peer-urls http://0.0.0.0:2380
-initial-cluster-state new
启动
docker-compose up -d
测试 成功
curl -L http://127.0.0.1:2379/v2/members
curl -L http://127.0.0.1:2379/v2/keys/foo -XPUT -d value="Hello foo"
{"members":[{"id":"8e9e05c52164694d","name":"node1","peerURLs":["http://localhost:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}
curl -L http://127.0.0.1:2379/v2/keys/foo
{"action":"get","node":{"key":"/foo","value":"Hello foo","modifiedIndex":5,"createdIndex":5}}
2 go-micro 示例
hi.proto文件
syntax = "proto3";
package hello;
service Say {
rpc Hello(SayRequest) returns (SayResponse) {}
}
message SayRequest {
string from = 1;
string to = 2;
string msg = 3;
}
message SayResponse {
string from = 1;
string to = 2;
string msg = 3;
}
编译
➜ tree protoc --micro_out=. --go_out=. hi.proto
产生go文件
➜ tree
.
├── hi.micro.go
├── hi.pb.go
└── hi.proto
0 directories, 3 files
server.go 服务端示例代码
package main
import (
"context"
"fmt"
"github.com/cowkeys/test/etcd/proto/hello"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/etcdv3"
)
type Say struct{}
func (s *Say) Hello(ctx context.Context, req *hello.SayRequest, rsp *hello.SayResponse) error {
fmt.Printf("received req %#v \n", req)
rsp.From = "server"
rsp.To = "client"
rsp.Msg = "ok"
return nil
}
func main() {
reg := etcdv3.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{"http://127.0.0.1:2379"}
})
service := micro.NewService(
micro.Name("cowkeys.srv.say"),
micro.Registry(reg),
)
service.Init()
hello.RegisterSayHandler(service.Server(), new(Say))
if err := service.Run(); err != nil {
panic(err)
}
}
运行
go run main.go
报很多错
cannot use auth.callOpts (type []"github.com/coreos/etcd/vendor/google.golang.org/grpc".CallOption) as type []"go.etcd.io/etcd/vendor/google.golang.org/grpc".CallOption in argument to auth.remote.AuthEnable
感觉是micro与etcd库路径或者版本不一致, 直接用govendor来处理包,然后
重新运行,运行成功:
➜ server go run server.go
2019-10-16 15:26:22.869873 I | Transport [http] Listening on [::]:34129
2019-10-16 15:26:22.869916 I | Broker [http] Connected to [::]:40577
2019-10-16 15:26:22.870161 I | Registry [etcdv3] Registering node: cowkeys.srv.say-bc3deb4a-ae65-4e1c-8af8-c95fd02b376a
client 客户端调用示例代码:
package main
import (
"context"
"fmt"
"github.com/cowkeys/test/etcd/proto/hello"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/etcdv3"
)
func main() {
reg := etcdv3.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
"http://127.0.0.1:2379",
}
})
service := micro.NewService(
micro.Registry(reg),
)
service.Init()
sayClent := hello.NewSayService("cowkeys.srv.say", service.Client())
param := &hello.SayRequest{
From: "client",
To: "server",
Msg: "hello xxx",
}
rsp, err := sayClent.Hello(context.Background(), param)
if err != nil {
panic(err)
}
fmt.Println(rsp)
}
运行
➜ go run client.go
from:"server" to:"client" msg:"ok"
// server 会打印出来调用信息
received req &hello.SayRequest{From:"client", To:"server", Msg:"hello xxx", XXX_NoUnkeyedLiteral:struct {}{}, XXX_unrecognized:[]uint8(nil), XXX_sizecache:0}
到此执行成功
其他etcd命令