I want to configure a gRPC server in my Spring Boot application (Java 17), but I am encountering the following error when trying to instantiate the server:
package org.xenwgram.config;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.ServerCredentials;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xenwgram.service.Impl.MessageServiceImpl;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Configuration
public class GrpcServerConfig {
@Bean
public Server grpcServer() {
try {
Server server = Grpc.newServerBuilderForPort(6565, InsecureServerCredentials.create())
.build();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
return server;
} catch (IOException e) {
throw new RuntimeException("Failed to start gRPC server", e);
}
}
}
I am using the following dependencies in my build.gradle.kts file:
plugins {
id("java")
id("com.google.protobuf") version "0.9.4"
id("io.spring.dependency-management") version "1.1.6"
id("org.springframework.boot") version "2.7.16"
}
group = "org.xenwgram"
version = "v0.0.1"
repositories {
mavenLocal()
mavenCentral()
maven("https://gitlab.com/api/v4/projects/65346162/packages/maven")
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("org.xenwgram:xengram-proto:test-1.1")
// Spring Boot starter web
implementation("org.springframework.boot:spring-boot-starter-web:3.4.0")
// Lombok for compile-time code generation
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
implementation("org.projectlombok:lombok-mapstruct-binding:0.1.0")
// gRPC Spring Boot starter
implementation("net.devh:grpc-server-spring-boot-starter:3.1.0.RELEASE")
// gRPC dependencies
implementation("io.grpc:grpc-netty-shaded:1.69.0")
implementation("io.grpc:grpc-protobuf:1.69.0")
implementation("io.grpc:grpc-stub:1.69.0")
// Google Protocol Buffers
implementation("com.google.protobuf:protobuf-java:4.29.1")
}
tasks.test {
useJUnitPlatform()
}
The error I am encountering:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'grpcServer' defined in class path resource [org/xenwgram/config/GrpcServerConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.grpc.Server]: Factory method 'grpcServer' threw exception; nested exception is java.util.ServiceConfigurationError: io.grpc.ServerProvider: io.grpc.netty.NettyServerProvider Unable to get public no-arg constructor
What I have tried:
- When using the auto-configuration provided by the grpc-server-spring-boot-starter, everything works fine without issues.
- When I use a custom server configuration as shown in the code above, I get the error above.
What I would like to understand:
- Why am I encountering the ServiceConfigurationError with NettyServerProvider?
- Is there something wrong with how I am configuring the gRPC server in Spring Boot?
- Should I switch to using the auto-configuration provided by Spring Boot starter?
Any help would be greatly appreciated! Thanks in advance!
implementation("io.grpc:grpc-netty:1.69.0")should fix it. I'd have to spend more time to detail what is happening.