proto 导入外部包

88 阅读1分钟

项目结构,idl 放在项目内部的这种

/apps/www/go/src/newstart
├── idl
│   ├── common
│   │   └── base.proto
│   ├── order
│   │   └── v1
│   │       └── order.proto
│   └── product
│       └── v1
│           └── product.proto
└── internal
  

base.proto 定义

package base;

// 确保 go_package 路径能正确映射到生成的 Go 代码
option go_package = "newstart/idl/common;v1";

message BaseModel {
  int32 baseCode = 1; 
  string baseMsg = 2;
}

order.proto 定义


package order;
option go_package="idl/order;v1";

import "common/base.proto";

message Request {
  string ping = 1;
}

message Response {
  string pong = 1;
  base.BaseModel base = 2; // 引入公共基础模型
}

service Order {
  rpc Ping(Request) returns(Response);
}

protoc 命令

VERSION=$(shell git describe --tags --always)
GOHOSTOS:=$(shell go env GOHOSTOS)

ifeq ($(GOHOSTOS), windows)
	Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
	INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto 2>/dev/null || true")
	API_PROTO_FILES=$(shell $(Git_Bash) -c "find idl -name *.proto 2>/dev/null || true")
else
	INTERNAL_PROTO_FILES=$(shell find internal -name *.proto 2>/dev/null || true)
	API_PROTO_FILES=$(shell find idl -name *.proto 2>/dev/null || true)
endif

.PHONY: kratos_api
kratos_api:
	protoc --proto_path=./idl \
	       --proto_path=./third_party \
 	       --go_out=Morder/v1/order.proto=./idl/order/v1,paths=source_relative:./idl \
 	       --go-http_out=Morder/v1/order.proto=./idl/order/v1,paths=source_relative:./idl \
 	       --go-grpc_out=Morder/v1/order.proto=./idl/order/v1,paths=source_relative:./idl \
	       --openapi_out=fq_schema_naming=true,default_response=false:. \
	       $(API_PROTO_FILES)

如果外部多项目共用common 需要在 common/base.proto 里设置全局唯一的 go_package

如果是基于buf 管理

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest

buf.gen.yaml

managed:
  enabled: true
  override:
    - file_option: go_package
      value: newstart/rpc/order/v1;v1
    - file_option: go_package
      value: newstart/rpc/common;v1
  disable:
    - file_option: go_package
      module: buf.build/bufbuild/protovalidate
    - file_option: go_package
      module: buf.build/googleapis/googleapis
plugins:
  - local: protoc-gen-go
    out: rpc
    opt:
      - paths=source_relative
      - Morder/v1/order.proto=./idl/order/v1
  - local: protoc-gen-go-grpc
    out: rpc
    opt:
      - paths=source_relative
      - Morder/v1/order.proto=./idl/order/v1
inputs:
  - directory: ./idl
  - directory: ./third_party

buf generate

go-zero的goctl是不支持外部proto导入的,如何即想用goctl又想使用buf导入 在当前项目gen目录创建项目proto,比如项目就叫newstart,那就是newstart.proto


package newstart;
option go_package="./newstart/v1";

message Request {
  string ping = 1;
}

message Response {
  string pong = 1;
}

service NewStart {
  rpc Ping(Request) returns(Response);
}

然后

# generate rpc code
rpc_test:
	goctl rpc protoc newstart.proto  --go_out=./rpc --go-grpc_out=./rpc --zrpc_out=.
	@rm -rf rpc


.PHONY: all
# generate all
all:
    make rpc_test
    buf generate