Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling GRPC server/client

I'm having a lot of trouble figuring out how to compile a GRPC Java server. I looked all over the grpc.io website and closest thing I found was this: http://www.grpc.io/docs/#quick-start , where I run ../gradlew -PskipCodegen=true installDist to build, and ./build/install/grpc-examples/bin/hello-world-client to run the client. This all works, but only for the hello-world tutorial. I have no idea how to do this for my own client/server. I'm able to generate the client/server protobufs using the .proto file. I looked in their readme and Java tutorial and couldn't find out how to compile the actual server (and client) after I write them https://github.com/grpc/grpc-java/blob/master/examples/README.md (can't link java tutorial because I dont have enough reputation). Unless there's documentation im missing, does anyone know how to compile a server and client that implements the GRPC classes generated from the .proto file? I did spend a fair amount of time searching. Any advice is much appreciated, thanks.

like image 620
user3125693 Avatar asked Mar 14 '23 08:03

user3125693


2 Answers

Also have a similar problem, ensure that:

  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.

The idea plugin, is jut for telling IntelliJ to recognize the src/main/proto as a source set.

  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) } }
    
like image 188
Ivan Avatar answered Mar 20 '23 06:03

Ivan


I had a similar issue but solved it using it in Gradle by adding the 'application' plugin. Before I was using the 'java' plugin and I could only generated a jar file. After switching to the 'application' plugin there is a gradle task similar to the gRPC example.

./gradlew installDist

And now to start your server you can run something similar to this:

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

To actually generate the Java classes off my .proto files I needed to run './gradle build' and also include the source generated using the sourceDir element you can see in the build.gradle below.

This is the full build.gradle file.

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");
    }
}

I am new to gRPC so any improvments to my Gradle file would be appericated.

like image 26
Geekygecko Avatar answered Mar 20 '23 04:03

Geekygecko