GO 微服务GO-Micro(10)-纯个人学习笔记记录:切换Docker方案启动etcd服务中心

etcd简介与应用场景

etcd 是一个分布式一致性k-v存储系统,通常我们可用于服务注册发现与共享配置,它主要有以下特点:

  1. 简单: 基于HTTP+JSON的API让你可以用CURL命令就可以轻松使用。
  2. 安全: 可以选择SSL客户认证机制。
  3. 快速: 每个实例每秒支持一千次写操作。
  4. 可信: 使用 Ralf 算法充分实现了分布式。

说明

之前我们的服务中心是再我们注册到默认的MDNS,还有consul,但是发现我们在使用consul的时候,查询相关的服务的时候,总是查询不到服务的列表。
如我们的启动我们的服务之后:

D:\code\go\Mi_Onse\greeter>go run main_consul.go
2021-01-26 14:45:42  [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-26 14:45:42  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:58790
2021-01-26 14:45:42  file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:58791
2021-01-26 14:45:42  file=grpc/grpc.go:697 level=info Registry [consul] Registering node: go.micro.service.greeter-893e627e-9e1a-4974-bc6b-7dd6a4d35853
2021-01-26 14:45:42  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter

查询当前注册中心的服务列表

D:\code\go\Mi_Onse>micro --registry etcd list services
{"level":"warn","ts":"2021-01-26T14:55:27.439+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-a76adcde-e089-422b-bf12-6452ed5130c2/127.0.0.1:2379","attempt":0,"e
rror":"rpc error: code = DeadlineExceeded desc = latest balancer error: all SubConns are in TransientFailure, latest connection error: connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:2379: connectex: No
connection could be made because the target machine actively refused it.\""}
context deadline exceeded

D:\code\go\Mi_Onse>micro --registry consul list services
Registry consul not found

D:\code\go\Mi_Onse>micro --registry mdns list services

D:\code\go\Mi_Onse>

使用Docker方案启动etcd

因为之前也有学习过如何使用Docker,为方便快捷,我们这也直接的使用Docker快速启动我们的etcd来搭建我们的环境。

启动我们之前新建的安装好Docker的虚拟机环境,然后启动etcd单一节点:

docker run -d \
  -p 2379:2379 \
  -p 2380:2380 \
  --name etcd2 \
  quay.io/coreos/etcd:v3.3.8 \
  /usr/local/bin/etcd \
  --name s1 \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://0.0.0.0:2379
image.png

耐心等待启动后,查看一下当前我们的docker
查看我们的启动的服务:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                              NAMES
54c16f197329        quay.io/coreos/etcd:v3.3.8   "/usr/local/bin/etcd…"   4 minutes ago       Up 4 minutes        0.0.0.0:2379-2380->2379-2380/tcp   etcd2
[root@localhost ~]#

1.使用命令的形式把我们的服务注册到我们的ECTD上:

D:\code\go\Mi_Onse\greeter>go run main.go --registry=etcd --registry_address=192.168.219.130:2379

因为我们的是使用Docker的形式启动我们的环境,所以此时我们的需要注意使用registry_address来指定注册的地址
,不然默认是会去查询本地的121.0.0.1
操作结果:

D:\code\go\Mi_Onse\greeter>go run main.go --registry=etcd --registry_address=192.168.219.130:2379
2021-01-26 15:35:30  [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-26 15:35:30  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:63499
2021-01-26 15:35:30  file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:63500
2021-01-26 15:35:30  file=grpc/grpc.go:697 level=info Registry [etcd] Registering node: go.micro.service.greeter-753c8941-78a1-4452-bb7e-2439a3ef0657
2021-01-26 15:35:30  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter

查看我们的注册服务的结果列表:

D:\code\go\Mi_Onse>micro --registry etcd --registry_address 192.168.219.130:2379 list services
go.micro.service.greeter
micro.http.broker

错误的示例

D:\code\go\Mi_Onse>micro --registry etcd list services
{"level":"warn","ts":"2021-01-26T15:38:55.880+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-3cfc1566-5f6e-40e7-a040-16459b1f4ca1/127.0.0.1:2379","attempt":0,"e
rror":"rpc error: code = DeadlineExceeded desc = latest balancer error: all SubConns are in TransientFailure, latest connection error: connection error: desc = \"transport: Error while dialing dial tcp 127.0.0.1:2379: connectex: No
connection could be made because the target machine actively refused it.\""}
context deadline exceeded

2.使用代码的形式把我们的服务注册到我们的ECTD上:

修改注册中心代码:

使用的是:"github.com/micro/go-micro/v2/registry/etcd"

package main

import (

    "github.com/micro/go-micro/v2"
    log "github.com/micro/go-micro/v2/logger"
    "github.com/micro/go-micro/v2/registry"
    "github.com/micro/go-micro/v2/registry/etcd"

    "greeter/handler"
    "greeter/subscriber"
    _ "github.com/micro/go-plugins/registry/consul/v2"
    greeter "greeter/proto/greeter"
)

func main() {

    reg := etcd.NewRegistry(registry.Addrs("192.168.219.130:2379"))


    // New Service
    service := micro.NewService(
        micro.Name("go.micro.service.greeter"),
        micro.Version("latest"),
        micro.Registry(reg),
    )

    // Initialise service
    service.Init()

    // Register Handler
    _ = greeter.RegisterGreeterHandler(service.Server(), new(handler.Greeter))

    // Register Struct as Subscriber
    _ =micro.RegisterSubscriber("go.micro.service.greeter", service.Server(), new(subscriber.Greeter))

    // Run service
    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

然后运行起来我们的服务:

D:\code\go\Mi_Onse\greeter>go run main_ectd.go
2021-01-26 15:54:07  [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-26 15:54:07  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:65403
2021-01-26 15:54:07  file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:65404
2021-01-26 15:54:07  file=grpc/grpc.go:697 level=info Registry [etcd] Registering node: go.micro.service.greeter-218b0dd0-811c-43e2-8402-b73430bb66b4
2021-01-26 15:54:07  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter

再查看我们的服务,一样的可以看得到我们注册成功的服务列表信息:

D:\code\go\Mi_Onse>micro --registry etcd --registry_address 192.168.219.130:2379 list services
go.micro.service.greeter
micro.http.broker

D:\code\go\Mi_Onse>

你可能感兴趣的:(GO 微服务GO-Micro(10)-纯个人学习笔记记录:切换Docker方案启动etcd服务中心)