簡體   English   中英

通過gRPC / Protobuf進行通訊

[英]Communication through gRPC/Protobuf

我之所以來是因為無法進行API(Go)和客戶端(Android)之間的通信。

我有這個protobuf文件:

syntax = "proto3";

option java_package = "com.emixam23.rushpoc.protobuf";
option java_outer_classname = "HelloWorld";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

protobuf文件來自https://grpc.io/docs/quickstart/go.html的示例,我只是沒有實現SayHelloAgain。 我想要實現的是,從我的Android應用程序SayHello到我的Go API並獲得回復...

對於android系統,我遵循了該教程( https://grpc.io/docs/quickstart/android.html ),以便從protobuf文件中與我的API通信。 但是,有一個stub ,不知道從哪里來。

因此,我搜索了如何創建存根( https://grpc.io/docs/tutorials/basic/android.html ),一無所有。ManagedChannelBuilder不存在,我找不到安裝它的方法。

PS:要從protobuf文件生成Java類,我遵循了該教程: https : //proandroiddev.com/how-to-setup-your-android-app-to-use-protobuf-96132340de5c

我是朝正確的方向還是完全錯誤的方向?

我的項目結構:

在此處輸入圖片說明

APP build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.rushpoc.emixam23.androidapp"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //Protobuf
    implementation 'com.google.protobuf:protobuf-lite:3.0.0'

    implementation 'io.grpc:grpc-okhttp:1.13.2'
    implementation 'io.grpc:grpc-protobuf-lite:1.13.2'
    implementation 'io.grpc:grpc-stub:1.13.2'
}

protobuf {
    generatedFilesBaseDir = "$projectDir/generated"
    protoc {
        // You still need protoc like in the non-Android case
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            // The codegen for lite comes as a separate artifact
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.13.2'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java
            }
            task.plugins {
                grpc {}
            }
        }
    }
}

build.gradle 級別/根 build.gradle

//頂層構建文件,您可以在其中添加所有子項目/模塊共有的配置選項。

buildscript {
    ext.protobufVersion = '0.8.6'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufVersion"


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我還沒有檢查整個gradle文件,但是我在您的屏幕快照中看到.proto文件位於src/main/protobufs ,該文件沒有遵循您提到的任何一個教程。 默認情況下,protobuf gradle插件不會檢測到此目錄。 因此,我建議您將其更改為默認目錄src/main/proto 如果您想堅持將.proto文件放在src/main/protobufs ,則可能需要通過添加來讓protobuf gradle插件知道它

// see https://github.com/google/protobuf-gradle-plugin#customizing-source-directories    
sourceSets {
  main {
    proto {
      // In addition to the default 'src/main/proto'
      srcDir 'src/main/protobufs'
    }
  }
}

之后,如果沒有其他錯誤,protobuf gradle插件將生成Java代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM