编译GRPC服务器/客户端

4

我在编译GRPC Java服务器方面遇到了很多问题。我查看了grpc.io网站,最接近的东西是这个:http://www.grpc.io/docs/#quick-start,我运行以下命令进行构建: ../gradlew -PskipCodegen=true installDist, 并且使用以下命令运行客户端: ./build/install/grpc-examples/bin/hello-world-client, 这一切都能正常运行,但只适用于hello-world教程。我不知道如何为自己的客户端/服务器执行此操作。我可以使用.proto文件生成客户端/服务器protobufs。我在他们的自述文件和Java教程中寻找了一下,但没有找到如何在编写后编译实际服务器(和客户端)的方法。 https://github.com/grpc/grpc-java/blob/master/examples/README.md (无法链接Java教程,因为我的声望不够)。除非有文档我漏掉了,否则有人知道如何编译实现从.proto文件生成的GRPC类的服务器和客户端吗?我花了相当长时间搜索。非常感谢任何建议。

3个回答

6
也有一个类似的问题,请确保:
  1. You configured correctly the protoc, that will be downloading the executable and configure it as an environment variable of your OS.
  2. The build.gradle file, make sure to include protobuf-gradle-plugin:

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'com.google.protobuf'
    
    ext.grpcVersion = '1.0.1'
    ext.protobufVersion = '3.0.2'
    
    buildscript {
    repositories { mavenCentral() }
    dependencies { 
    classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0' }
    }
    
    repositories {
    mavenCentral()
    }
    
    dependencies {
    compile "com.google.protobuf:protobuf-java:${protobufVersion}"
    compile "io.grpc:grpc-all:${grpcVersion}"
    }
    
    protobuf {
    protoc { artifact = "com.google.protobuf:protoc:${protobufVersion}"     }
    plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } }
    generateProtoTasks { ofSourceSet('main')*.plugins { grpc { } } }
    }
    
    idea {
    module {
        sourceDirs +=     file("${protobuf.generatedFilesBaseDir}/main/java");
        sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/grpc");
    }
    }
    
  3. Your proto file:

    syntax = "proto3";
    package com.company.project;
    
    service CompanyService{
        rpc call(RequestMessage) returns (ResponseMessage) {}
    }
    
  4. Run gradle clean build, and it should generate your service and client classes for CompanyService.

这个 idea 插件只是告诉 IntelliJ 把 src/main/proto 识别为源代码集。

  1. To actually execute the client and server, you will need to make the implementation, mentioned in the tutorial for gRpc, and then apply the application plugin, in order to generate correctly the executable jar

    //build.grdle code...
    apply plugin: 'application'
    mainClassName = 'com.company.project.MainClass'
    jar { manifest { attributes('Main-Class' : mainClassName) } }
    

1

我曾经遇到过类似的问题,但是通过在Gradle中添加“application”插件来解决了它。在此之前,我使用的是“java”插件,只能生成jar文件。切换到“application”插件后,就有一个类似于gRPC示例的gradle任务。

./gradlew installDist

现在,您可以运行类似于以下内容的命令来启动服务器:

./build/install/your-project/bin/your-server

为了从我的.proto文件中生成Java类,我需要运行“./gradle build”,并且还要包含使用下面的build.gradle中的sourceDir元素生成的源代码。
这是完整的build.gradle文件。
apply plugin: 'application'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
    }
}

repositories {
    jcenter()
}

dependencies {
    compile 'io.grpc:grpc-all:0.14.0'
}

mainClassName = "com.domain.service.YourMainClass"

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.0.0-beta-2"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:0.14.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

idea {
    module {
        sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
        sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
    }
}

我对 gRPC 还不熟悉,如果有任何改进 Gradle 文件的建议,将不胜感激。


0

这个问题已经被 'Eric Anderson' 在 groups.google.com 上回答:

The JAR only has your code in it. It sounds like you want to make a "fat" jar which includes all your dependencies. You can do something like this:

jar {
      from {
          configurations.compile.collect {
              it.isDirectory() ? it : zipTree(it)
        }
    }
}

Note that that isn't gRPC-specific; it's just working with Gradle. There may be alternatives, such as a quick Googling returned gradle-fatjar-plugin.


我已经编辑了你的答案,引用了你链接到的Google Groups上的答案。这样,你的答案就可以免受链接失效的影响,并且不会被视为仅有链接的答案而被删除。 - Wai Ha Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接