我正在尝试在 intelij 中使用 protoc jar maven 插件。我已将此添加到我的 pom.xml 中:
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.1.0.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<includeDirectories>
<include>src/main/protobuf</include>
</includeDirectories>
<inputDirectories>
<include>src/main/protobuf</include>
</inputDirectories>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我有一个位于src/main/protobuf. 我的 pom.xml 中也有这个:
<dependency>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar</artifactId>
<version>3.1.0.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是原型:
package stringtest;
message StringObject {
repeated string string_one = 1;
repeated string string_two = 2;
repeated string string_three = 3;
}
Run Code Online (Sandbox Code Playgroud)
但是,无论何时我右键单击我的模块并单击构建,或者如果我转到构建菜单并点击构建项目,我都看不到正在生成任何已编译的 protobuf 文件。我也没有看到任何错误。有谁知道我做错了什么?
我已经在我的 linux ubuntu 上克隆了链接器(https://github.com/linkerd)repo,并安装了 protoc,版本 2.5。当我尝试使用以下命令编译链接器时,出现错误“无法识别的语法标识符“proto3”。此解析器仅识别“proto2”。
命令: ./sbt linkerd/compile
假设我有 test.proto 文件:
syntax = "proto3";
option java_package = "testing";
option java_outer_classname = "Test_v1";
import "google/protobuf/wrappers.proto";
message TestMessage {
.google.protobuf.Int32Value integerField = 1;
}
Run Code Online (Sandbox Code Playgroud)
如果我将它编译(使用 protoc v3.5.0)到 C# 代码,我将获得可为空的类型属性:
public int? IntegerField {
get { return integerField_; }
set { integerField_ = value; }
}
Run Code Online (Sandbox Code Playgroud)
但是如果我将它编译为 Java 代码,字段将是众所周知的类型:
public Builder setIntegerField(com.google.protobuf.Int32Value value) {
if (integerFieldBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
integerField_ = value;
onChanged();
} else {
integerFieldBuilder_.setMessage(value);
}
Run Code Online (Sandbox Code Playgroud)
我正在将项目从 proto2 移至 proto3,因此我想避免使用众所周知的类型,因为更改相关代码需要大量工作。对于 …
我一直在尝试使用 protobuf 类型google.protobuf.Timestamp,protoc-jar-maven-plugin但只得到这些编译时错误:
google/protobuf/timestamp.proto: File not found.
test.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
test.proto:9:5: "google.protobuf.Timestamp" is not defined.
Run Code Online (Sandbox Code Playgroud)
proto 文件如下所示:
syntax = "proto3";
import "google/protobuf/timestamp.proto";
package test;
option java_package = "test";
message TestTimestamp {
google.protobuf.Timestamp liveStartDate = 1;
}
Run Code Online (Sandbox Code Playgroud)
而pom文件有如下配置:
<build>
<plugins>
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.6.0.1</version>
<executions>
<execution>
<id>protoc.main</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocVersion>3.6.0</protocVersion>
<addSources>main</addSources>
<includeDirectories>
<include>src/main/protobuf</include>
</includeDirectories>
<inputDirectories>
<include>src/main/protobuf</include>
</inputDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
据我了解,这种类型是 proto3 的一部分,为什么会出现这些错误?
我运行以下.sh文件来构建我的原型。
mkdir -p ./src/gen
protoc -I=../protos/ \
../protos/common/*.proto \
../protos/service/protos/*.proto \
../protos/ui/*.proto \
../protos/*.proto \
--js_out=import_style=commonjs,binary:./src/gen \
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但是,我的团队可能会将其他目录添加到protos该文件看不到的目录中。
有没有办法更新此脚本以获取目录.proto中的所有文件protos?
我尝试了以下方法,但没有成功:
mkdir -p ./src/gen
protoc -I=../protos/**/*.proto \
--js_out=import_style=commonjs,binary:./src/gen \
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen
Run Code Online (Sandbox Code Playgroud)
我确信这只是一些语法问题,但我不知道该怎么做。
编辑:
我已经实现了一些东西来使我的生成器更加灵活,但它仍然很混乱:
mkdir -p ./src/gen
protoc -I=../protos/ \
../protos/*.proto \
../protos/**/*.proto \
../protos/**/**/*.proto \
../protos/**/**/**/*.proto \
../protos/**/**/**/**/*.proto \
--js_out=import_style=commonjs,binary:./src/gen \
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen
Run Code Online (Sandbox Code Playgroud) 假设我有一条消息定义为test.proto:
message TestMessage {\n int64 id = 1;\n string title = 2;\n string subtitle = 3;\n string description = 4;\n}\nRun Code Online (Sandbox Code Playgroud)\n我使用 protoc 将其转换为 Python,如下所示:
\nprotoc --python_out=. test.proto
时间PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python:
from test_pb2 import TestMessage\n\n%%timeit\ntm = TestMessage()\ntm.id = 1\ntm.title = 'test title'\ntm.subtitle = 'test subtitle'\ntm.description = 'this is a test description'\nRun Code Online (Sandbox Code Playgroud)\n6.75 \xc2\xb5s \xc2\xb1 152 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)
时间PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp:
我是 grpc 菜鸟,想创建一个 REST 端点来检查 grpc 服务器的健康状况。为此我决定使用 grpc 网关。
但是,文档中用于将 grpc 代理为 json 的 grpc-REST 网关的示例仅适用于 Golang。但我有一个 python 后端,想要使用 Google Cloud Endpoints 制作一个基于 gRPC 的 api,可以对传入的 REST 请求进行转码。
我确实在这个 stackoverflow答案中找到了手动生成注释的方法。但我想知道最好的方法是什么。这是我想要生成到类中的示例 .proto 文件。
syntax = "proto3";
package example;
+
+import "google/api/annotations.proto";
+
message StringMessage {
string value = 1;
}
service YourService {
- rpc Echo(StringMessage) returns (StringMessage) {}
+ rpc Echo(StringMessage) returns (StringMessage) {
+ option (google.api.http) = {
+ post: "/v1/example/echo"
+ body: "*" …Run Code Online (Sandbox Code Playgroud) 我在不同的 go 包下编译了 2 个 proto,但是当我在 aa 服务器中注册它们并运行它时,我得到:
panic: proto: file "common.proto" is already registered
previously from: "github.com/soft/test-platform.go/common"
currently from: "github.com/soft/proto-asterix/asterix"
Run Code Online (Sandbox Code Playgroud)
这是测试平台存储库(/api文件夹中)中的common.proto :
syntax = "proto3";
package soft.testplatform.common; // here I do defint a UNIQUE package name !
option java_multiple_files = true;
option go_package = "github.com/soft/test-platform.go/common"; // Here I do define a unique go package name !
message MyMessage{
string commandId = 1;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,go_package和package的包定义不会与来自 github.com/soft/proto-asterix/asterix 的包发生冲突。只有 .proto 文件名相似(common.proto)。
我使用以下命令生成带有 protoc 和 protoc-gen-go 插件的 …
是否可以使用 protoc 从生成的 pb2.py 中获取 proto 文件?gRPC 是否可以进行相同的逆向工程?
当我编译我的原型文件时,我收到此错误:
protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
\--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
Run Code Online (Sandbox Code Playgroud)
我正在使用这些版本:
| 二进制 | 版本 |
|---|---|
| 原型-gen-go | v1.25.0-开发 |
| 协议 | v3.12.4 |
我尝试编译这个原型文件:
protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
\--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
Run Code Online (Sandbox Code Playgroud)
我使用了这个命令: …
protoc ×10
grpc ×3
go ×2
grpc-python ×2
java ×2
maven ×2
proto ×2
grpc-gateway ×1
grpc-web ×1
linkerd ×1
proto3 ×1
python ×1
python-3.x ×1
sh ×1