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

go-zero微服务入门案例

一、go-zero微服务环境安装

  • 1、go-zero脚手架的安装

    go install github.com/zeromicro/go-zero/tools/goctl@latest
    
  • 2、etcd的安装下载地址根据自己电脑操作系统下载对应的版本,具体的使用自己查阅文章

二、创建一个user-rpc服务

  • 1、定义user.proto文件

    syntax = "proto3";package user;
    option go_package="./user";service User {rpc FindById(FindByIdReq) returns (FindByIdResp);
    }message FindByIdReq{int64 id = 1;
    }message FindByIdResp {int64 id = 1;string username = 2;
    }
    
  • 2、使用命令生成对应的项目文件

    goctl rpc protoc ./user.proto --go_out=. --go-grpc_out=. --zrpc_out=./
    
  • 3、安装对应的依赖包

    go mod tidy
    
  • 4、运行user服务

    go run user.go
  • 5、在etcd中查看服务是否已经注册成功

    etcdctl get --prefix user.rpc
    
  • 6、模拟业务代码返回数据

    func (l *FindByIdLogic) FindById(in *user.FindByIdReq) (*user.FindByIdResp, error) {return &user.FindByIdResp{Id:       in.Id,Username: "哈哈哈",}, nil
    }
    
  • 7、使用apifox可以直接调用rpc的服务,引入文件

    在这里插入图片描述

三、在提供restful api接口端调用rpc服务返回数据给前端

  • 1、创建一个user-api的项目

  • 2、创建描述文件

    syntax = "v1"type GetUserReq {Id int64 `path:"id"` // 主键id
    }type GetUserResp {Id int64 `json:"id"`              // 用户idUsername string `json:"username"` // 用户名
    }
    @server(prefix: api/v1/usergroup: user
    )
    service user-api {@doc "根据用户id获取用户新"@handler GetUserByIdApiget /:id (GetUserReq) returns (GetUserResp)
    }
    
  • 3、使用脚本生成对应的项目文件

    goctl api go -api *.api -dir . --style=gozero
    
  • 4、在user-api的配置文件中引入rpc服务的配置

    Name: user-api
    Host: 0.0.0.0
    Port: 8888UserRpc:Etcd:Hosts:- 127.0.0.1:2379Key: user.rpc
    
  • 5、在apps/user-api/internal/config/config.go创建服务的配置

    type Config struct {rest.RestConfUserRpc zrpc.RpcClientConf
    }
    
  • 6、在apps/user-api/internal/svc/servicecontext.go依赖注入rpc服务

    type ServiceContext struct {Config  config.ConfigUserRpc userclient.User
    }func NewServiceContext(c config.Config) *ServiceContext {return &ServiceContext{Config:  c,UserRpc: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),}
    }
    
  • 7、模拟实现业务代码

    func (l *GetUserByIdApiLogic) GetUserByIdApi(req *types.GetUserReq) (resp *types.GetUserResp, err error) {// 模拟业务开发findByIdResp, err := l.svcCtx.UserRpc.FindById(l.ctx, &user.FindByIdReq{Id: req.Id,})if err != nil {return &types.GetUserResp{}, errors.New("查询失败")}return &types.GetUserResp{Id:       findByIdResp.Id,Username: findByIdResp.Username,}, nil
    }
    
  • 8、直接浏览模拟请求http://localhost:8888/api/v1/user/1

http://www.lqws.cn/news/202915.html

相关文章:

  • vite+tailwind封装组件库
  • 如何配置 MySQL 允许远程连接
  • 《探秘局域网广播:网络世界的 “大喇叭”》
  • 64、js 中require和import有何区别?
  • Xilinx FPGA 重构Multiboot ICAPE2和ICAPE3使用
  • LeetCode 高频 SQL 50 题(基础版)之 【子查询】· 上
  • 【力扣链表篇】19.删除链表的倒数第N个节点
  • CRMEB 中 PHP 快递查询扩展实现:涵盖一号通、阿里云、腾讯云
  • A Survey on the Memory Mechanism of Large Language Model based Agents
  • LeetCode 08.06 面试题 汉诺塔 (Java)
  • uniapp 对接腾讯云IM群公告功能
  • 图上合成:用于大型语言模型持续预训练的知识合成数据生成
  • Linux中MySQL的逻辑备份与恢复
  • NamedParameterJdbcTemplate 使用方法及介绍
  • Readest(电子书阅读器) v0.9.53
  • Python爬虫-爬取各省份各年份高考分数线数据,进行数据分析
  • 使用 C/C++ 和 OpenCV 提取图像的感兴趣区域 (ROI)
  • Mysql批处理写入数据库
  • Opencv查找图形形状的重要API讲解
  • Cursor实现用excel数据填充word模版的方法
  • C#报价系统陈列展示成本核算系统项目管理系统纸品非纸品报价软件
  • linux 用户态时间性能优化工具perf/strace/gdb/varlind/gprof
  • 让DeepSeek写2025年高考作文
  • qiankun微前端 主应用vue3+vite、子应用vue3+vite
  • 使用 C++/OpenCV 创建动态流星雨特效 (实时动画)
  • ngx_stream_geo_module在传输层实现高性能 IP Region 路由
  • 学习STC51单片机30(芯片为STC89C52RCRC)
  • 【论文阅读笔记】《A survey on deep learning approaches for text-to-SQL》
  • 以SMMUv2为例,使用Trace32可视化操作SMMU的常用命令详解
  • 深入理解 Agent 与 LLM 的区别:从智能体到语言模型