GRPC channel in Spring Boot application cause ClassNotFoundException for AbstractManagedChannelImplBuilder

80 Views Asked by At

When I configure a ManagedChannelBuilder for a GRPC client, the method ManagedChannelBuilder.forAddress(...) causes a java.lang.NoClassDefFoundError for the class io.grpc.internal.AbstractManagedChannelImplBuilder.

This is caused by the grpc-api libary in the class ManagedChannelRegistry, when it tries to load the class io.grpc.netty.NettyChannelProvider.

GRPC client:

public class GrpcClient {
    public void request() throws InterruptedException {
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("myGrpcServerAddress",
                        12345)
                .useTransportSecurity()
                .keepAliveTime(1, TimeUnit.MINUTES)
                .keepAliveTimeout(10, TimeUnit.SECONDS).build()

        MyServiceGrpc.MyServiceBlockingStub stub 
          = MyServiceGrpc.newBlockingStub(channel);

        MyResponse myResponse = stub.hello(MyRequest.newBuilder()
            .setFirstName("test")
            .setLastName("test")
            .build());

        System.out.println("Response received from server:\n" + myResponse);

        channel.shutdown();
    }
}

The following method is causing the exception:

ManagedChannelBuilder.forAddress("myGrpcServerAddress",12345)

Method of the library which causing the exception:

    static List<Class<?>> getHardCodedClasses() {
        List<Class<?>> list = new ArrayList();
       ...
        try {
            list.add(Class.forName("io.grpc.netty.NettyChannelProvider"));
        } catch (ClassNotFoundException var3) {
            logger.log(Level.FINE, "Unable to find NettyChannelProvider", var3);
        }

       ...

The following dependencies were used to run the grpc client:

"io.grpc:grpc-stub:1.61.1"
"io.grpc:grpc-protobuf:1.61.1"
"com.google.protobuf:protobuf-java-util:3.7.1"
"io.grpc:grpc-netty:1.61.1"
"io.grpc:grpc-netty-shaded:1.61.1"

I already tried different grpc versions with the same result. Any ideas why this is happening?

0

There are 0 best solutions below