准备代码
etcd先安装启动
目录结构
go mod init rpc/demo/v2
编写rpc
在user-rpc目录下
user.proto 文件
可使用goctl快速生成 goctl rpc -o user.proto
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项目
goctl rpc protoc .\user.proto --go_out=. --go-grpc_out=. --zrpc_out=.
编写etc目录下的user.yaml文件,一般会把127.0.0.1修改为0.0.0.0
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
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文件
syntax = "v1"
info (
title: "dada"
desc: "使用api调用rpc"
author: "akadod"
email: "aka.dod@yandex.com"
)
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代码
goctl api go --api .\user-api.api --dir . --style goZero
添加etcd配置 编辑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配置
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 文件
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 业务文件
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
Name: user.rpc
ListenOn: 0.0.0.0:8980
Mode: dev
api修改配置文件
user-api.api
Name: user-api
Host: 0.0.0.0
Port: 8988
UserRpcConf:
Endpoints:
- 127.0.0.1:8980