繁体   English   中英

grpc-java的bindableService问题

[英]bindableService issue with grpc-java

我正在尝试使用grpc-java v1.1.2(下面的build.gradle部分),但是当我尝试为示例应用程序运行胖罐时,它抛出下面给出的异常。 编译应用程序时看不到任何问题。

build.gradle部分:

    apply plugin: 'com.google.protobuf'

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
            // gradle versions
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }


    def grpcVersion = '1.1.2'   //''1.2.0-SNAPSHOT' // CURRENT_GRPC_VERSION

    dependencies {
        compile "io.grpc:grpc-netty:${grpcVersion}"
        compile "io.grpc:grpc-protobuf:${grpcVersion}"
        compile "io.grpc:grpc-stub:${grpcVersion}"
        compile 'me.grapebaba:hyperledger-java-client:0.1.3'
}


    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.2.0'
        }
        plugins {
            grpc {
                artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
            }
        }
        generateProtoTasks {
            all()*.plugins {
                grpc {
                    // To generate deprecated interfaces and static bindService method,
                    // turn the enable_deprecated option to true below:
                    option 'enable_deprecated=false'
                }
            }
        }
    }

    idea {
        module {
            // Not using generatedSourceDirs because of
            // https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
            sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
        }
    }


    jar {
        manifest {
               attributes(
                    'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                    'Main-Class': 'com.test.io.grpc.HelloWorldServer'
            )
        }
    }



    task helloWorldServer(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldServer'
        applicationName = 'hello-world-server'
        outputDir = new File(project.buildDir, 'tmp')
        classpath = jar.outputs.files + project.configurations.runtime
    }

    task helloWorldClient(type: CreateStartScripts) {
        mainClassName = 'io.grpc.mgcs.HelloWorldClient'
            applicationName = 'hello-world-client'
            outputDir = new File(project.buildDir, 'tmp')
            classpath = jar.outputs.files + project.configurations.runtime
    }

    applicationDistribution.into('bin') {
        from(helloWorldServer)
        from(helloWorldClient)
        fileMode = 0755
    }

例外

Caused by: java.lang.ClassNotFoundException: io.grpc.BindableService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我通过使用影子插件创​​建胖罐来解决此问题。

    buildscript {
        repositories {
            mavenCentral()
            mavenLocal()
        }
        dependencies {
            classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
            classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
            classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
        }
    }

    plugins {
        id 'java'
        id 'application'
        id 'idea'
        id 'com.github.johnrengelman.shadow' version '1.2.4'
        id "net.ltgt.errorprone" version '0.0.9'
    }


    shadowJar {
        baseName = 'shadow'
        classifier = null
        version = null
    }

jar {
    manifest {
        attributes(
                'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' '),
                'Main-Class': 'com.abc.test'
        )
    }
}

运行以下命令:

gradle shadowJar

shadowJar文件在build / libs目录中创建,可以通过以下方式运行:

java -jar shadowJar

对我来说,问题是jar不包含依赖的lib类。 我必须进行此更改才能解决此问题:

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

这只是创建胖子的另一种方法:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM