【go-zero】api与rpc使用etcd服务发现

准备代码

etcd先安装启动

目录结构

go mod init rpc/demo/v2

编写rpc

在user-rpc目录下

user.proto 文件

可使用goctl快速生成 goctl rpc -o user.proto

bash 复制代码
syntax = "proto3";

option go_package="./pb";
package pb;

message GetUserInfoReq {
  int64 id = 1;
}

message GetUserInfoResp {
  int64 id = 1;
  string nickname = 2;
}

service StreamGreeter {
  rpc getUserInfo(GetUserInfoReq) returns (GetUserInfoResp);
}

生成rpc项目

bash 复制代码
goctl rpc protoc .\user.proto --go_out=. --go-grpc_out=. --zrpc_out=.

编写etc目录下的user.yaml文件,一般会把127.0.0.1修改为0.0.0.0

yaml 复制代码
Name: user.rpc
ListenOn: 0.0.0.0:8980
Etcd:
  Hosts:
  - 0.0.0.0:2379
  Key: user.rpc

写下业务代码 user-rpc/internal/logic/getuserinfologic.go

go 复制代码
func (l *GetUserInfoLogic) GetUserInfo(in *pb.GetUserInfoReq) (*pb.GetUserInfoResp, error) {
	// todo: add your logic here and delete this line

	return &pb.GetUserInfoResp{
		Id:       in.Id,
		Nickname: "zero-rpc",
	}, nil
}

etcd开启 就可以启动rpc服务了

编写api

在user-api目录下

user-api.api文件

go 复制代码
syntax = "v1"

info (
	title:  "dada"
	desc:   "使用api调用rpc"
	author: "akadod"
	email:  "[email protected]"
)

type request {
	UserId int64 `path:"userId"`
}

type response {
	Id   int64  `json:"id"`
	Name string `json:"name"`
}

service user-api {
	@handler GetUser // TODO: set handler name and delete this comment
	get /users/id/:userId (request) returns (response)

	@handler CreateUser // TODO: set handler name and delete this comment
	post /users/create (request)
}

生成api代码

bash 复制代码
goctl api go --api .\user-api.api --dir . --style goZero

添加etcd配置 编辑yaml文件

yaml 复制代码
Name: user-api
Host: 0.0.0.0
Port: 8988
UserRpcConf:
  Etcd:
    Hosts:
      - 0.0.0.0:2379
    Key: user.rpc

config.go文件添加rpc配置

go 复制代码
package config

import (
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
)

type Config struct {
	rest.RestConf
	UserRpcConf zrpc.RpcClientConf
}

编辑 user-api/internal/svc/serviceContext.go 文件

go 复制代码
package svc

import (
	"github.com/zeromicro/go-zero/zrpc"
	"rpc/demo/v2/user-api/internal/config"
	"rpc/demo/v2/user-rpc/streamgreeter"
)

type ServiceContext struct {
	Config config.Config
	SsRpc  streamgreeter.StreamGreeter
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config: c,
		SsRpc:  streamgreeter.NewStreamGreeter(zrpc.MustNewClient(c.UserRpcConf)),
	}
}

编写getUserLogic.go 业务文件

go 复制代码
package logic

import (
	"context"
	"rpc/demo/v2/user-rpc/streamgreeter"

	"rpc/demo/v2/user-api/internal/svc"
	"rpc/demo/v2/user-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserLogic) GetUser(req *types.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userResp, err := l.svcCtx.SsRpc.GetUserInfo(l.ctx, &streamgreeter.GetUserInfoReq{Id: req.UserId})
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Id:   userResp.Id,
		Name: userResp.Nickname,
	}, nil
}

启动api服务

测试

通过直连

rpc把Etcd配置删掉

user.yaml

yaml 复制代码
Name: user.rpc
ListenOn: 0.0.0.0:8980
Mode: dev

api修改配置文件

user-api.api

yaml 复制代码
Name: user-api
Host: 0.0.0.0
Port: 8988
UserRpcConf:
  Endpoints:
    - 127.0.0.1:8980
相关推荐
roman_日积跬步-终至千里13 小时前
【Go语言基础【3】】变量、常量、值类型与引用类型
开发语言·算法·golang
roman_日积跬步-终至千里14 小时前
【Go语言基础】基本语法
开发语言·golang·xcode
月忆3641 天前
go语言的锁
golang
fashia1 天前
Java转Go日记(六十):gin其他常用知识
开发语言·后端·golang·go·gin
一只特立独行的兔先森1 天前
WordZero:让Markdown与Word文档自由转换的Golang利器
golang·word·word自动化
我的golang之路果然有问题2 天前
ElasticSearch+Gin+Gorm简单示例
大数据·开发语言·后端·elasticsearch·搜索引擎·golang·gin
K____End2 天前
Spring 中的disposableBean介绍
java·spring·rpc
高hongyuan2 天前
Linux RPC 和 NFS 教程
linux·运维·rpc·nfs
钟离墨笺2 天前
Go语言学习-->第一个go程序--hello world!
开发语言·学习·golang
march of Time2 天前
go的工具库:github.com/expr-lang/expr
开发语言·golang·github