当前位置: 首页 > news >正文

go go-zero的学习,持续中...

官网地址

https://go-zero.dev/

命令行

自动创建rpc和api服务

  1. 构建rpc服务
goctl rpc new user

手动创建服务

命令要在user.proto所在的目录

goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=.

在这里插入图片描述

API创建

自动创建api

goctl api new api

手动创建api

goctl api go -api user.api -dir . -style gozero

etcd 安装

go-zero 默认配置是etcd

下载不同系统的版本

https://github.com/etcd-io/etcd/releases

解压后获得两个主要的程序 etcd.exe 和 etcdctl.exe

加入环境变量

D:\software\etcd-v3.6.4-windows-amd64\etcd-v3.6.4-windows-amd64

验证安装

etcd --version
etcd Version: 3.6.4
Git SHA: 5400cdc
Go Version: go1.23.11
Go OS/Arch: windows/amd64

运行etcd -单节点运行

.\etcd.exe ^
--name etcd01 ^
--data-dir .\data\etcd01 ^
--advertise-client-urls http://127.0.0.1:2379 ^
--listen-client-urls http://127.0.0.1:2379 ^
--listen-peer-urls http://127.0.0.1:2380 ^
--initial-advertise-peer-urls http://127.0.0.1:2380 ^
--initial-cluster etcd01=http://127.0.0.1:2380 ^
--initial-cluster-state newpause

验证是否成功

etcdctl --endpoints=http://127.0.0.1:2379 endpoint health# 期望结果http://127.0.0.1:2379 is healthy: successfully committed proposal: took = 3.4443ms

查看当前 etcd 集群中所有成员(节点)状态

etcdctl.exe member list
# 期望结果
b71f75320dc06a6c, started, etcd01, http://127.0.0.1:2380, http://127.0.0.1:2379, false

运行go-zero rpc

切换到 ·user/api·
记得加.

go run .

测试

  1. 使用apipost,新建grpc
    在这里插入图片描述

  2. 导入proto文件
    在这里插入图片描述

  3. 测试
    在这里插入图片描述

运行go-zero api

切换到 ·user/api·
记得加.

goctl api go -api user.api -dir . -style gozero
go run .

在这里插入图片描述

api 调用grpc服务

1、修改api-yml配置文件

user/api/etc/user.yaml

UserRpc:Etcd:Hosts:- 127.0.0.1:2379Key: user.rpc

2、修改配置文件

user/api/internal/config/config.go

package configimport ("github.com/zeromicro/go-zero/rest""github.com/zeromicro/go-zero/zrpc"
)type Config struct {rest.RestConfUserRpc zrpc.RpcClientConf # 新增
}

3、修改svc

user/api/internal/svc/servicecontext.go
新增userclient.User

package svcimport ("github.com/zeromicro/go-zero/zrpc""go-zero-user/api/internal/config""go-zero-user/rpc/userclient"
)type ServiceContext struct {Config config.Configuserclient.User
}func NewServiceContext(c config.Config) *ServiceContext {return &ServiceContext{Config: c,User:   userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),}
}

4、api服务逻辑层获取rpc服务数据

user/api/internal/logic/userlogic.go

package logicimport ("context""errors""go-zero-user/rpc/user""strconv""go-zero-user/api/internal/svc""go-zero-user/api/internal/types""github.com/zeromicro/go-zero/core/logx"
)type UserLogic struct {logx.Loggerctx    context.ContextsvcCtx *svc.ServiceContext
}func NewUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {return &UserLogic{Logger: logx.WithContext(ctx),ctx:    ctx,svcCtx: svcCtx,}
}func (l *UserLogic) User(req *types.UserReq) (resp *types.UserResp, err error) {if req.Id == 0 {return nil, errors.New("用户ID不能为空")}// todo: add your logic here and delete this linegetUsers, err := l.svcCtx.User.GetUser(l.ctx, &user.UserRequest{Id: strconv.FormatInt(req.Id, 10),})if err != nil {return nil, err}id, err := strconv.Atoi(getUsers.Id)resp = &types.UserResp{Id:    int64(id),Name:  getUsers.Name,Phone: getUsers.Phone,}return resp, nil
}

5、接口测试

在这里插入图片描述

中间件

http://www.dtcms.com/a/398077.html

相关文章:

  • C++篇 类和对象(3)万能工具怎么用?
  • 跨端边云时序数据管理新范式:Apache IoTDB 的 DB+AI 融合之道
  • 线程同步与互斥和生产消费模型
  • Java怎么终止一个线程
  • 软件项目管理中, UT测试如何体现
  • 神经网络工具箱
  • 软考系统架构设计师知识点-软件系统质量属性
  • 西安网站建设的软件哪个免费的网页制作软件最好
  • 【安装配置】【搭建本地Maven私服】
  • 一维卡尔曼滤波(无过程噪声)详解
  • AUTOSAR---汽车软件架构的标准化与未来展望
  • 压阻式应变传感器
  • Pydantic库应用
  • 【Linux手册】多线程编程的关键支撑:线程池与线程安全
  • 数字孪生:技术应用与实践案例
  • 阿里云上CentOS6.9(停止维护)导致的yum下载chrony失败如何解决?
  • ubuntu中mysql初始化报错
  • 上海网站推广排名百度图片搜索图片识别
  • 安庆有做网站的吗已矣seo排名点击软件
  • 优雅的 async/await 错误处理模式指南
  • 八、神经网络(下)
  • 鲜花购物商城(WebSocket及时通讯、协同过滤算法、支付宝沙盒支付、Echarts图形化分析、快递物流API)
  • 精准调度,确定性保障:信而泰IEEE 802.1Qbv协议测试实战指南
  • 硬件开发_基于STM32单片机的智能路灯系统
  • 学校校园网站建设服务程序员用的编程软件
  • 网站图标ico企业网关路由器怎么设置
  • 怎么安装并使用数字人EchoMimicV2
  • 电气仿真模型资料合集,微电网优化,综合能源调度,配电网无功优化,风光出力预测,电动汽车
  • FunASR:面向产业落地的端到端语音识别系统
  • FFmpeg 深入精讲(四)SDL音视频渲染实践