ETCD服务注册与发现使用

1 配置

1.1 Registry配置

  1. type Config struct {
  2. Scheme string // 协议
  3. Prefix string // 注册前缀
  4. ReadTimeout time.Duration // 读超时
  5. ServiceTTL time.Duration // 服务续期
  6. OnFailHandle string // 错误后处理手段,panic,error
  7. }

1.2 ETCD配置

  1. type config struct {
  2. Addrs []string // 地址
  3. CertFile string // cert file
  4. KeyFile string // key file
  5. CaCert string // ca cert
  6. UserName string // 用户名
  7. Password string // 密码
  8. ConnectTimeout time.Duration // 连接超时时间
  9. AutoSyncInterval time.Duration // 自动同步member list的间隔
  10. EnableBasicAuth bool // 是否开启认证
  11. EnableSecure bool // 是否开启安全
  12. EnableBlock bool // 是否开启阻塞,默认开启
  13. EnableFailOnNonTempDialError bool // 是否开启gRPC连接的错误信息
  14. }

2 服务注册

2.1 服务注册配置

  1. [server.grpc]
  2. port = 9003
  3. [etcd]
  4. addrs=["127.0.0.1:2379"]
  5. connectTimeout = "1s"
  6. secure = false
  7. [registry]
  8. scheme = "etcd" # grpc resolver默认scheme为"etcd",你可以自行修改
  9. #serviceTTL = "10s"

2.2 服务注册代码

  1. package main
  2. import (
  3. "context"
  4. "github.com/gotomicro/ego"
  5. "github.com/gotomicro/ego-component/eetcd"
  6. "github.com/gotomicro/ego-component/eetcd/examples/helloworld"
  7. "github.com/gotomicro/ego-component/eetcd/registry"
  8. "github.com/gotomicro/ego/core/elog"
  9. "github.com/gotomicro/ego/server"
  10. "github.com/gotomicro/ego/server/egrpc"
  11. "google.golang.org/grpc/codes"
  12. "google.golang.org/grpc/status"
  13. )
  14. // export EGO_DEBUG=true && go run main.go --config=config.toml
  15. func main() {
  16. if err := ego.New().
  17. Registry(registry.Load("registry").Build(registry.WithClientEtcd(eetcd.Load("etcd").Build()))).
  18. Serve(func() server.Server {
  19. server := egrpc.Load("server.grpc").Build()
  20. helloworld.RegisterGreeterServer(server.Server, &Greeter{server: server})
  21. return server
  22. }()).Run(); err != nil {
  23. elog.Panic("startup", elog.Any("err", err))
  24. }
  25. }
  26. type Greeter struct {
  27. server *egrpc.Component
  28. }
  29. func (g Greeter) SayHello(context context.Context, request *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
  30. if request.Name == "error" {
  31. return nil, status.Error(codes.Unavailable, "error")
  32. }
  33. return &helloworld.HelloReply{
  34. Message: "Hello EGO, I'm " + g.server.Address(),
  35. }, nil
  36. }

3 服务发现

3.1 服务发现

  1. [grpc.test]
  2. debug = true # 开启后并加上export EGO_DEBUG=true,可以看到每次grpc请求,配置名、地址、耗时、请求数据、响应数据
  3. addr = "etcd:///main"
  4. #balancerName = "round_robin" # 默认值
  5. #dialTimeout = "1s" # 默认值
  6. #enableAccessInterceptor = true
  7. #enableAccessInterceptorRes = true
  8. #enableAccessInterceptorReq = true
  9. [etcd]
  10. addrs=["127.0.0.1:2379"]
  11. connectTimeout = "1s"
  12. secure = false
  13. [registry]
  14. scheme = "etcd" # grpc resolver默认scheme为"etcd",你可以自行修改

3.2 服务注册代码

  1. package main
  2. import (
  3. "context"
  4. "github.com/gotomicro/ego"
  5. "github.com/gotomicro/ego/client/egrpc"
  6. "github.com/gotomicro/ego/core/elog"
  7. "github.com/gotomicro/ego-component/eetcd"
  8. "github.com/gotomicro/ego-component/eetcd/examples/helloworld"
  9. "github.com/gotomicro/ego-component/eetcd/registry"
  10. )
  11. func main() {
  12. if err := ego.New().Invoker(
  13. invokerGrpc,
  14. callGrpc,
  15. ).Run(); err != nil {
  16. elog.Error("startup", elog.FieldErr(err))
  17. }
  18. }
  19. var grpcComp helloworld.GreeterClient
  20. func invokerGrpc() error {
  21. // 注册resolver
  22. registry.Load("registry").Build(registry.WithClientEtcd(eetcd.Load("etcd").Build()))
  23. grpcConn := egrpc.Load("grpc.test").Build()
  24. grpcComp = helloworld.NewGreeterClient(grpcConn.ClientConn)
  25. return nil
  26. }
  27. func callGrpc() error {
  28. _, err := grpcComp.SayHello(context.Background(), &helloworld.HelloRequest{
  29. Name: "i am client",
  30. })
  31. if err != nil {
  32. return err
  33. }
  34. _, err = grpcComp.SayHello(context.Background(), &helloworld.HelloRequest{
  35. Name: "error",
  36. })
  37. if err != nil {
  38. return err
  39. }
  40. return nil
  41. }