Go-Micro注册到etcd

xiaohai 2021-05-09 22:33:54 2586人围观 标签: Go 
简介Go-Micro注册到etcd

1、新版go-micro命令行已经废弃了consul,支持了etcd

2、安装etcd
  参考网址:https://www.cnblogs.com/chenqionghe/p/10503840.html

3、注册更换成etcd

package main

import (
    "github.com/micro/go-micro"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-micro/registry/etcd"
    "micro-api/service/controller"
    services "micro-api/service/model"
)

func main() {
    registerEtcd := etcd.NewRegistry(func(options *registry.Options) {
        options.Addrs = []string{
            "192.168.1.171:2379",
        }
    })

    service := micro.NewService(
        micro.Name("student-service"),
        micro.Registry(registerEtcd),
    )
    if err := services.RegisterStudentListServiceHandler(service.Server(), new(controller.StudentService)); err != nil {

    }
    service.Init()

    service.Run()
}

4、使用micro工具包查看服务列表

#1、查看服务
> micro --registry=etcd --registry_address=192.168.1.171:2379 list services
go.micro.http.broker
student-service

#2、获取服务
> micro --registry=etcd --registry_address=192.168.1.171:2379 list services
go.micro.http.broker
student-service

E:\go\micro-student>micro --registry=etcd --registry_address=192.168.1.171:2379 get service student-service
service  student-service

version 2020.03.14.14.01

ID      Address Metadata
student-service-5b943dd6-3c6a-420f-b171-a2dbfc5ca88f    192.168.1.61:57953      transport=http,broker=http,protocol=mucp,registry=etcd,server=mucp

Endpoint: StudentListService.GetStudentListService

Request: {
        num int32
}

Response: {
        status int32
        data []StudentModel
}

#3、调用服务
> micro --registry=etcd --registry_address=192.168.1.171:2379 call student-service StudentListService.GetStudentListService "{\"num\":3}"
{
        "status": 200,
        "data": [
                {
                        "id": 1,
                        "name": "name_1",
                        "sex": "男"
                },
                {
                        "id": 2,
                        "name": "name_2",
                        "sex": "男"
                },
                {
                        "id": 3,
                        "name": "name_3",
                        "sex": "男"
                }
        ]
}

5、使用micro工具的Dashboard

micro --registry=etcd --registry_address=192.168.1.171:2379 web

通过这个管理界面可以查看服务的详细信息,可以测试服务,使用比较方便