go-zero安装

dandan3年前程序开发2705

go版本1.20.2


go环境变量

export GOROOT=/Users/yelin/go_dev/go
export GOPATH=/Users/yelin/go_dev/project
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

下载goctl

GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest


安装protoc & protoc-gen-go

goctl env check -i -f --verbose


创建测试项目hellow,进入执行

go mod init hellow


创建内容

goctl api new hellow1


下载依赖

go mod tidy


在hellow1/logic/hellow1logic.go中写逻辑

func (l *Hellow1Logic) Hellow1(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
resp = &types.Response{
Message: "hello go",
}
return
}


运行

go run hellow1/hellow1.go -f hellow1/etc/hellow1-api.yaml



微服务版本

建立目录,如hellow_rpc,进入后

创建order服务

goctl api new order

创建user服务

goctl api new user

建立工作区

go work init
go work use order
go work use user
//难道实际测试好像没有自动生成,手动改写go.work
go 1.20
use (
./order
./user
)


创建user的rpc文件,/user/rpc/user.proto

syntax = "proto3";
package user;
  
// protoc-gen-go 版本大于1.4.0, proto文件需要加上go_package,否则无法生成
option go_package = "./user";
message IdRequest {
    string id = 1;
}
  
message UserResponse {
    // 用户id
    string id = 1;
    // 用户名称
    string name = 2;
    // 用户性别
    string gender = 3;
}
  
service User {
    rpc getUser(IdRequest) returns(UserResponse);
}

执行创建

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


跑完后,可以把里面除了user.protoc外的内容都放到上一层,然后上一层的user.api可以删除



相关文章

Docker安装篇(CentOS7.6)

7.6环境自带gcc和gcc-c++,不用再装安装yum-utilsyum install -y yum-utils设置阿里云镜像仓库yum-config-manager...

camunda使用

1、 拉取镜像docker pull camunda/camunda-bpm-platform:7.17.02、配置并启动docker run -d ...

k8s学习

k3s学习环境安装,准备3台虚拟机,centos7.6以上操作每台机子都要执行,操作完成后,修改主机名hostnamectl set-hostname k8s-master...

mac m1 中go使用kafka

当前docker-comose:version: '3' services:   Etcd:     con...

MQTT协议使用

服务端因为使用的发布/订阅模式,似乎不需要自己写服务端,只要安装现成开源服务器即可,这里选了EMQX#到官网,选择服务器版本,下载安装,已centos7为例: #下载源码包 wget h...

opemim安装部署

前置准备:(1)安装golang环境(官网下linux包到服务器解压,设置环境变量到bin目录即可),并设置go代理:go env -w GOPROXY=https://proxy.golang.co...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。