Skip to content

Commit aeec8f5

Browse files
Merge pull request eugenp#2353 from eugenp/BAEL-1022-grpc-ganesh
BAEL-1022-grpc-ganesh
2 parents b0e0563 + 6fcbc9b commit aeec8f5

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎grpc/pom.xml‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>grpc</groupId>
6+
<artifactId>grpc-demo</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>grpc-demo</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<io.grpc.version>1.5.0</io.grpc.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>io.grpc</groupId>
21+
<artifactId>grpc-netty</artifactId>
22+
<version>${io.grpc.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>io.grpc</groupId>
26+
<artifactId>grpc-protobuf</artifactId>
27+
<version>${io.grpc.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>io.grpc</groupId>
31+
<artifactId>grpc-stub</artifactId>
32+
<version>${io.grpc.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.12</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
<build>
42+
<extensions>
43+
<extension>
44+
<groupId>kr.motd.maven</groupId>
45+
<artifactId>os-maven-plugin</artifactId>
46+
<version>1.5.0.Final</version>
47+
</extension>
48+
</extensions>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.xolstice.maven.plugins</groupId>
52+
<artifactId>protobuf-maven-plugin</artifactId>
53+
<version>0.5.0</version>
54+
<configuration>
55+
<protocArtifact>
56+
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
57+
</protocArtifact>
58+
<pluginId>grpc-java</pluginId>
59+
<pluginArtifact>
60+
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
61+
</pluginArtifact>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<goals>
66+
<goal>compile</goal>
67+
<goal>compile-custom</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.baeldung.grpc.client;
2+
3+
import org.baeldung.grpc.HelloRequest;
4+
import org.baeldung.grpc.HelloResponse;
5+
import org.baeldung.grpc.HelloServiceGrpc;
6+
7+
import io.grpc.ManagedChannel;
8+
import io.grpc.ManagedChannelBuilder;
9+
10+
public class GrpcClient {
11+
public static void main(String[] args) throws InterruptedException {
12+
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
13+
.usePlaintext(true)
14+
.build();
15+
16+
HelloServiceGrpc.HelloServiceBlockingStub stub
17+
= HelloServiceGrpc.newBlockingStub(channel);
18+
19+
HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
20+
.setFirstName("Baeldung")
21+
.setLastName("gRPC")
22+
.build());
23+
24+
System.out.println("Response received from server:\n" + helloResponse);
25+
26+
channel.shutdown();
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.grpc.server;
2+
3+
import java.io.IOException;
4+
5+
import io.grpc.Server;
6+
import io.grpc.ServerBuilder;
7+
8+
public class GrpcServer {
9+
public static void main(String[] args) throws IOException, InterruptedException {
10+
Server server = ServerBuilder.forPort(8080)
11+
.addService(new HelloServiceImpl()).build();
12+
13+
System.out.println("Starting server...");
14+
server.start();
15+
System.out.println("Server started!");
16+
server.awaitTermination();
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.grpc.server;
2+
3+
import org.baeldung.grpc.HelloRequest;
4+
import org.baeldung.grpc.HelloResponse;
5+
import org.baeldung.grpc.HelloServiceGrpc.HelloServiceImplBase;
6+
7+
import io.grpc.stub.StreamObserver;
8+
9+
public class HelloServiceImpl extends HelloServiceImplBase {
10+
11+
@Override
12+
public void hello(
13+
HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
14+
System.out.println("Request received from client:\n" + request);
15+
16+
String greeting = new StringBuilder().append("Hello, ")
17+
.append(request.getFirstName())
18+
.append(" ")
19+
.append(request.getLastName())
20+
.toString();
21+
22+
HelloResponse response = HelloResponse.newBuilder()
23+
.setGreeting(greeting)
24+
.build();
25+
26+
responseObserver.onNext(response);
27+
responseObserver.onCompleted();
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
option java_multiple_files = true;
3+
package org.baeldung.grpc;
4+
5+
message HelloRequest {
6+
string firstName = 1;
7+
string lastName = 2;
8+
}
9+
10+
message HelloResponse {
11+
string greeting = 1;
12+
}
13+
14+
service HelloService {
15+
rpc hello(HelloRequest) returns (HelloResponse);
16+
}

0 commit comments

Comments
 (0)