Skip to content

Commit bb3c3fd

Browse files
committed
BAEL-9013 Add code for intro to spring grpc
1 parent bb337c9 commit bb3c3fd

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@
794794
<module>spring-drools</module>
795795
<module>spring-ejb-modules</module>
796796
<module>spring-exceptions</module>
797+
<module>spring-grpc</module>
797798
<module>spring-jersey</module>
798799
<module>spring-kafka</module>
799800
<module>spring-kafka-2</module>
@@ -1226,6 +1227,7 @@
12261227
<module>spring-drools</module>
12271228
<module>spring-ejb-modules</module>
12281229
<module>spring-exceptions</module>
1230+
<module>spring-grpc</module>
12291231
<module>spring-jersey</module>
12301232
<module>spring-kafka</module>
12311233
<module>spring-kafka-2</module>

spring-grpc/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Spring Protocol Buffers
2+
3+
This module contains articles about Spring with gRPC

spring-grpc/pom.xml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-grpc</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<name>spring-grpc</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-boot-3</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-boot-3</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.grpc</groupId>
20+
<artifactId>grpc-services</artifactId>
21+
<version>${grpc-services.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.grpc</groupId>
25+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
26+
<version>${spring-grpc.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<version>${spring-boot.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.grpc</groupId>
37+
<artifactId>spring-grpc-test</artifactId>
38+
<version>${spring-grpc.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-core</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>ch.qos.logback</groupId>
47+
<artifactId>logback-classic</artifactId>
48+
</dependency>
49+
</dependencies>
50+
<dependencyManagement>
51+
<dependencies>
52+
<dependency>
53+
<groupId>org.springframework.grpc</groupId>
54+
<artifactId>spring-grpc-dependencies</artifactId>
55+
<version>${spring-grpc.version}</version>
56+
<type>pom</type>
57+
<scope>import</scope>
58+
</dependency>
59+
</dependencies>
60+
</dependencyManagement>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>kr.motd.maven</groupId>
66+
<artifactId>os-maven-plugin</artifactId>
67+
<version>1.7.1</version>
68+
<executions>
69+
<execution>
70+
<id>initialize</id>
71+
<phase>initialize</phase>
72+
<goals>
73+
<goal>detect</goal>
74+
</goals>
75+
</execution>
76+
</executions>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.xolstice.maven.plugins</groupId>
80+
<artifactId>protobuf-maven-plugin</artifactId>
81+
<version>0.6.1</version>
82+
<configuration>
83+
<protocArtifact>com.google.protobuf:protoc:${protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
84+
<pluginId>grpc-java</pluginId>
85+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
86+
</configuration>
87+
<executions>
88+
<execution>
89+
<id>compile</id>
90+
<goals>
91+
<goal>compile</goal>
92+
<goal>compile-custom</goal>
93+
</goals>
94+
<configuration>
95+
<pluginParameter>jakarta_omit,@generated=omit</pluginParameter>
96+
</configuration>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
103+
<properties>
104+
<java.version>17</java.version>
105+
<grpc.version>1.72.0</grpc.version>
106+
<protobuf-java.version>4.30.2</protobuf-java.version>
107+
<spring-grpc.version>0.8.0</spring-grpc.version>
108+
<grpc-services.version>1.72.0</grpc-services.version>
109+
<spring-grpc.version>0.8.0</spring-grpc.version>
110+
<spring-boot.version>3.4.5</spring-boot.version>
111+
</properties>
112+
113+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.grpc;
2+
3+
import org.springframework.stereotype.Service;
4+
import org.springframework.grpc.calculator.proto.CalculatorGrpc;
5+
import org.springframework.grpc.calculator.proto.Response;
6+
import org.springframework.grpc.calculator.proto.Request;
7+
8+
import io.grpc.stub.StreamObserver;
9+
10+
@Service
11+
public class GrpcCalculatorService extends CalculatorGrpc.CalculatorImplBase {
12+
13+
@Override
14+
public void multiply(Request req, StreamObserver<Response> responseObserver) {
15+
Response reply = Response.newBuilder().setResult(req.getFirstValue() * req.getSecondValue()).build();
16+
responseObserver.onNext(reply);
17+
responseObserver.onCompleted();
18+
}
19+
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.grpc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringgRPCApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringgRPCApplication.class, args);
11+
}
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
syntax = "proto3";
2+
3+
option java_multiple_files = true;
4+
option java_package = "org.springframework.grpc.calculator.proto";
5+
option java_outer_classname = "CalculatorProto";
6+
7+
service Calculator {
8+
rpc Multiply(Request) returns (Response) {}
9+
}
10+
11+
message Request {
12+
int32 firstValue = 1;
13+
int32 secondValue = 2;
14+
}
15+
16+
message Response {
17+
int32 result = 1;
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=Calculator

0 commit comments

Comments
 (0)