设置 gRPC Java

Setting gRPC Java

祝你 Java 和 gRPC 大师们快乐。

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

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

org.jpdna.grpchello.HelloWorldServer

正在寻找 class“GreeterGrpc”。

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

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

它生成了以下 classes:

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

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

非常感谢你们!

您需要一个 protoc 插件 protoc-gen-grpc-java 以在编译时生成 GreeterGrpc.class hello_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>

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

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