使用Kitex Protobuf示例遇导入错误,求解决方案
解决Kitex Protobuf示例中"could not import echoTest/kitex_gen/echo"错误
问题原因
Kitex处理Protobuf与Thrift的代码生成逻辑存在差异,你遇到的情况是因为Protobuf模式下代码生成规则、文件命名和Thrift不同,加上proto文件的go_package配置未与模块路径匹配,导致目标目录缺失必要的代码文件。
修复步骤
调整proto文件的go_package配置
将proto文件中的go_package修改为包含模块名的完整路径,确保Kitex能正确定位生成代码的位置:syntax = "proto3"; option go_package = "echoTest/kitex_gen/echo"; package echo; message Request { string msg = 1; } message Response { string msg = 1; } service EchoService { rpc ClientSideStreaming(stream Request) returns (Response){} rpc ServerSideStreaming(Request) returns (stream Response){} rpc BidiSideStreaming(stream Request) returns (stream Response){} }初始化Go模块并重新生成代码
在Protobuf-test目录下先初始化Go模块(若未执行过):go mod init echoTest然后重新执行Kitex生成命令:
kitex -type protobuf -module echoTest -service echoTest echo.proto验证生成结果
执行完成后,检查kitex_gen/echo目录,会生成echo.pb.go(Protobuf基础定义代码)和echo_kitex.go(Kitex服务框架代码)等文件。Protobuf模式下Kitex不会生成Thrift模式中的echo.go,而是以.pb.go为后缀的标准Protobuf生成文件,此时导入路径echoTest/kitex_gen/echo可正常识别。
内容的提问来源于stack exchange,提问作者john tan
相关产品推荐
相关产品推荐

