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"
            }
        }
    ]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值