1. gRPC java

1.1. 接入流程

1.1.1. build.gradle

def grpcVersion = '1.21.0' // CURRENT_GRPC_VERSION
def protobufVersion = '3.7.1'
def protocVersion = protobufVersion

sourceSets {
    main {
        proto {
            // In addition to the default 'src/main/proto'
            // .proto 文件路径,默认包含 'src/main/proto'
            // srcDir 'src/main/proto'

            // In addition to the default '**/*.proto' (use with caution).
            // Using an extension other than 'proto' is NOT recommended,
            // because when proto files are published along with class files, we can
            // only tell the type of a file from its extension.
            // 文件后缀,默认包含  '**/*.proto'
            // include '**/*.protodevel'
        }
        java {
            // 和 protobuf.generatedFilesBaseDir 对应,把 protobuf 生成的 java 文件添加到工程
            srcDir 'build/generated/source/proto/main/grpc'
            srcDir 'build/generated/source/proto/main/java'
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
    }
    // 生成的 java 文件 要目录, 默认是 'build/generated'
    // generatedFilesBaseDir = "build/generated"

    generateProtoTasks {
        all()*.plugins { grpc {} }
    }
}


buildscript {
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
    }
}

dependencies {
  // protobuf
  compile "com.google.protobuf:protobuf-java:${protocVersion}"

  // grpc
  compile group: 'io.grpc', name: 'grpc-netty-shaded', version: grpcVersion
  compile group: 'io.grpc', name: 'grpc-protobuf', version: grpcVersion
  compile group: 'io.grpc', name: 'grpc-stub', version: grpcVersion
}

1.1.2. protocol 文件

option java_multiple_files = true;
//生成java代码的package
option java_package = "com.grpc.mistra.generate";
//创建的javaBean的文件名
option java_outer_classname = "MistraProto";
// 可以生成rpc接口
option java_generic_services = true;

1.1.3. protocol 文件转代码

./gradlew generateProto

生成的 java 文件会存储在 build.gradle 中 protobuf.generatedFilesDir 文件夹下

results matching ""

    No results matching ""