Environment:
- Micronaut version: 4.6.3
- JDK: 17
I have a rest api developed with Micronaut. Now I want to add the feature of gRPC client to this rest api for calling a gRPC server at http://localhost:50051.
I have read https://micronaut-projects.github.io/micronaut-grpc/latest/guide/index.html but there are still some information not described clearly.
Following the suggestion of chatgpt, I add some lines and blocks to my current build.gradle
build.gradle
plugins {
id("groovy")
id("com.github.johnrengelman.shadow") version "8.1.1"
id("io.micronaut.application") version "4.4.2"
id("io.micronaut.aot") version "4.4.2"
id("com.google.protobuf") version "0.9.2" // I add this line
}
version = "0.1"
group = "com.example"
repositories {
mavenCentral()
}
dependencies {
annotationProcessor("org.projectlombok:lombok")
annotationProcessor("io.micronaut:micronaut-http-validation")
annotationProcessor("io.micronaut.serde:micronaut-serde-processor")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut.serde:micronaut-serde-jackson")
implementation("jakarta.annotation:jakarta.annotation-api") // I add this line
compileOnly("org.projectlombok:lombok")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("org.yaml:snakeyaml")
// I add below 2 lines
implementation("io.micronaut.grpc:micronaut-grpc-runtime")
implementation("io.micronaut.grpc:micronaut-grpc-client-runtime")
}
application {
mainClass = "com.example.Application"
}
java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("netty")
testRuntime("spock2")
processing {
incremental(true)
annotations("com.example.*")
}
aot {
// Please review carefully the optimizations enabled below
// Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
optimizeServiceLoading = false
convertYamlToJava = false
precomputeOperations = true
cacheEnvironment = true
optimizeClassLoading = true
deduceEnvironment = true
optimizeNetty = true
replaceLogbackXml = true
}
}
// I add below protobuf block
protobuf {
protoc { artifact = "com.google.protobuf:protoc:3.25.5" }
plugins {
grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.66.0" }
}
generateProtoTasks {
all()*.plugins {grpc {}}
}
}
// I add below sourceSets block
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
application.yml
micronaut:
application:
name: grpc-http-client-example
server:
port: 8080
## I add below grpc block
grpc:
channels:
greeter:
address: 'http://localhost:50051'
plaintext: true
Also, I add a .proto file proviede by the gRPC-server as below to src/main/proto
grpc-server-example.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
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;
}
Now I execute below command:
./gradlew clean generateProto
./gradlew run
Then it appears below error:
9:23:57 a.m.: Executing ':classes'...
> Task :extractIncludeProto UP-TO-DATE
> Task :extractProto UP-TO-DATE
> Task :generateProto UP-TO-DATE
> Task :compileJava FAILED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.8/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
4 actionable tasks: 1 executed, 3 up-to-date
E:\Project-Space\microservices\grpc-http-client2-example\build\generated\source\proto\main\grpc\com\example\GreeterGrpc.java:10: error: cannot find symbol
@javax.annotation.Generated(
^
symbol: class Generated
location: package javax.annotation
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
Below is the directory structure of the rest api project
How to fix the error?
