1

Java 和 gRPC 大师们祝你有美好的一天。

我一直在关注这个https://github.com/jpdna/gRPC-maven-helloworld,因为我正在使用 Java 学习 gRPC。

我能够使用 mvn clean package 编译它。但是当我在 Eclipse 中加载项目时,文件:

org.jpdna.grpchello.HelloWorldServer

正在寻找班级“ GreeterGrpc”。

我尝试在终端中执行此操作:

$ protoc --java_out=/gRPC-maven-helloworld/src/main/java hello_world.proto

它生成了以下类:

  - HelloRequest.java
  - HelloRequestOrBuilder.java
  - HelloResponse.java
  - HelloResponseOrBuilder.java
  - HelloWorldProto.java

但是没有 GreeterGrpc.java 被定义为这个项目中的服务。如果你不介意我问,我想知道如何创建或生成这个 GreeterGrpc.java 类?

非常感谢你们!

4

2 回答 2

3

complie 时需要一个protoc插件protoc-gen-grpc-java来生成。GreeterGrpc.classhello_world.proto

或者您可以使用 maven 插件来实现相同的目的。我的 Maven 配置:

<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-all</artifactId>
    <version>1.0.3</version>
</dependency>


<extension>
    <groupId>kr.motd.maven</groupId>
    <artifactId>os-maven-plugin</artifactId>
    <version>1.5.0.Final</version>
</extension>


<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
    <execution>
        <id>uncompress</id>
        <phase>install</phase>
        <goals>
            <goal>exec</goal>
        </goals>
        <configuration>
            <executable>${basedir}/bin/upgrade.sh ${environment}</executable>
        </configuration>
    </execution>
</executions>
</plugin>


<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.5.0</version>
    <configuration>
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.3:exe:${os.detected.classifier}</pluginArtifact>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>compile-custom</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2017-07-14T08:50:36.007 回答
1

我找到了我的问题的答案......解决方案就是右键单击 Eclipse 上的项目,然后选择 Run As --> Maven generate-sources。一旦在目标文件夹中生成源代码及其文件夹。右键单击那些生成的文件夹(带有源代码),然后选择 Build Path --> Use as Source Folder。该错误将消失,因为它将能够解决缺少的类。

谢谢,祝你有美好的一天=)!

于 2017-08-01T22:51:04.023 回答