diff --git a/pom.xml b/pom.xml index d3db76e746de..b041a694074a 100644 --- a/pom.xml +++ b/pom.xml @@ -794,6 +794,7 @@ spring-drools spring-ejb-modules spring-exceptions + spring-grpc spring-jersey spring-kafka spring-kafka-2 @@ -1226,6 +1227,7 @@ spring-drools spring-ejb-modules spring-exceptions + spring-grpc spring-jersey spring-kafka spring-kafka-2 diff --git a/spring-grpc/README.md b/spring-grpc/README.md new file mode 100644 index 000000000000..7c9e7b0db64d --- /dev/null +++ b/spring-grpc/README.md @@ -0,0 +1,3 @@ +## Spring Protocol Buffers + +This module contains articles about Spring with gRPC diff --git a/spring-grpc/pom.xml b/spring-grpc/pom.xml new file mode 100644 index 000000000000..1085c90d3813 --- /dev/null +++ b/spring-grpc/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + spring-grpc + 0.1-SNAPSHOT + spring-grpc + + + com.baeldung + parent-boot-3 + 0.0.1-SNAPSHOT + ../parent-boot-3 + + + + + io.grpc + grpc-services + ${grpc-services.version} + + + org.springframework.grpc + spring-grpc-spring-boot-starter + ${spring-grpc.version} + + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + org.springframework.grpc + spring-grpc-test + ${spring-grpc.version} + test + + + ch.qos.logback + logback-core + + + ch.qos.logback + logback-classic + + + + + + org.springframework.grpc + spring-grpc-dependencies + ${spring-grpc.version} + pom + import + + + + + + + + kr.motd.maven + os-maven-plugin + 1.7.1 + + + initialize + initialize + + detect + + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.6.1 + + com.google.protobuf:protoc:${protobuf-java.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + + + compile + + compile + compile-custom + + + jakarta_omit,@generated=omit + + + + + + + + + 17 + 1.72.0 + 4.30.2 + 0.8.0 + 1.72.0 + 0.8.0 + 3.4.5 + + + \ No newline at end of file diff --git a/spring-grpc/src/main/java/com/baeldung/grpc/GrpcCalculatorService.java b/spring-grpc/src/main/java/com/baeldung/grpc/GrpcCalculatorService.java new file mode 100644 index 000000000000..25890b5e1c9c --- /dev/null +++ b/spring-grpc/src/main/java/com/baeldung/grpc/GrpcCalculatorService.java @@ -0,0 +1,20 @@ +package com.baeldung.grpc; + +import org.springframework.stereotype.Service; +import org.springframework.grpc.calculator.proto.CalculatorGrpc; +import org.springframework.grpc.calculator.proto.Response; +import org.springframework.grpc.calculator.proto.Request; + +import io.grpc.stub.StreamObserver; + +@Service +public class GrpcCalculatorService extends CalculatorGrpc.CalculatorImplBase { + + @Override + public void multiply(Request req, StreamObserver responseObserver) { + Response reply = Response.newBuilder().setResult(req.getFirstValue() * req.getSecondValue()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + +} diff --git a/spring-grpc/src/main/java/com/baeldung/grpc/SpringgRPCApplication.java b/spring-grpc/src/main/java/com/baeldung/grpc/SpringgRPCApplication.java new file mode 100644 index 000000000000..30e284d14e79 --- /dev/null +++ b/spring-grpc/src/main/java/com/baeldung/grpc/SpringgRPCApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.grpc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringgRPCApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringgRPCApplication.class, args); + } + +} diff --git a/spring-grpc/src/main/proto/calculator.proto b/spring-grpc/src/main/proto/calculator.proto new file mode 100644 index 000000000000..fecd033cfd74 --- /dev/null +++ b/spring-grpc/src/main/proto/calculator.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "org.springframework.grpc.calculator.proto"; +option java_outer_classname = "CalculatorProto"; + +service Calculator { + rpc Multiply(Request) returns (Response) {} +} + +message Request { + int32 firstValue = 1; + int32 secondValue = 2; +} + +message Response { + int32 result = 1; +} \ No newline at end of file diff --git a/spring-grpc/src/main/resources/application.properties b/spring-grpc/src/main/resources/application.properties new file mode 100644 index 000000000000..8addb2e8a215 --- /dev/null +++ b/spring-grpc/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=Calculator \ No newline at end of file