go-micro使用etcd作为服务注册中心

需要安装etcd服务,我用的是windows版本的,具体安装自行解决

  • proto文件编辑
syntax = "proto3";

message GetRequest {
    int32 user_id = 1;
}

message PutResponse {
    string name = 1;
    int32 age = 2;
    int32 score = 3;
}

service UserServer {
    rpc UserInfo(GetRequest) returns(PutResponse){}
}

编译生成go需要的文件
protoc --micro_out=. --go_out=. user.proto

  • 服务端代码
package main

import (
	"context"
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/registry"
	etcd2 "github.com/micro/go-micro/registry/etcd"
	"github.com/micro/go-micro/transport/grpc"
	pb "micro.zozoo.net/proto"
)

//定义空结构体,实现接口方法
type userServer struct {
}

func (c *userServer)UserInfo(ctx context.Context,res *pb.GetRequest,resp *pb.PutResponse) error  {
	resp.Name = "gangan"
	resp.Age = res.UserId
	resp.Score = 80
	return nil
}

func main() {
	etcd := etcd2.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{"127.0.0.1:2379"}//地址
		//etcd2.Auth("gangan","123")//密码
	})
	//创建一个新的服务
	server := micro.NewService(
		micro.Name("Hello"),
		micro.Registry(etcd),
		micro.Transport(grpc.NewTransport()),//修改传输协议
		)

	//服务初始化
	server.Init()

	//注册方法
	err := pb.RegisterUserServerHandler(server.Server(),new(userServer))
	if err != nil {
		panic(err.Error())
	}

	//启动
	if err = server.Run();err != nil {
		panic(err.Error())
	}
}
  • 客户端代码
package main

import (
	"context"
	"fmt"
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/registry"
	etcd2 "github.com/micro/go-micro/registry/etcd"
	"github.com/micro/go-micro/transport/grpc"
	pb "micro.zozoo.net/proto"
)

func main() {
	etcd := etcd2.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{"127.0.0.1:2379"}//地址
		//etcd2.Auth("gangan","123")//密码

	})
	//创建服务
	server := micro.NewService(
		micro.Name("Hello.client"),
		micro.Registry(etcd),
		micro.Transport(grpc.NewTransport()),
		)

	//初始化
	server.Init()

	//注册方法
	userInfo := pb.NewUserServerService("Hello",server.Client())

	//调用方法
	resp,err := userInfo.UserInfo(context.TODO(),&pb.GetRequest{UserId:18})
	if err != nil {
		panic(err.Error())
	}

	fmt.Println(resp.Name)
	fmt.Println(resp.Score)
	fmt.Println(resp.Age)
}

  • mod文件
module micro.zozoo.net

go 1.13

require (
	github.com/coreos/etcd v3.3.25+incompatible // indirect
	github.com/go-log/log v0.2.0 // indirect
	github.com/gogo/protobuf v1.3.1 // indirect
	github.com/golang/protobuf v1.4.2
	github.com/google/btree v1.0.0 // indirect
	github.com/google/uuid v1.1.2 // indirect
	github.com/grpc-ecosystem/grpc-gateway v1.9.2 // indirect
	github.com/json-iterator/go v1.1.10 // indirect
	github.com/kr/pretty v0.2.0 // indirect
	github.com/lucas-clemente/quic-go v0.18.0 // indirect
	github.com/marten-seemann/qtls-go1-15 v0.1.1 // indirect
	github.com/micro/go-micro v1.18.0
	github.com/miekg/dns v1.1.31 // indirect
	github.com/nats-io/jwt v1.0.1 // indirect
	github.com/nats-io/nats.go v1.10.0 // indirect
	github.com/nats-io/nkeys v0.2.0 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	github.com/prometheus/client_golang v1.4.0 // indirect
	go.uber.org/zap v1.16.0 // indirect
	golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
	golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
	golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
	golang.org/x/sys v0.0.0-20200917073148-efd3b9a0ff20 // indirect
	golang.org/x/text v0.3.3 // indirect
	golang.org/x/tools v0.0.0-20200913032122-97363e29fc9b // indirect
	google.golang.org/genproto v0.0.0-20200911024640-645f7a48b24f // indirect
	google.golang.org/grpc v1.32.0 // indirect
	google.golang.org/protobuf v1.25.0
	honnef.co/go/tools v0.0.1-2020.1.5 // indirect
)

replace (
	github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.14.1
	google.golang.org/grpc => google.golang.org/grpc v1.26.0
)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值